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

Mats Wichmann mats at wichmann.us
Fri Nov 19 14:52:12 EST 2021


On 11/18/21 19:40, 2QdxY4RzWzUUiLuE at potatochowder.com wrote:
> On 2021-11-18 at 23:16:32 -0300,
> René Silva Valdés <rene.silva.valdes at gmail.com> wrote:
> 
>> 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 ...
> 
> It's not int() that's doing the rounding; that second numerator is being
> rounded before being divided by 12:
> 
>      Python 3.9.7 (default, Oct 10 2021, 15:13:22)
>      [GCC 11.1.0] on linux
>      Type "help", "copyright", "credits" or "license" for more information.
>      >>> 23.999999999999999
>      24.0
>      >>> (23.999999999999999).hex()
>      '0x1.8000000000000p+4'
> 

The documentation has a fair bit to say on the subject of floating 
point.  I never remember the precise link, but fortunately it's pretty 
easy to search for:

https://docs.python.org/3/tutorial/floatingpoint.html



More information about the Python-list mailing list