Re^2: Small Perl 6 discoveries II, Rats
by afoken (Chancellor) on Sep 27, 2017 at 18:38 UTC
|
| [reply] [Watch: Dir/Any] |
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|
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.
| [reply] [Watch: Dir/Any] [d/l] |
|
|
|
|
| [reply] [Watch: Dir/Any] |
|
|
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|
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 | [reply] [Watch: Dir/Any] [d/l] [select] |
|
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.
| [reply] [Watch: Dir/Any] |
|
|
|
|
If the Rat class isn't any more accurate than a Num, but it's much slower, then what purpose does it serve?
| [reply] [Watch: Dir/Any] |
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|