0

Why does round in the following code:

>>> round(0.49999999999999997)
0.0
>>> round(0.49999999999999998)
1.0
>>> round(1.4999999999999998)
1.0
>>> round(1.4999999999999999)
2.0

round 0.49999999999999998 and 1.4999999999999999 up instead of down, and why does this behavior happen for 0.49999999999999998 by one 9 after the decimal later than for 1.4999999999999998?

Niek de Klein
  • 8,524
  • 20
  • 72
  • 143
  • 1
    Regarding the additional question of why it happens "one 9 later" for 1.4999...: floating-point numbers have a fixed number of *significant* digits. You can think of the integral part "1." already consuming one significant digit, so there's one less digit left for the fractional part. – Sven Marnach Oct 01 '13 at 12:23
  • Use `decimal.Decimal` for infinite precision floating point numbers. – Erik Kaplun Oct 01 '13 at 12:27

0 Answers0