[issue39059] Getting incorrect results in rounding procedures

Terry J. Reedy report at bugs.python.org
Mon Dec 16 13:46:59 EST 2019


Terry J. Reedy <tjreedy at udel.edu> added the comment:

The flaw in my_round is that it rounds twice, not once.  The first rounding is caused by multiplying by a factor that is not a power of 2.  

In the case of 2.675, that rounding is up enough to affect the second rounding.
>>> format(2.675, ".17f")
'2.67499999999999982'
>>> format(2.675 *100, ".17f")
'267.50000000000000000'

In the case of 4.395, the first rounding is down.
>>> format(4.395, ".17f")
'4.39499999999999957'
>>> format(4.395 *100, ".17f")
'439.49999999999994316'
Even if it had been up, it might not have been enough to affect the outcome, as 57 is a lot farther from 100 than 82.

If you want to discuss floating point approximations and rounding further, please post to python-list, not here.

----------
components:  -Documentation, FreeBSD, IDLE, Library (Lib), Tests, Windows, macOS
versions:  -Python 3.6

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


More information about the Python-bugs-list mailing list