Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Small Perl 6 discoveries II, Rats

by Anonymous Monk
on Sep 27, 2017 at 15:09 UTC ( #1200204=note: print w/replies, xml ) Need Help??


in reply to [Perl6] Small discoveries I, __DATA__

> say 1.111111111111111111111 1.11111111111111111604544

Replies are listed 'Best First'.
Re^2: Small Perl 6 discoveries II, Rats
by afoken (Abbot) on Sep 27, 2017 at 18:38 UTC
      So the question becomes, why does perl6 print all those wrong digits?
      > perl5 -E 'say 1.111111111111111111111' 1.11111111111111 > perl6 -e 'say 1.111111111111111111111' 1.11111111111111111604544
        Are you trolling or what?

        Here Perl6 is 1000 times more accurate, it's just not lying to you about being arbitrarily precise. You will notice that 1.11111111111111 is three digits shorter than 1.11111111111111111 !

        And also:
        holli@605 ~ $ perl -e 'say "oops" unless 0.1 + 0.2 == 0.3' oops holli@605 ~ $ perl6 -e 'say "yeah" if 0.1 + 0.2 == 0.3' yeah


        holli

        You can lead your users to water, but alas, you cannot drown them.
        why does perl6 print all those wrong digits?

        There is a well-hidden message in the "wrong" digits. Click here and follow the links.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Re^2: Small Perl 6 discoveries II, Rats
by bduggan (Pilgrim) on Oct 19, 2017 at 12:39 UTC
    If you want arbitrary precision rational arithmetic, you can use a FatRat:
    > say 1.111111111111111111111.FatRat 1.111111111111111111111
      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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1200204]
help
Chatterbox?
and snow settles gently...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2018-01-22 01:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (230 votes). Check out past polls.

    Notices?