Skip to content
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

RationalNode numerator & denominator fields to make it directly usable #2521

Closed
eregon opened this issue Feb 28, 2024 · 5 comments
Closed

RationalNode numerator & denominator fields to make it directly usable #2521

eregon opened this issue Feb 28, 2024 · 5 comments
Labels
enhancement New feature or request
Milestone

Comments

@eregon
Copy link
Member

eregon commented Feb 28, 2024

Rational literals are either:

  • 123r
  • 12.34r

(12ri/12.34ri is just ImaginaryNode of RationalNode, so a trivial variant)

These are actually quite different, the first is the same as Rational(123, 1) but the second is Rational(1234, 100), not Rational(12.34):

irb(main):001:0> 12.34r
=> (617/50)
irb(main):002:0> Rational(1234, 100)
=> (617/50)
irb(main):003:0> Rational(12.34)
=> (3473401212609495/281474976710656)

So right now to handle this we have to manually parse the number, e.g. TruffleRuby does it like this:
https://github.com/oracle/truffleruby/blob/5096dddf6c3adc7edbae0938efc4fb4913b62bba/src/main/java/org/truffleruby/parser/YARPTranslator.java#L2905-L2927

It would be great if RationalNode was changed to have a numerator and denominator field, then we wouldn't need to manually reparse the number.
It could also potentially save an unnecessary conversion from string to double, since the double value in this case should not be used anyway.

code before after
123r RationalNode(numeric=IntegerNode(123)) RationalNode(numerator=IntegerNode(123), denominator=IntegerNode(1) (or nil))
12.34r RationalNode(numeric=FloatNode(12.34)) RationalNode(numerator=IntegerNode(1234), denominator=IntegerNode(100))

cc @andrykonchin

@eregon eregon changed the title Parse RationalNode value to make it directly usable RationalNode numerator & denominator fields to make it directly usable Feb 28, 2024
@kddnewton kddnewton added the enhancement New feature or request label Feb 28, 2024
@kddnewton kddnewton added this to the Long-term milestone Feb 28, 2024
@eregon
Copy link
Member Author

eregon commented May 24, 2024

@kddnewton Should this be addressed for 1.0, since it will likely be a breaking change for consumers?
Although maybe it's feasible to define a numeric method for compatibility.

@kddnewton
Copy link
Collaborator

Yeah this is already out! There's a numeric method for backwards compat as well.

@kddnewton
Copy link
Collaborator

Oh this can be closed now.

@eregon
Copy link
Member Author

eregon commented May 24, 2024

Sweet!
@andrykonchin Could you adopt this in TruffleRuby? It should simplify a bunch of logic.
It also looks (from the docs) that Prism does the canonicalization, so we could skip that when creating the Rational from the literal.

@kddnewton
Copy link
Collaborator

It only does the reduction if it fits into 32-bits unfortunately. I haven't implemented the bignum stuff yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants