in reply to Re^4: Variables are automatically rounded off in perl (audiences)
in thread Variables are automatically rounded off in perl
The notion that one grain of sand on a beach is not important, but one hundred are, is dubious to say the least.
perl -le '$x += 0.1 for 1..100; print $x;'
There are couple of points to make. First: it's not about print really. It's about stringification. Perl scalars can get pPOK from "action at a distance"; and stringification is currently lossy.
Someone scientifically minded will of course understand the caveats of floating point, but at the same time expect roundtrip accuracy. The numbers must convert back and forth from text to internal representation. This caters to human audiences; to suggest hexdumps or similar would fly in the face of your own argument. Why would you insist people need to go out of their way to have their numbers stringify correctly?
Frankly, this sounds like you are discouraging perl use for scientific work ("the wrong tool"). Also reminds me of the definition π = 22/7 ("correct for all practical purposes").
Update. Uh, and a link to one of the curiosities: shortest round-tripping.
Update2. Here's a better demonstration of the WTF equality problem.
#! /usr/bin/perl -wl my @input = qw( 3335.9999999999995 3336.0000000000000 ); my @nums = map 0+$_, @input; sub uniq_count { int keys %{{map {$_ => 1} @_}} } print "The numbers are:"; printf " %.16e\n", $_ for @nums; printf "A set of %d different value(s)\n", uniq_count(@nums);
And yes, this also affects Memoize, FWIW.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^6: Variables are automatically rounded off in perl (scientists)
by tye (Sage) on Jul 25, 2016 at 19:40 UTC | |
by oiskuu (Hermit) on Jul 25, 2016 at 21:08 UTC | |
by tye (Sage) on Jul 25, 2016 at 21:43 UTC | |
by oiskuu (Hermit) on Jul 25, 2016 at 22:16 UTC |