in reply to Re^4: Variables are automatically rounded off in perl (audiences)
in thread Variables are automatically rounded off in perl

If you are using '==' or '!=' on computed floating point values, then you've already lost my sympathy

I, of course, am completely shattered at having lost your sympathy ;-)

... we got tons of complaints because the result of code like ... <snip> ... was not a nice "0.5" but "0.5000000000000002".

The output of that code is *still* not nice, even today. Try it and you'll see.
We need to get the output precision further reduced.

Seriously ... you can reduce the decimal precision of print even further, and that sort of procedure can still produce results that are "not nice":
perl -le "for(1..100000000) {$x += 0.01}print $x;" 1000000.00077928
We need to reduce displayed output to no more than 10 decimal digits ?? (Even less)

The "unniceness" of those results come from cumulatively adding a value that is not precisely 1/100.
I don't see that it has much to do with the decimal precision of print()'s output.

People who can't stand miscounting one grain of sand on their huge beach are a much better choice for who needs to do a tiny bit of extra work

Yes, that's the current state of play. I hope you pointed that out to the people who complained about the output of the snippet you provided.
And I'm sure they were quite happy to do that extra work.

Cheers,
Rob

Replies are listed 'Best First'.
Re^6: Variables are automatically rounded off in perl (slippery)
by tye (Sage) on Jul 25, 2016 at 19:40 UTC
    The output of that code is *still* not nice, even today. Try it and you'll see.

    I ran that code in multiple versions of Perl that I had handy. The output was the intuitive ("nice") version, even when I ran the code with twice as many iterations.

    Note that your stance would produce 16 digits of noise for even trivial cases like print 0.1+0.2.

    Yes, there is no one obvious best value for how many digits to show by default. C says 6, for example. So, you give a "slippery slope" argument, dodge one argument by concentrating on tangential aspects of the phrasing, and make a false assumption about whether I ran the code. Not much to refute here.

    - tye        

      The output was the intuitive ("nice") version

      Did you look at the intermediate values ? (That's what your original complainants were doing.)
      C:\_32\pscrpt>perl -le "print $];" 5.024000 C:\_32\pscrpt>perl -V:archname archname='MSWin32-x86-multi-thread-64int'; C:\_32\pscrpt>perl -V:nvtype nvtype='double'; C:\_32\pscrpt>type try.pl use warnings; use strict; my $x = 0; for( 1..100 ) { $x += 0.01; print "$x\n" if length $x > 4; } C:\_32\pscrpt>perl try.pl 0.810000000000001 0.820000000000001 0.830000000000001 0.840000000000001 0.850000000000001 0.860000000000001 0.870000000000001 0.880000000000001 0.890000000000001 0.900000000000001 0.910000000000001 0.920000000000001 0.930000000000001 0.940000000000001 0.950000000000001 0.960000000000001 0.970000000000001 0.980000000000001 0.990000000000001 C:\_32\pscrpt>
      Those are actually correct values (rounded to 15 decimal digits of precision) for a perl whose nvtype is an 8-byte double.

      And it's also to be expected (given perl's current practice) that the next (and last) value to be calculated is printed as "1" - because 1.0000000000000007 rounded to 15 decimal digits of precision is exactly that.

      Cheers,
      Rob

        I checked 50 iterations (as hinted at by the text) and 100 iterations (as shown with the code). You claimed my code showed ugly values, and yet your current demonstration actually shows the opposite. Had I chosen between 80 and 99 iterations, I might have seen ugly values (if our Perl versions are similar enough). I didn't try any of those. It was just a quick example. But now we see that your claim was actually false. But why don't we discuss something of more import instead of picking nits about some trivial example?

        The sweet spot for this feature (which you view as a bug) is a relatively small number of values each with a relatively small number of digits after each decimal point. So 80 is pushing "a small number of values" a bit. I'd actually prefer one more digit of blur so even a few hundred such numbers would likely be handled. I don't mind at all if a few thousand values is too much to be handled by this feature.

        I find that summing a few dozen numbers, each with only a few digits after the decimal is a pretty darn common scenario when doing rather informal calculations with Perl. And I have personally benefited quite a few times from not having to bother to force a particular format upon the output in such scenarios.

        I recall it even being a pain to get as nice of output when using a specific printf format. A quick test just now shows that '%.6g' seems to produce as nice of output as the default these days. I don't know if that is a change since long ago, or if I just failed to find that solution, or if my current quick check is overlooking cases that had caused me problems previously.

        But I still find it more appropriate to put the onus on those wanting 17 full digits of precision in their output (who I find to be much more likely to be knowledgeable about such things) than to force casual Perl users to jump through such hoops.

        - tye