in reply to Re: Small Perl 6 discoveries III, Ints
in thread Small Perl 6 discoveries III, Ints

It will reuse a floating-point literal for another value that's approximately equal

I'll speculate that it will only engage in "reuse" when the two values are exactly equal. (Bear in mind that 2e25 and 20e24 are exactly equal.)
So it should be a valid optimization - it's only being brought undone by the miscalculation of 20e24.

The miscalculation of 20e24 is a bug in perl6 that should be reported, though FAIK it might already have been fixed.

UPDATE: The same miscalculation is also a bug in perl5 - but such miscalculations are already known in perl 5

Cheers,
Rob
  • Comment on Re^2: Small Perl 6 discoveries III, Ints

Replies are listed 'Best First'.
Re^3: Small Perl 6 discoveries III, Ints
by Anonymous Monk on Sep 29, 2017 at 03:02 UTC
    > 1.000000000000001e0 - 1e0 0 > 1.000000000000001e0 - 1 1.11022302462516e-15
      > 1.000000000000001e0 - 1e0 0
      Certainly can't reproduce that one on perl5 ;-)
      It looks buggy to me.
      It appears that 1.000000000000001e0 has been rounded to 1.00000000000000e0 prior to doing the subtraction.
      Though it's also possible that 0.000000000000001e0 has been rounded to 0 when the value is printed out.
      Does perl6 actually assert that 1.000000000000001e0 == 1e0 ??

      Of course, "double" builds of perl5 will round 1.000000000000001e0 to 1e0 prior to printing it out:
      C:\>perl -le "print 1.000000000000001e0;" 1
      but that's different to what appears to be happening in the given example. (And even though perl5 has printed out "1", it knows damn well that 1.000000000000001e0 != 1e0)

      Cheers,
      Rob
        This too is due to the exponential notation always generating Nums instead of Rats. So if you substract a value with rounding errors minus the same value without rounding errors, what you have left is the rounding error.


        holli

        You can lead your users to water, but alas, you cannot drown them.
        > my $a = 1.000000000000001e0; my $b = 1e0; $a - $b 0 > $a - 1 1.11022302462516e-15 > $b - 1 1.11022302462516e-15 > $a.WHERE 4405787632 > $b.WHERE 4405787632 > 1.000000000000001e0 == 1e0 True