0

It seems, from what I can tell, that python 3.10.4 math.floor() will sometimes round up instead of down. This seems to be in contrast to the purpose of the function.

Could someone please explain this?

Example:

>>> math.floor(0.9999999999999999)
0
>>> math.floor(0.99999999999999999)
1
wovano
  • 4,543
  • 5
  • 22
  • 49
jetole
  • 666
  • 5
  • 10

1 Answers1

2

floor() has nothing to do with this:

>>> 0.9999999999999999
0.9999999999999999
>>> 0.99999999999999999
1.0

That is, your second literal rounds up to 1.0 all on its own before math.floor() happens. Thus, math.floor() is flooring the number 1, not 0.99999999999999999. Floating-point literals are automatically converted to internal machine binary floating-point format, which has only 53 bits of precision ("IEEE 754 double" format on almost all machines).

Tim Peters
  • 67,464
  • 13
  • 126
  • 132