-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
modifications for round #16608
modifications for round #16608
Conversation
✅ Hi, I am the SymPy bot (v147). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.5. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
Update The release notes on the wiki have been updated. |
@asmeurer, do you want this in 1.4 or should it wait? |
It's too late for 1.4 unless it is an absolutely critical bug fix. I plan to do the final release tomorrow or Wednesday. |
Nope -- just creating a compatibilty version of round and changing default rounding semantics |
Some of the doctests are failing |
Thanks -- updated accordingly:
|
It would be good to have another set of eyes check the changes.
|
Some tests that |
5841b0b
to
28c4dac
Compare
514afbb
to
4c99f7a
Compare
I'm not sure what to do about rounding Integers. What should the following give and why?
|
Shouldn't round(123,0) be 123, an int |
See OP for how SymPy and Python differ in rounding algorithms. @asmeurer , comparisons of types are now tested. |
In Python 3, yes. This is now the result given by SymPy in this branch. |
Codecov Report
@@ Coverage Diff @@
## master #16608 +/- ##
==============================================
+ Coverage 11.001% 73.753% +62.751%
==============================================
Files 619 619
Lines 158846 158982 +136
Branches 37254 37292 +38
==============================================
+ Hits 17476 117254 +99778
+ Misses 140684 36301 -104383
- Partials 686 5427 +4741 |
|
||
assert (Float(.3, 3) + 2*pi).round() == 7 | ||
assert (Float(.3, 3) + 2*pi*100).round() == 629 | ||
assert (Float(.03, 3) + 2*pi/100).round(5) == 0.09283 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the output is limited by the precision of 3
5c315d0
to
35bfdd5
Compare
1dfc48a
to
7a00309
Compare
- don't recast arg to float - Integer in Integer out - single arg call (n=None) gives Integer out
I feel like the test that were in place and those that I have added are a good test of this algorithm. I am also happy that we have round-to-even rounding working for Floats. The simplified code here belies the difficulty of getting this right. If anyone else wants to give this a check that would be nice, otherwise I will commit this post-Easter. |
Codecov Report
@@ Coverage Diff @@
## master #16608 +/- ##
=============================================
- Coverage 73.841% 73.826% -0.015%
=============================================
Files 619 619
Lines 159332 159354 +22
Branches 37390 37396 +6
=============================================
- Hits 117653 117646 -7
- Misses 36249 36275 +26
- Partials 5430 5433 +3 |
+1 Should a new issue be opened with the dropping Python 2 label noting to follow up on this later? |
cf #16686 |
I'd like to (eventually) see Float rounding done by mpmath (mpmath/mpmath#455) Then we could worry separately about how Rationals, irrational Numbers, etc are dealt with, without the code for Floats being interspersed. But that could wait until Python 2 is dropped, so I'm ok in principle with some of these changes. But the round-to-even bit is suspicious for Floats. I don't believe the "augment precision, add 5 to previously least significant digit" logic. Here is a float, converted to a Float, that is less than 575/1000 but none-the-less rounds (incorrectly) to 0.58:
|
@cbm77, please see inline notes to the algorithm which is now recast more explicitly in terms of known digits as an integer and the rounding digit in the 10ths place. |
References to other Issues or PRs
closes #12056
Brief description of what is fixed or changed
The round method has been modified to round to even by default
in keeping with Python 3 semantics
The round method no longer casts args to float so precision
is no longer lost.
To get Python 3 behavior in Python 2, import round from sympy.core.compatibility
Other comments
Python and SymPy use different methods of rounding so rounding to the right of the decimal is WYSIWIG in SymPy but not in Python:
This is because Python uses the full binary representation while SymPy rounds an integer based on the precision of the number, e.g. if a number
n
is stored as 0.3499999 with 2 digits of precision, SymPy would use 35 for the rounding since this is the closest integer ton*10**2
.cf discussion here
Release Notes