Perl: the Markov chain saw PerlMonks

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

 on Sep 27, 2017 at 21:42 UTC ( #1200235=note: print w/replies, xml ) Need Help??

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

```> perl5 -E 'say 3e-1 - .3'
0
> perl6 -e 'say 3e-1 - .3'
5.55111512312578e-17

Replies are listed 'Best First'.
Re^6: Small Perl 6 discoveries II, Rats
by Laurent_R (Canon) on Sep 28, 2017 at 14:11 UTC
```> 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://1200235]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (3)
As of 2018-03-23 04:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
When I think of a mole I think of:

Results (287 votes). Check out past polls.

Notices?