develooper Front page | perl.perl5.porters | Postings from September 2015

Re: smartmatch needs your eyes

Thread Previous | Thread Next
September 24, 2015 12:51
Re: smartmatch needs your eyes
Message ID:
Kent Fredric wrote:
>Isn't this a moot point because doing "==" on floats is considered a
>bad idea at best anyway?

No, == can be used perfectly correctly on floating-point values.
As with everything else around floating-point rounding, it does need some
consideration of the specific algorithm being used.  Note that where one
side of the comparison is an integer, in the context of Perl programming
it's quite likely that an exact comparison is required.

>                                           You generally want "within
>tolerance X", for which stringification serves as a reasonable proxy,

Stringification just gives you an incoherently reduced precision.
The tolerance that's required has to be determined by consideration of
the specific algorithm.  String equality is not a good default tolerance;
I think it's a worse default than numerical equality.

>Yes, bignum is probably overkill here, but all I'm doing is using it
>to make sure nextup(42)  stringifies to its full precision.

Actually it doesn't stringify the exact value.  It's not even correct
to the precision that it gives:

$ perl -Mbignum -MData::Float=nextup -lwe 'print nextup(42); printf "%.50f\n", nextup(42)'

(The printf output is exact.)

>nextup(42) ~~ within( 42, sigfig => 20,   )

That's a much better arrangement.


Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About