Skip to content
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

Closed
adelsonllima mannequin opened this issue Dec 23, 2019 · 6 comments
Closed

round Decimal error #83305

adelsonllima mannequin opened this issue Dec 23, 2019 · 6 comments
Labels
3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error

Comments

@adelsonllima
Copy link
Mannequin

adelsonllima mannequin commented Dec 23, 2019

BPO 39124
Nosy @mdickinson, @joernheissler, @adelsonllima

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:

assignee = None
closed_at = <Date 2019-12-23.15:39:52.497>
created_at = <Date 2019-12-23.14:34:58.570>
labels = ['invalid', 'type-bug', '3.7']
title = 'round Decimal error'
updated_at = <Date 2019-12-23.15:39:52.496>
user = 'https://github.com/adelsonllima'

bugs.python.org fields:

activity = <Date 2019-12-23.15:39:52.496>
actor = 'mark.dickinson'
assignee = 'none'
closed = True
closed_date = <Date 2019-12-23.15:39:52.497>
closer = 'mark.dickinson'
components = []
creation = <Date 2019-12-23.14:34:58.570>
creator = 'adelsonllima'
dependencies = []
files = []
hgrepos = []
issue_num = 39124
keywords = []
message_count = 6.0
messages = ['358814', '358815', '358816', '358820', '358821', '358822']
nosy_count = 3.0
nosy_names = ['mark.dickinson', 'joernheissler', 'adelsonllima']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39124'
versions = ['Python 3.7']

@adelsonllima
Copy link
Mannequin Author

adelsonllima mannequin commented Dec 23, 2019

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

@adelsonllima adelsonllima mannequin added type-crash A hard crash of the interpreter, possibly with a core dump 3.7 (EOL) end of life labels Dec 23, 2019
@mdickinson
Copy link
Member

In Python 3, the rounding mode is round-ties-to-even. (In Python 2, it's round-ties-to-away.)

@mdickinson
Copy link
Member

More details on the change here: https://docs.python.org/3/whatsnew/3.0.html#builtins

and in the library documentation:
https://docs.python.org/3/library/functions.html#round

@adelsonllima
Copy link
Mannequin Author

adelsonllima mannequin commented Dec 23, 2019

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

@adelsonllima adelsonllima mannequin reopened this Dec 23, 2019
@adelsonllima adelsonllima mannequin reopened this Dec 23, 2019
@joernheissler
Copy link
Mannequin

joernheissler mannequin commented Dec 23, 2019

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).

@mdickinson
Copy link
Member

@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.

@mdickinson mdickinson added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Dec 23, 2019
@mdickinson mdickinson added type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Dec 23, 2019
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant