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

[perl #126502] Storable Alters Floating Point Number?

Thread Next
From:
masterchiefaragorn
Date:
October 31, 2015 21:28
Subject:
[perl #126502] Storable Alters Floating Point Number?
Message ID:
rt-4.0.18-19620-1446225065-1073.126502-75-0@perl.org
# New Ticket Created by  masterchiefaragorn 
# Please include the string:  [perl #126502]
# in the subject line of all future correspondence about this issue. 
# <URL: https://rt.perl.org/Ticket/Display.html?id=126502 >


Hi,

I don't know if this is an issue with Storable or with Perl itself, but when I compare two identical floating points together, they compare correctly.  However, when I freeze() and thaw() them, they do not.

I am familiar with the dynamics of how floating points are stored, so that's not the issue.  The issue is that Storable slightly alters an SvPVNV in a way that it doesn't compare like it used to.  Here's the code (this is on Perl 5.18 and Storable 2.51):

    use 5.018001;
    use warnings;
    use Math::Round qw(nearest);
    use Storable qw(freeze thaw);

    my $a = nearest(.01,-656.8);
    my $b = nearest(.01,-656.8);
    if ($a eq 'qwer') {} # this upgrades $a to an SvPVNV
    say '' . ($a == $b ? 'EQUAL' : 'NOT EQUAL');

    my $c = thaw(freeze(\$a)); # here Storable alters the SvPVNV so that it compares incorrectly
    my $d = thaw(freeze(\$b));
    say '' . ($c == $d ? 'EQUAL' : 'NOT EQUAL');

OUTPUT:

    EQUAL
    NOT EQUAL


(NOTE: I know that '==' is unreliable for comparing floating point numbers, and that doing a nearest(.01,$a) eq nearest(.01,$b) is a more reliable way to solve this.  Regardless, I still think this might be a bug folks would want addressed?)


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