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

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.

  • Comment on Re^4: Small Perl 6 discoveries II, Rats

Replies are listed 'Best First'.
Re^5: Small Perl 6 discoveries II, Rats
by syphilis (Archbishop) on Oct 22, 2017 at 11:34 UTC
    $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
      From docs.perl6.org/type/Rat (anons can't post full urls):
      To prevent the numerator and denominator from becoming pathologically large, the denominator is limited to 64 bit storage. On overflow of the denominator a Num (floating-point number) is returned instead.
      However, Rat values generated from decimal notation can violate the 64-bit limit.

      You can use $x.WHAT to find $x's type if you get tired of guessing based on the number of decimal places displayed.

        To prevent the numerator and denominator from becoming pathologically large, the denominator is limited to 64 bit storage

        Well, that doesn't seem to be the case with (latest) rakudo-star-2017.07:
        > my $x=9.999999999999999999999999999999999999999999999.Rat; $x.nude (9999999999999999999999999999999999999999999999 1000000000000000000000 +000000000000000000000000) > $x.WHAT (Rat)
        but the base 10 denominator of 1000000000000000000000000000000000000000000000 is a 150-bit value.

        The documentation also concludes with "Rat objects are immutable" - yet the following operation converts $x from a Rat to a FatRat:
        > $x *= 0.3.FatRat; $x.nude (29999999999999999999999999999999999999999999997 100000000000000000000 +00000000000000000000000000) > $x.WHAT (FatRat)
        Am I misunderstanding that documentation ?
        Is there a way of interpreting the documentation such that it does match the above behaviours ?

        Cheers,
        Rob