Skip to content

Support gmpy2-like rounding modes in to_str()#830

Merged
skirpichev merged 32 commits intompmath:masterfrom
javierelpianista:tying_support
Aug 26, 2024
Merged

Support gmpy2-like rounding modes in to_str()#830
skirpichev merged 32 commits intompmath:masterfrom
javierelpianista:tying_support

Conversation

@javierelpianista
Copy link
Contributor

@javierelpianista javierelpianista commented Aug 2, 2024

Closes #757.

* 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.
@skirpichev skirpichev self-requested a review August 3, 2024 04:06
Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@javierelpianista
Copy link
Contributor Author

javierelpianista commented Aug 3, 2024

@skirpichev The DeprecationWarning makes most test cases fail.

To fix this I should modify all calls to to_str to include correct rounding.
This includes the __str__ and __repr__ methods for each type defined in mpmath.
I've made some of these modifications for mpf and mpc types to take the rounding from context. Would you please tell me if you think this is the correct way to go forward?

@skirpichev skirpichev self-requested a review August 4, 2024 02:50
Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

@skirpichev
Copy link
Collaborator

Now this has merge conflicts.

Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, lest just switch to round_nearest per default

@javierelpianista
Copy link
Contributor Author

yeah, lest just switch to round_nearest per default

OK, so if I understand correctly, I should modify all calls to to_str in the __str__ and __repr__ methods to use round_nearest, correct? Otherwise we would get deprecation warnings for simply printing a float, which is something regular users would not like (myself included)

@skirpichev
Copy link
Collaborator

OK, so if I understand correctly, I should modify all calls to to_str in the str and repr methods to use round_nearest, correct?

No. Lets just change default in to_str() to round_nearest.

Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again. Please just drop legacy code (rounding=None). Don't test rounding=None.

@javierelpianista
Copy link
Contributor Author

@skirpichev I think we should assert inside to_str that rounding is one of the correct modes.

Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@skirpichev
Copy link
Collaborator

skirpichev commented Aug 25, 2024

Hmm, it seems some test(s?) loop forever with your recent changes. Edit: ah, it's new test.

@skirpichev
Copy link
Collaborator

I don't think that just removing "bad test" is ok. Why it's failing?

@javierelpianista
Copy link
Contributor Author

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.

@skirpichev
Copy link
Collaborator

I can reproduce this failure locally with python backend (MPMATH_NOGMPY=1). Maybe it's a normalize() bug.

Copy link
Collaborator

@skirpichev skirpichev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, but please update pr title/description

@skirpichev skirpichev changed the title Tying support Support gmpy2-like rounding modes in to_str() Aug 26, 2024
@skirpichev skirpichev merged commit 7afe9ac into mpmath:master Aug 26, 2024
@skirpichev
Copy link
Collaborator

Thanks.

@javierelpianista javierelpianista deleted the tying_support branch August 26, 2024 11:27
@skirpichev skirpichev added this to the 1.4 milestone May 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement directed rounding in to_str()

2 participants