Support gmpy2-like rounding modes in to_str()#830
Conversation
* Added new rounding mode: round_nearest_even: round to nearest tying to even. * Modified round_digits to accomodate for this new mode.
* Wrote abs_rounding function. * Replaced part of the code in to_str with a call to round_digits. * Added tests for different kinds of rounding in test_convert.py
* Add new symbol, 'M', for rounding to nearest with tying away from zero. * Replaced part of the code in format_mpf with a call to abs_rounding * Added tests for the M rounding mode.
* Add new symbol, 'Y', for rounding away from zero. * Added tests for the Y rounding mode.
There was a problem hiding this comment.
Thus,
please add 'Y' support in a separate pr; done: see #831- please don't introduce new rounding mode
- rounding=None in to_str() will be current (legacy) behaviour, that should be deprecated
- deprecation message should announce that new default will be round_nearest
* The default for the rounding parameter is None. * If rounding is None, a DeprecationWarning is raised; the rounding part of the code executes legacy code * Else, the new round_digits function is called
* Made all the dunder methods that call to_str to include the rounding parameter obtained from context. This is to avoid the new deprecation warning in to_str.
Modified the call to to_str in mpc_to_str to include the rounding parameter taken from context.
|
@skirpichev The DeprecationWarning makes most test cases fail. To fix this I should modify all calls to |
skirpichev
left a comment
There was a problem hiding this comment.
The DeprecationWarning makes most test cases fail.
It's easy to filter out this warning. My second commit shows how to do this.
To fix this I should modify all calls to to_str to include correct rounding.
Huh, tough decision. It seems that we in any case will introduce a compatibility break. Or - at least - a DeprecationWarning, which might surprise user, as it will come from the mpmath code, that calls to_str().
On another hand, removing legacy code and setting new defaults - leave us with just one test failure in test_str.py. See #832. I'll run diofant's test suite on top of that pr, see diofant/diofant#1430.
I'll appreciate @oscarbenjamin feedback on this.
This includes the str and repr methods for each type defined in mpmath.
Hmm, maybe we won't such flexibility? E.g. gmpy2 uses rounding to nearest, regardless of the context settings. (But see gmpy2/gmpy2#503)
|
Now this has merge conflicts. |
skirpichev
left a comment
There was a problem hiding this comment.
This now lacks tests (don't forgot to test the warning too).
But I'm not sure if we should pursue this approach. #832 seems to be working for the Diofant (not test failures). I would appreciate testing on SymPy, however.
Lets wait few days for feedback from SymPy people. If no objections follows - I think we should just switch to round_nearest.
skirpichev
left a comment
There was a problem hiding this comment.
yeah, lest just switch to round_nearest per default
OK, so if I understand correctly, I should modify all calls to |
No. Lets just change default in to_str() to round_nearest. |
skirpichev
left a comment
There was a problem hiding this comment.
Again. Please just drop legacy code (rounding=None). Don't test rounding=None.
|
@skirpichev I think we should assert inside |
skirpichev
left a comment
There was a problem hiding this comment.
we should assert inside to_str that rounding is one of the correct modes.
No. This is a public function and we should raise ValueError.
Please add basic tests for new option.
|
Hmm, it seems some test(s?) loop forever with your recent changes. Edit: ah, it's new test. |
|
I don't think that just removing "bad test" is ok. Why it's failing? |
The test I removed aimed to cover line 1237 of libmpf.py. It wasn't relevant for this PR so I removed it without much thought. |
|
I can reproduce this failure locally with python backend (MPMATH_NOGMPY=1). Maybe it's a normalize() bug. |
This reverts commit 38f6e7b.
skirpichev
left a comment
There was a problem hiding this comment.
LGTM, but please update pr title/description
|
Thanks. |
Closes #757.