Skip to content

Conversation

@lvlte
Copy link

@lvlte lvlte commented Jan 26, 2026

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.

lvlte added 3 commits January 26, 2026 17:27
- 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).
Comment on lines +256 to +260
if p > precision(Float64)
return setprecision(() -> rationalize(T, BigFloat(x), tol), BigFloat, p)
end
x, a, r = Float64.((x, a, r))
end
Copy link
Member

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.

Copy link
Author

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.

Copy link
Author

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 ?

lvlte and others added 2 commits January 26, 2026 18:16
Co-authored-by: Oscar Smith <oscardssmith@gmail.com>
Co-authored-by: Oscar Smith <oscardssmith@gmail.com>
@nsajko nsajko added rationals The Rational type and values thereof maths Mathematical functions labels Jan 26, 2026
@adienes adienes added the bugfix This change fixes an existing bug label Jan 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix This change fixes an existing bug maths Mathematical functions rationals The Rational type and values thereof

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rationalize(x::Rational) inconsistencies rationalize incorrect for Float16 input rationalize mishandles some subnormals

5 participants