New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
round Decimal error #83305
Comments
When I round this: round(Decimal('9.925'), 2), in Python 3.7.5 the result is Decimal('9.92'), but in Python 2.7.17 is 9.93 |
In Python 3, the rounding mode is round-ties-to-even. (In Python 2, it's round-ties-to-away.) |
More details on the change here: https://docs.python.org/3/whatsnew/3.0.html#builtins and in the library documentation: |
Thanks for the quick response. I do not understande why behavior of float is diferrent of the Decimal. In python 2.7 I have the same result: 9.93 |
9.925 is 9.925000000000000710542735760100185871124267578125 on my platform. |
@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. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: