BlueMango
BlueMango

Reputation: 503

np.linalg.norm and how to deal with machine epsilon

I have this fairly simple problem. I want to calculate Euclidean distance with numpy with this code:

a= np.array([1,2,3])
b= np.array([2,3,4])

print((np.linalg.norm(a-b))**2)

This yields 2.9999999999999996, However, the answer should be 3. How do I achieve this?

Thanks

Upvotes: 0

Views: 355

Answers (2)

bantmen
bantmen

Reputation: 768

In general, you can use np.isclose to compare float values.

Upvotes: 1

Mike
Mike

Reputation: 80

You can use a round() function

a= np.array([1,2,3])
b= np.array([2,3,4])

print((np.linalg.norm(round(a-b))**2))

Upvotes: 0

Related Questions