-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Fix rationalize(x) accuracy with tiny floats / when x is a rational
#60828
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
base: master
Are you sure you want to change the base?
Conversation
- Fix `tol` ignored when `T` is not specified. - Specific implementation for rationals (vs floats): return `x` unless the given `tol` is significant enough to reduce the magnitude of its components, in which case rationalize `float(x)` instead (see JuliaLang#60768).
Convert `x` to a float type of higher precision when needed, so that `a, r = divrem(x,y)` remain exact in the while loop (this adds type instability and breaks inference so the function return type needs to be specified).
| if p > precision(Float64) | ||
| return setprecision(() -> rationalize(T, BigFloat(x), tol), BigFloat, p) | ||
| end | ||
| x, a, r = Float64.((x, a, r)) | ||
| end |
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.
This will break type stability.
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 conversion depends on the value of x, so yes. I made the function return type explicit to preserve inference.
I don't think there is a way to fix the inaccuracy issues without giving x more precision.
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.
Maybe we should use a wrapper function that does the check and conversion if needed, so that the current (inner) function remains type stable ?
Co-authored-by: Oscar Smith <oscardssmith@gmail.com>
Co-authored-by: Oscar Smith <oscardssmith@gmail.com>
Co-authored-by: Oscar Smith <oscardssmith@gmail.com>
Fixes #60768, fixes #49803, fixes #49848.
I made it in a single PR because there isn't much code involved and because one fix needs the other. More details in the commit messages and issue descriptions.