Anonymous Monk brought up a really interesting discovery here. Unfortunately, that thread got derailed, so I’m making a separate one, as suggested by Your Mother.
One of the first things I found while testing is this really interesting tidbit:
But then I realized I was using an outdated Rakudo (2017.04). So I updated to 2017.09, and now those print 1 and False, respectively. There’s still some interesting behavior in 2017.09, though:$ perl6 -e 'say 0.99999999999999999000001' 1.000000000000000073886090 $ perl6 -e 'say 0.99999999999999999000001 > 1' True
Note that the second number printed is strictly smaller than the first one, even though the second source number is strictly larger than the first one, spelled in the same fashion and to the same number of significant digits! However, comparison and subtraction still return exact results:$ perl6 -e 'say 0.7777777777777777777770' 0.77777777777777785672697 $ perl6 -e 'say 0.7777777777777777777771' 0.777777777777777767909129
Okay, that’s probably because one is a Num and the other is a Rat, so let’s convert everything to Num explicitly:$ perl6 -e 'say 0.7777777777777777777771 > 0.7777777777777777777770' True $ perl6 -e 'say 0.7777777777777777777771 - 0.7777777777777777777770' 1e-22
Huh. Now they print the same, but they’re still different numbers when compared. Note that the sign of the difference got switched:$ perl6 -e 'say Num(0.7777777777777777777770)' + 0.777777777777778 $ perl6 -e 'say Num(0.7777777777777777777771)' 0.777777777777778 $ perl6 -e 'say Num(0.7777777777777777777770) > Num(0.7777777777777777 +777771)' True $ perl6 -e 'say Num(0.7777777777777777777770) - Num(0.7777777777777777 +777771)' 1.11022302462516e-16
Also interesting is that many Nums don’t survive a round-trip to Str:$ perl6 -e 'my $a = 0.7777777777777777777770; my $b = 0.77777777777777 +77777771; say $a <=> $b; say Num($a) <=> Num($b)' + Less More
Can anyone point me to the Perl6 specs/docs/whatever that explain those behaviors?$ perl6 -e 'my $a = Num(1/9); say $a == Num(Str($a))' False
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Perl6 discoveries — floating-point
by syphilis (Archbishop) on Oct 18, 2017 at 12:24 UTC | |
by Grimy (Pilgrim) on Oct 18, 2017 at 14:13 UTC | |
by syphilis (Archbishop) on Oct 19, 2017 at 00:41 UTC | |
Re: Perl6 discoveries — floating-point
by Grimy (Pilgrim) on Oct 18, 2017 at 16:45 UTC |
Back to
Meditations