Skip to content

Fix nearby_rational max_error coercion #40280

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

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

user202729
Copy link
Contributor

@user202729 user202729 commented Jun 21, 2025

Previously the coercion model makes it so that self - max_error is coerced to the common parent (minimum precision) of self and max_error, which is often undesirable. See the newly-added test in nearby_rational

            sage: x = RealField(500)(pi)
            sage: abs(x.nearby_rational(max_error=1e-100) - x) <= 1e-100
            True

(previously it returns False)

Also implement add_error, mirroring the method with same name in RBF. See also #39846

I decide to delete one test (originally added in #6616 ) because: the value RR(2.1) is exactly represented internally by

sage: int(2.1*2^100)/2^100
4728779608739021/2251799813685248

and as shown later, 4728779608739021/2251799813685248 - 21/10 is in fact less than 10e-17, so the original result is incorrect, just as an artifact of how nearby_rational were implemented.

This new implementation, however, might make it such that the returned value is not within the interval. However this is not particularly easy to handle (we may use X extra bits of precision in the interval, but we will not know which X is sufficient), and it may not be what the user want anyway (the original input might its contain error of the order of Θ(1) ulp)

📝 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

Copy link

github-actions bot commented Jun 21, 2025

Documentation preview for this PR (built with commit b6a758b; 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.

1 participant