P is for Practical PerlMonks

Re^3: Small Perl 6 discoveries II, Rats

 on Sep 27, 2017 at 19:05 UTC ( #1200219=note: print w/replies, xml ) Need Help??

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

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

Replies are listed 'Best First'.
Re^4: Small Perl 6 discoveries II, Rats
by holli (Monsignor) on Sep 27, 2017 at 20:11 UTC
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.
```> perl5 -E 'say 3e-1 - .3'
0
> perl6 -e 'say 3e-1 - .3'
5.55111512312578e-17
```> perl5 -E 'say 3e-1 - .3'
0
In this case, the floating values for 3e-1 and .3 are both "wrong," i.e. not really equal to mathematical .3, but since they have the same error, subtracting one from the other happens to return 0. So the result seems correct, but only because the two inaccuracies compensate each other.
```> perl6 -e 'say 3e-1 - .3'
5.55111512312578e-17
Here, voluntarily or not, you mix up two different types. 3e-1 is a floating-point value (and, as such, liable to inaccuracies, as we all know), whereas .3 is a rational, and is entirely accurate.

For example, printing the first 100 digits of .3 under Perl 6:

```> printf "%.100f\n", .3;
0.30000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000
>
Subtracting a rational from an inaccurate float is just wrong, it is like adding apples and oranges.
Re^4: Small Perl 6 discoveries II, Rats
by afoken (Abbot) on Sep 27, 2017 at 21:23 UTC
why does perl6 print all those wrong digits?

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
Why the difference?
```> 111111111111111111111/1000000000000000000000
0.111111111111111
> .111111111111111111111
0.11111111111111111604544

I would assume that one is a rational number while the other gets constructed and represented as a floating point number. I think you can somehow look into how Perl6 represents the values but I don't know it.

Create A New User
Node Status?
node history
Node Type: note [id://1200219]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-04-22 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (82 votes). Check out past polls.

Notices?