"be consistent" PerlMonks

### Re^4: Small Perl 6 discoveries II, Rats

by holli (Monsignor)
 on Sep 27, 2017 at 20:11 UTC ( #1200224=note: print w/replies, xml ) Need Help??

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

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.

Replies are listed 'Best First'.
Re^5: Small Perl 6 discoveries II, Rats
by Anonymous Monk on Sep 27, 2017 at 21:42 UTC
```> 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.
Am I allowed to subtract a rational from a float if I explicitly convert it to a float first? Pretty please?
```> 3e-1 - Num(.3)
5.55111512312578e-17
Oh look, I got the same answer. Also, Perl6's printf lies.
```> printf "%.100f\n", .3
0.30000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000
> printf "%.100f\n", 3e-1
0.30000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000
> printf "%.100f\n", Num(.3)
0.30000000000000000000000000000000000000000000000000000000000000000000
+00000000000000000000000000000000
But you can still get an honest printf with Inline::Perl5.
```use Inline::Perl5;
my \$p5 = Inline::Perl5.new;
\$p5.run('sub p { printf "%.100f\n", shift }');
\$p5.call('p', .3);
\$p5.call('p', Num(.3));
\$p5.call('p', 3e-1);
=finish
0.29999999999999998889776975374843459576368331909179687500000000000000
+00000000000000000000000000000000
0.29999999999999998889776975374843459576368331909179687500000000000000
+00000000000000000000000000000000
0.30000000000000004440892098500626161694526672363281250000000000000000
+00000000000000000000000000000000

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2018-07-19 20:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

Results (420 votes). Check out past polls.

Notices?