Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Small Perl 6 discoveries II, Rats

by bduggan (Pilgrim)
on Oct 19, 2017 at 12:39 UTC ( [id://1201671]=note: print w/replies, xml ) Need Help??


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

If you want arbitrary precision rational arithmetic, you can use a FatRat:
> say 1.111111111111111111111.FatRat 1.111111111111111111111

Replies are listed 'Best First'.
Re^3: Small Perl 6 discoveries II, Rats
by syphilis (Archbishop) on Oct 20, 2017 at 04:12 UTC
    If you want arbitrary precision rational arithmetic, you can use a FatRat

    I didn't know about FatRats (yes, I know very little about perl6) - so I had a bit of a play (on rakudo-star-2017.07) and encountered confusing results:
    > my $x = 1.111111111111111111111.FatRat; my $y = 1.111111111111111111 +111.Rat; $x - $y 0 > $x == $y True > say $x 1.111111111111111111111 > say $y 1.11111111111111111604544 >
    On the bases that $x-$y==0 and $x==$y one is led to believe that $x and $y are exactly equivalent.
    Yet, say() presents us with different values.

    Are the 2 rationals equivalent ?
    If so, then why does say() output different values ?
    If not, then why do both $x-$y==0 and $x==$y evaluate as "True" ?

    Interestingly, 1.11111111111111111604544 is the value of the double 1.1111111111111111 (16 decimal places) rounded to 23 decimals:
    C:\>perl -le "printf '%.22e\n', 1.1111111111111111;" 1.1111111111111111604544e+000\n
    Perhaps this ties in with:
    > my $x = 1.111111111111111111111.FatRat; my $y = 1.1111111111111111.N +um; $x - $y 0 > $x == $y True
    How does one coerce perl6 into displaying the actual numerator and denominator of these rationals ?

    Cheers,
    Rob
      How does one coerce perl6 into displaying the actual numerator and denominator of these rationals?
      $x.nude

      That would make sense if the numerator and denominator were $x.nu and $x.de, but they're not. You have to type out $x.numerator and $x.denominator. I thought we were trying to make Perl6 safe for 10-year-old girls, but I guess someone just couldn't pass up an opportunity for a crude joke.

        $x.nude

        That enables me to see that 1.111111111111111111111.FatRat and 1.111111111111111111111.Rat have equivalent rational values:
        > my $x = 1.111111111111111111111.Rat; $x.nude (1111111111111111111111 1000000000000000000000) > my $y = 1.111111111111111111111.FatRat; $y.nude (1111111111111111111111 1000000000000000000000)
        and that $x (the Rat) gets handled in a way that I don't really expect:
        > my $r1 = $x * 0.3 0.333333333333333 > $r1 = $x * 0.3.Rat 0.333333333333333 > $r1 = $x * 0.3.Num 0.333333333333333 > $r1 = $x * 3e-1 0.333333333333333 > $r1 = $x * 0.3.FatRat 0.3333333333333333333333
        though the last result is as I expected.
        Compare those outputs with:
        > my $r2 = $y * 0.3 0.3333333333333333333333 > my $r2 = $y * 0.3.Rat 0.3333333333333333333333 > my $r2 = $y * 0.3.Num 0.333333333333333 > my $r2 = $y * 3e-1 0.333333333333333 > my $r2 = $y * 0.3.FatRat 0.3333333333333333333333
        which is more in keeping with my expectations.

        I'm sure it all makes sense if you know how to look at it from the appropriate angle.

        Cheers,
        Rob
Re^3: Small Perl 6 discoveries II, Rats
by Anonymous Monk on Oct 19, 2017 at 15:52 UTC
    If the Rat class isn't any more accurate than a Num, but it's much slower, then what purpose does it serve?
      Well -- I'd say it is more accurate (though not more precise) -- it can accurately represent rational numbers, so you can do rational arithmetic without rounding errors. e.g.
      > .3.Num - .2.Num - .1.Num == 0 False > .3 - .2 - .1 == 0 True
        The reason this digression started is because it can't accurately represent rational numbers. Except for trivial examples, I suppose.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-25 21:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found