0

I am trying to calculate the floor value of certain numbers.

It works fine when I run this: math.floor(9.999999999999999) .
I get the output as 9 . This number has 15 decimal places.

However, math.floor() seems to fail after 15 decimal places.

When I run this: math.floor(9.9999999999999999) , I get the output as 10 . This number has 16 decimal places.

I want to understand why math.floor() is rounding up the number after 15 decimal places.

NOTE :

Somehow, adding 1 after the 15th decimal place also works correctly.

Input : math.floor(9.9999999999999991111)

Output : 9

However, any other number besides 1 , and the number gets rounded up.

Input : math.floor(9.9999999999999992)

Output : 10

I would like to understand the reason for this unusual behavior in python.

  • To add on to mousetail's linked question, you can try to enter the values you listed as-is into a terminal. You will see that the return from the terminal is different from the input value. – Shorn Jun 01 '23 at 06:58
  • 2
    Forget about `floor`, the point is that floating point numbers don't have enough precision for more than ~17 decimal digits. If you print `9.9999999999999999` you'll discover that it's already 10.0 (and `9.9999999999999999 == 10.0` evaluates to `true`), as it's "snapped" to the nearest representable number on parsing. – Matteo Italia Jun 01 '23 at 06:59

0 Answers0