[issue39124] round Decimal error

Mark Dickinson report at bugs.python.org
Mon Dec 23 10:39:44 EST 2019


Mark Dickinson <dickinsm at gmail.com> added the comment:

@adelsonllima: Take a look at the documentation that I linked to for the round function, and in particular this note:

"The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information."

As Jörn Heissler observed, the issue here is that the numeric literal 9.925 in the source gets turned into a float whose exact value is just slightly larger than 9.925 (because 9.925 can't be exactly represented in the IEEE 754 binary64 floating-point format that your machine is almost certainly using). So because it's a touch larger than 9.925, it rounds up when rounding to two decimal places.

Closing again here: believe it or not, round is working as designed here.

----------

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


More information about the Python-bugs-list mailing list