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
Introducing $*RAT-UPGRADE-CLASS #4299
Conversation
See also: #3122 and Raku/problem-solving#185 |
Re Jonathan's original comment: #3122 (comment) some updated clarifications.
I think @jnthn and I have a fundamental difference of opinion: language architect vs module developer :-) Either you could consider it affecting the behaviour of all operators that can potentially generate a Making it a lexical property, would disallow affecting this externally for any math libraries: if a math library wants to make sure it will never lose precision, it should use
I've confirmed that this approach does not affect inlineability of the
gets completely inlined (except for the call to Furthermore, if an upgrade to |
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.
As long as performance cost is only applied when the actual up/downgrade takes place, I like it.
A lexical implementation as I see it (via a scalar local to Rat
) may cause more troubles than it worth performance-wise.
@vrurg yes, only when actual downgrade takes place. The only real change is:
to:
The rest of the PR is setting up the |
I believe $*RAT-UPGRADE-DELEGATE would be more descriptive. Would also accepting (and calling) a |
@gfldex The current implementation will take anything on which an As to the name: perhaps |
POLICY is much better then CLASS. A class doesn't really exist at runtime. It's just a declarator for type objects. Since methods can be build and added to objects at runtime, CALL-ME is dynamic enough to get the same effect then a Callable. |
This dynamic variable indicates the *class* in which an UPGRADE-RAT method will be called whenever a Rat has a denominator that does not fit in a 64bit native integer. The default setting for $*RAT-UPGRADE-CLASS is Num: this will silently downgrade a Rat to a Num (floating point), thus sacrificing precision for speed (the current behaviour). Other possible settings are: - CX::Warn downgrade to Num but warn - FatRat silently upgrade to FatRat - Failure don't upgrade, return an appropriate Failure - Exception don't upgrade, throw an appropriate Exception Although the dynamic variable indicates a "class" is expected, there are no technical obstacles to put anything else in there, as long as it can dispatch to a "UPGRADE-RAT" method that takes two Ints: the numerator and denominator. This does *not* affect the inlineability of the CREATE_RATIONAL_FROM_INTS subs, but it does make the default case of silently downgrading to Num slightly slower. Currently this slowness is mostly caused by &DYNAMIC being slow, and no locally cached dynamic variables (which will hopefully change in the not-so-distant future). Inspired by the discussions at: https://www.reddit.com/r/rakulang/comments/mipog4/raku34_python19_extreme_math_steve_roe/ https://twitter.com/liztormato/status/1378294510430601220
c4dc183
to
cead6b0
Compare
Just some bikeshedding, but... It seems odd that an "UPGRADE" could be a fatal exception or a warning message or a conversion to a float. And "POLICY" seems redundant. Why not |
src/core.c/Rat.pm6
Outdated
@@ -83,6 +93,7 @@ multi sub CREATE_RATIONAL_FROM_INTS(Int:D $nu, Int:D $de, Any, Any) is raw { | |||
nqp::p6bindattrinvres(nqp::create(Rat),Rat,'$!numerator',$nu), | |||
Rat,'$!denominator',$de | |||
) | |||
!! $*RAT-UPGRADE-POLICY.UPGRADE-RAT(nu, de) | |||
!! nqp::p6box_n(nqp::div_In($nu,$de)) # downgrade to float |
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.
Syntax error here, looks like you meant to replace the nqp::p6box_n(nqp::div_In($nu,$de)) # downgrade to float
with $*RAT-UPGRADE-POLICY.UPGRADE-RAT(nu, de)
, but accidentally just copied it in.
This dynamic variable indicates the class in which an UPGRADE-RAT
method will be called whenever a Rat has a denominator that does not
fit in a 64bit native integer.
The default setting for $*RAT-UPGRADE-CLASS is Num: this will silently
downgrade a Rat to a Num (floating point), thus sacrificing precision
for speed (the current behaviour).
Other possible settings are:
Although the dynamic variable indicates a "class" is expected, there
are no technical obstacles to put anything else in there, as long
as it can dispatch to a "UPGRADE-RAT" method that takes two Ints:
the numerator and denominator.
This does not affect the inlineability of the CREATE_RATIONAL_FROM_INTS
subs, but it does make the default case of silently downgrading to Num
slightly slower. Currently this slowness is mostly caused by &DYNAMIC
being slow, and no locally cached dynamic variables (which will hopefully
change in the not-so-distant future).
Inspired by the discussions at:
https://www.reddit.com/r/rakulang/comments/mipog4/raku34_python19_extreme_math_steve_roe/
https://twitter.com/liztormato/status/1378294510430601220