- Code: Select all
`import math`

def length_squared(u):

return sum([a ** 2 for a in u])

def length(u):

return math.sqrt(length_squared(u))

def scale_by_scalar(u, scalar):

return [a * scalar for a in u]

def norm(u):

return scale_by_scalar(u, 1 / length(u))

print norm([100, 6])

The answer of this program is: [0.9982048454657786, 0.05989229072794672]

However, if I were to set the print norm function as the following: print norm([100, 0]) it would give me: [1.0, 0.0]

Why does it give me those floating point values if 100 * (1 / sqrt(100^2)) is 1 and 6 * (1 / sqrt(6^2)) is 1? I also thought sum adds up these two values which would give 2?