Merged
Conversation
4853eec to
5f250e5
Compare
Probably this will look non-intuitive, but underlying floating-point
arithmetic is binary, not decimal. Thus, round(x, n) sometimes will pick
up a decimal representation with more than n digits. This is something
common for CPython < 2.7 and < 3.1:
Python 2.6.6 (r266:84292, Aug 12 2014, 07:57:07)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> round(0.42754287856598971, 2)
0.42999999999999999
And with this patch as well:
Python 3.12.5+ (heads/3.12:0181aa2e3e, Aug 29 2024, 14:55:08) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from mpmath import *
>>> round(mp.mpf(0.42754287856598971), 2)
mpf('0.42999999999999999')
>>> mp.pretty = True
>>> round(mp.mpf(0.42754287856598971), 2)
0.43
See also python/cpython#45921
Closes mpmath#455
5f250e5 to
a545ecb
Compare
Collaborator
Author
|
I'm going to merge this after a week. But I would appreciate @cbm755 feedback, as new feature might look surprising. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Probably this will look non-intuitive, but underlying floating-point arithmetic is binary, not decimal. Thus, round(x, n) sometimes will pick up a decimal representation with more than n digits. This is something common for CPython < 2.7 and < 3.1:
And with this patch as well:
See also python/cpython#45921
Closes #455