newdev14
newdev14

Reputation: 1111

numpy float subtraction precision problems

I have a numpy.float64 object, lets call it A of value '1.0' that is generated from a program that I am using, and I checked its type

And I create object B using numpy.float64(1.0)

when I subtract the two, I strangely get this value: -2.22044604925e-16 when I actually expect a value of 0

Now when I subtract A from A I get 0

I am not sure what is going on and why I don't get a value of 0, any pointers?

Upvotes: 0

Views: 762

Answers (1)

Oliver Dain
Oliver Dain

Reputation: 9953

This is most likely due to floating point numbers having finite precision. The short, not quite true but close enough to give you the idea answer, is that A is a number more like 1.00000000000001 than 1.0 but it prints as 1.0 because things round when you print them.

A good, completely true and not just kind-of-true like the above, reference is here: https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

Upvotes: 1

Related Questions