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

Non-transitive numeric equality comparison: is that a problem?

Thread Next
From:
Zsbán Ambrus
Date:
November 5, 2015 14:55
Subject:
Non-transitive numeric equality comparison: is that a problem?
Message ID:
CAHku1CFcpK9tv3F9mQ7NyFBNpkjD_n=peq6U0zP0OgJhLZqvBw@mail.gmail.com
perl
8 == length(pack "J") or die;
$b = 1<<58; $c = $b + 1e-7; $d = $b + 1;
warn join " ", $b, $c, $d, $b <=> $c, $c <=> $d, $b <=> $d;
__END__
288230376151711744 2.88230376151712e+017 288230376151711745 0 0 -1 at - line 3.

When you numeric compare an integer and a floating point number, perl
seems to convert the integer to a float and then compares the two
floats.  If IVs are 64 byte sized, this can cause two values that are
numerically unequal to compare equal, namely $c and $d here.  The
result is the numeric equality, as understood by perl, isn't
transitive: here $c is equal to both $b and $d, but $b is not equal to
$d.

Is this a feature or a bug?  Can it cause problems, eg. when you
numerically sort lists that contain such values, or when you otherwise
handle numerical data?

-- Ambrus

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