2

In Python I did 3.08 - 4.31 and got back -1.2299999999999995.

This is definitely not right (-1.23 is right) and highly concerning. I am not seeing this behavior in other languages, any insights into what Python is doing here?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
David Crook
  • 2,722
  • 3
  • 23
  • 49
  • 1
    Any language that uses floating point will be prone to this same problem. Whether you're actually noticing it or not could be due to a number of factors, but you are guaranteed the problem exists and will be visible under certain circumstances. – Tom Karzes Jun 25 '16 at 08:32
  • You may be interested in the `decimal` module... – Bakuriu Jun 25 '16 at 09:09

1 Answers1

1

You can find a great explanation here: https://docs.python.org/2/tutorial/floatingpoint.html

To fix this use round(3.08 - 4.31, 2)

Ami Hollander
  • 2,435
  • 3
  • 29
  • 47