Unexpected behaviour of math.floor, round and int functions (rounding)

René Silva Valdés rene.silva.valdes at gmail.com
Thu Nov 18 21:16:32 EST 2021


Hello, I would like to report the following issue:

Working with floats i noticed that:

int(23.99999999999999/12) returns 1, and
int(23.999999999999999/12) returns 2

This implies that int() function is rounding, which doesn't appear to be
expected (documentation doesn't say anything about it). Looking further i
noticed that

0.5+0.49999999999999994 returns 1. This seems to be related to double
numbers' operations in C language, where 0.49999999999999994 is the
greatest floating-point value less than 0.5. Counting on this several
examples can be deduced, like:

round(0+0.49999999999999994) returns 0, and
round(1+0.49999999999999994) returns 2

This seems to be a known issue in Java (see reference)


Reference:

https://bugs.java.com/bugdatabase/view_bug.do?bug_id=6430675

I hope this information is helpful, thanks in advance for reading this,

Kind regards,

René


More information about the Python-list mailing list