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

Re: smartmatch needs your eyes

Thread Previous | Thread Next
From:
Zefram
Date:
September 24, 2015 12:51
Subject:
Re: smartmatch needs your eyes
Message ID:
20150924125128.GC9229@fysh.org
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)'
42.000000000000007105427357600992
42.00000000000000710542735760100185871124267578125000

(The printf output is exact.)

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

That's a much better arrangement.

-zefram

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About