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

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.

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

Replies are listed 'Best First'.
Re^7: Small Perl 6 discoveries II, Rats
by syphilis (Archbishop) on Oct 22, 2017 at 23:12 UTC
    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
      the following operation converts $x from a Rat to a FatRat

      That's a a mutable "container" aka a variable (a Scalar in this case) containing one immutable value (a Rat) then another immutable value (a FatRat).

      # Declare a new identifier $x and bind it to a new mutable Scalar cont +ainer: my $x; # Assign a value into the Scalar: $x # An LHS reference to a Scalar returns the Scalar. = # = is assignment. The LHS decides what to do with i +t. 9.9; # Creates a new immutable Rat. # VAR macro returns what the identifier is bound to: say $x.VAR.^name; # Scalar -- the type of variable bound to $x. # All other uses of $x or a Scalar on RHS return the value assigned in +to the Scalar: say $x; # 9.9 say $x.^name; # Rat -- the type of the value assigned into the Sca +lar. $x *= 2; # This is equivalent to ... $x = $x * 2; # ... which assigns result of RHS expression into Sc +alar on LHS. say $x; # 39.6

      Hth.

        That's a mutable "container" aka a variable (a Scalar in this case) containing one immutable value

        I really do not understand the use of the term "immutable".
        Is it perhaps meant to be some shorthand method of signifying that the numerator and denominator are co-prime ? (For this, the gmp C library documentation uses the term "canonical" - in the sense, I think, of "authoritative, standard, accepted".)
        Admittedly, the connection between the meanings of "co-prime" and "immutable" seems very tenuous to me, but it's about all that I can come up with.

        Cheers,
        Rob
        You can give $x a type, but prepare to be disappointed.
        > my Rat $x = 0.1; > $x *= 0.1.FatRat; Type check failed in assignment to $x; expected Rat but got FatRat (Fa +tRat.new(1, 100))