Title: round Decimal error
Type: behavior Stage: resolved
Components: Versions: Python 3.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: adelsonllima, joernheissler, mark.dickinson
Priority: normal Keywords:

Created on 2019-12-23 14:34 by adelsonllima, last changed 2019-12-23 15:39 by mark.dickinson. This issue is now closed.

Messages (6)
msg358814 - (view) Author: Adelson Luiz de Lima (adelsonllima) Date: 2019-12-23 14:34
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
msg358815 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-23 14:37
In Python 3, the rounding mode is round-ties-to-even. (In Python 2, it's round-ties-to-away.)
msg358816 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-23 14:39
More details on the change here:

and in the library documentation:
msg358820 - (view) Author: Adelson Luiz de Lima (adelsonllima) Date: 2019-12-23 15:20
Thanks for the quick response. 
I try the follow code in python 3.7: 
round(9.925, 2) => 9.93
round(Decimal('9.925'), 2) => Decimal('9.92')

I do not understande why behavior of float is diferrent of the Decimal.

In python 2.7 I have the same result: 9.93
msg358821 - (view) Author: Jörn Heissler (joernheissler) * Date: 2019-12-23 15:31
> round(9.925, 2) => 9.93

9.925 is 9.925000000000000710542735760100185871124267578125 on my platform.
This is larger than 9.925, so the "round-ties-to-even" rule can't be applied. Instead it is rounded up to 9.93 (9.92999999999999971578290569595992565155029296875).
msg358822 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2019-12-23 15:39
@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.
Date User Action Args
2019-12-23 15:39:52mark.dickinsonsetstatus: open -> closed
type: crash -> behavior
2019-12-23 15:39:44mark.dickinsonsetmessages: + msg358822
2019-12-23 15:31:54joernheisslersetnosy: + joernheissler
messages: + msg358821
2019-12-23 15:20:12adelsonllimasetstatus: closed -> open

messages: + msg358820
2019-12-23 14:39:16mark.dickinsonsetmessages: + msg358816
2019-12-23 14:37:38mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg358815

resolution: not a bug
stage: resolved
2019-12-23 14:34:58adelsonllimacreate