[issue32543] odd floor division behavior

Serhiy Storchaka report at bugs.python.org
Fri Jan 12 19:07:48 EST 2018


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

This is the result of multiple roundings.

0.9 and 0.1 can't be represented exactly as floats. They are approximated by binary fractions.

>>> from fractions import Fraction
>>> Fraction(0.9)
Fraction(8106479329266893, 9007199254740992)
>>> Fraction(0.1)
Fraction(3602879701896397, 36028797018963968)

The result of the division of these fractions can't be represented as a float too.

>>> Fraction(0.9)/Fraction(0.1)
Fraction(32425917317067572, 3602879701896397)
>>> Fraction(0.9)/Fraction(0.1) - 9
Fraction(-1, 3602879701896397)
>>> float(Fraction(0.9)/Fraction(0.1) - 9)
-2.7755575615628914e-16
>>> 9 + float(Fraction(0.9)/Fraction(0.1) - 9)
9.0

It is slightly smaller than 9, but the nearest float value is 9.0. Thus the result of the division is rounded up to 9.0.

A similar issue already was opened several months ago. I don't remember the number.

----------
nosy: +serhiy.storchaka
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32543>
_______________________________________


More information about the Python-bugs-list mailing list