Skip to content

Conversation

@cxzhong
Copy link
Contributor

@cxzhong cxzhong commented Dec 6, 2025

Update round method to use round-to-even for tie cases, aligning with Python 3 behavior and IEEE 754 default rounding.
Fix #41056

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies

Update round method to use round-to-even for tie cases, aligning with Python 3 behavior and IEEE 754 default rounding.
@cxzhong
Copy link
Contributor Author

cxzhong commented Dec 6, 2025

Before that, such interesting thing raise:

sage: (5/2).round()
2
sage: (-5/2).round()
-2
sage: (2.5).round()
3
sage: (-2.5).round()
-3

I think it is definitely a bug we need to fix

@cxzhong
Copy link
Contributor Author

cxzhong commented Dec 6, 2025

Seems some doctests affects

Update rounding behavior to match Python 3 and IEEE 754.
Copy link
Member

@dimpase dimpase left a comment

Choose a reason for hiding this comment

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

this is a major change, potentially breaking other people's code.

I'd rather have an interface to change the rounding mode, say during the creation of the field.

If we want to make this rounding default, it must come via the usual depreciation route.

@cxzhong
Copy link
Contributor Author

cxzhong commented Dec 6, 2025

this is a major change, potentially breaking other people's code.

I'd rather have an interface to change the rounding mode, say during the creation of the field.

If we want to make this rounding default, it must come via the usual depreciation route.

Yes. I know, we may merge it in 10.9 and we can take a deprecation notice?

@cxzhong
Copy link
Contributor Author

cxzhong commented Dec 6, 2025

this is a major change, potentially breaking other people's code.

I'd rather have an interface to change the rounding mode, say during the creation of the field.

If we want to make this rounding default, it must come via the usual depreciation route.

And we can create a interface to let people to choose the round way they need.

@cxzhong cxzhong requested review from nbruin and tscrim December 6, 2025 16:20
@nbruin
Copy link
Contributor

nbruin commented Dec 7, 2025

I recommend reading the documentation of RIF().round. It explains this routine is probably not what you want. Perhaps you mean RIF(2.5,3.5).unique_round(). That routine fails and if you read the docs you see why: It tries to apply round to each element in the interval and only returns a value if all of those agree. Similar semantics apply to round. Remember that interval arithmetic as implemented in MPFI tries to return values that contain all answers that can arise from numbers in the input intervals. So "banker's rounding" on RIF(2.5,3.5) should return RIF(2,4) . The current code ends up returning RIF(3,4) because it rounds up/away from zero

In practice, the edge cases for interval bounds are never significant. That's with the FP rounding internally is down for lower and up for upper bounds. You can do RIF(2.5,3.5).floor() or RIF(2.5,3.5).trunc() for other results. But note that you're getting an interval back.

@github-actions
Copy link

github-actions bot commented Dec 7, 2025

Documentation preview for this PR (built with commit ca533b0; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test failure due to rounding issues

3 participants