Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^11: Small Perl 6 discoveries II, Rats

by raiph (Deacon)
on Oct 27, 2017 at 18:38 UTC ( [id://1202180]=note: print w/replies, xml ) Need Help??


in reply to Re^10: Small Perl 6 discoveries II, Rats
in thread [Perl6] Small discoveries I, __DATA__

First, Rats aren't FatRats and vice-versa. (If we need to discuss why, we'll need to start with Larry's comment that "we've intentionally steered clear of any sort of numeric tower"; and then dig into what p6curious meant when they followed that up with "its one of the things that has impressed me the most about perl6"; and then consider Rats and FatRats in particular.)

Second, P6 has a nice feature that directly addresses this. It has been implemented for routine parameters but not yet `my` declarations:

sub fails-if-passed-Rat (FatRat $r) { say WHAT $r } ; sub works-if-passed-number (FatRat() $r) { say WHAT $r } ; fails-if-passed-Rat 1.0 ; # typecheck failure works-if-passed-number 1.0 ; # coerces 1.0 to a FatRat my FatRat $r = 1.0 ; # typecheck failure my FatRat() $r = 1.0 ; # should say "not yet implemented"

Replies are listed 'Best First'.
Re^12: Small Perl 6 discoveries II, Rats
by Anonymous Monk on Oct 27, 2017 at 19:05 UTC
    First, Rats aren't FatRats and vice-versa.
    They're both Rational, but that's still not very useful.
    > my Rational $x = 1.111111111111111111111; 1.11111111111111111604544 > $x *= 0.1; Type check failed in assignment to $x; expected Rational but got Num ( +0.111111111111111e0)
    The argument type-coercion is cute, but there doesn't seem to be any way to set the precision.
    > sub foo(FatRat() $x) { return $x }; foo(pi).nude (355 113) > pi.FatRat(1e-8).nude (103993 33102)
    And I don't know how to interpret this message...
    > my FatRat() $x = 0.1; ===SORRY!=== Error while compiling: Coercion FatRat(Any) is insufficiently type-like to qualify a variable ------> my FatRat() $x⏏ = 0.1; expecting any of: constraint
      In your first block of code you do a calculation whose resulting denominator is more than 64 bits. So the result is a Num.

      Your second block shows BUG Num.FatRat coercion is inexact.

      Your third block is an LTA error message. It should say something to the effect of "coercion type constraint on variable declaration not yet implemented".

      Hth.

        In your first block of code you do a calculation whose resulting denominator is more than 64 bits. So the result is a Num.
        So what you're telling me is that it's "working as designed"? There seems to be no way to get exact rational calculations without scattering a bunch of explicit FatRat conversions through my code. If I slip up somewhere, Perl6 will either hit me with a sneaky type error, or (worse) silently reduce precision on me.
        Your third block is an LTA error message.
        Lighter Than Air? Lawn Tennis Association? Long-Term Abuse?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1202180]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (10)
As of 2024-04-18 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found