0

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?

ali_m
  • 71,714
  • 23
  • 223
  • 298
newdev14
  • 1,091
  • 4
  • 15
  • 25

1 Answers1

1

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

Oliver Dain
  • 9,617
  • 3
  • 35
  • 48