0

I am having a problem with Python's precision when subtracting. For example if I wanted python to do 0.1234-.01 it would sometimes return 0.1134000000005.

Why does this happen and how can I stop it?

martineau
  • 119,623
  • 25
  • 170
  • 301
TheGarry243
  • 63
  • 1
  • 7
  • 1
    That's how floating point numbers work. It's usually solved by formatting the number when you print it out, or using `decimal` instead of float – Garr Godfrey May 04 '20 at 23:58
  • https://docs.python.org/3.8/library/decimal.html – Garr Godfrey May 04 '20 at 23:59
  • The *precision* is always the same, `float` objects are (depending on the hardware of your system) fundamentally wrapping a primitive 64bit floating point number. As such, the usual caveats to fixed-width floating point numbers apply – juanpa.arrivillaga May 05 '20 at 00:00

0 Answers0