Skip to content

Comments

Fix rationalize([T,] x::Rational; ...)#61057

Open
lvlte wants to merge 4 commits intoJuliaLang:masterfrom
lvlte:rationalize_rational_fix
Open

Fix rationalize([T,] x::Rational; ...)#61057
lvlte wants to merge 4 commits intoJuliaLang:masterfrom
lvlte:rationalize_rational_fix

Conversation

@lvlte
Copy link
Contributor

@lvlte lvlte commented Feb 17, 2026

Fixes #60768

  • 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.
  • Reset the main function specific to floats only: if rational, the error and tolerance terms (their components) are very likely to overflow as they converge towards zero. Other fixes specific to floats will be easier to implement.

- 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.
- Reset the main function specific to floats only: if rational, the
  error and tolerance terms (their components) are very likely to
  overflow as they converge towards zero. Other fixes specific to
  floats will be easier to implement.
base/rational.jl Outdated
Comment on lines 260 to 258
a = trunc(x)
r = x-a
y = one(x)
r, a = modf(x)
Copy link
Member

Choose a reason for hiding this comment

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

what's a case where the difference here matters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't matter. I just applied those changes without the fix for tiny x. One line vs two I guess. It wasn't a problem until now.

Copy link
Member

Choose a reason for hiding this comment

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

makes sense. Looks like there's no perf difference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought "more concise" but I understand modf does some checks we don't need at this point and in absolute terms it would be better to revert that change.

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.

rationalize(x::Rational) inconsistencies

3 participants