jeanluca has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I'm always struggling with perl variables. This time I have the following problem. In my program I get a hash via a module and in that hash is a number stored: 23465993.9380
Via some calculations I get an other number: 23465993.938
Now I like to check if they are the same (and they are!!)
if ($a == $b) { ... }
But somehow they are different according my perl program. Any suggestions how it is possible they are different. Maybe I have to cast the hash value to a double (if so, how do I do this?)

Thanks in advance
Luca

Replies are listed 'Best First'.
Re: problem with variables
by davido (Cardinal) on Oct 19, 2005 at 08:41 UTC

    It could be that they're not actually the same. Sure, they may look the same, but depending on what calculations were used to derive those numbers, their appearance may be deceiving to you; you could be getting bit by the floating point problems inherent in converting base two (binary) representations of numbers into base ten representations. This is discussed further in perlfaq4 under the heading, "Why am I getting long decimals (eg, 19.9499999999999) instead of the numbers I should be getting (eg, 19.95)?".

    The short version of it is that it's problematic to perform equality comparisons on floating point numbers. This is not a Perl-specific issue; it exists for any language where numbers are represented internally in base-2 (binary).

    Update: I just remembered an article that I found a year or so ago: What Every Computer Scientist Should Know About Foating-Point Arithmetic. It's an excellent article, and might qualify as "far more than you ever wanted to know", but definately will give you enough information to never fall for this type of bug again.


    Dave

      Interesting article!! Thanks
Re: problem with variables
by blazar (Canon) on Oct 19, 2005 at 08:28 UTC
    You should show some minimal example exhibiting the problem. It just shouldn't be so:
    $ perl -le 'print $ARGV[0]==$ARGV[1] ? "ok" : "no"' 23465993.9380 2346 +5993.938 ok
    But chances are that perldoc -q 'long decimals' from the faq may be of some interest to you.

    Incidentally, you shouldn't use $a and $b as general purpose variables, as this may interfere with sort.

      Ok, you aksed for it, @res is the array with hashes and ... represents a very long unique key. What happens here: $row contains a starttime and endtime. What I try to ckeck for is if there are gaps in time between the rows:
      foreach $row (@res) { if(defined $merge->{...} ) { if ( $merge->{...}->{next_samp} == $row->{starttime}) { $merge->{...}->{endtime} = $row->{endtime} ; $merge->{...}->{next_samp} = $row->{endtime} + 1./ +$row->{srate} ; } else { delete $merge->{...}->{next_samp} ; $dummy[++$#dummy] = $merge->{...} ; $merge->{ ...} = $row ; $merge->{...}->{next_samp} = $row->{endtime} + 1./ +$row->{srate} ; } } else { $merge->{ ...} = $row ; $merge->{...}->{next_samp} = $row->{endtime} + 1./$row +->{srate} ; } }
        Well, it seems that in the meanwhile you have solved your problem. My point was about preparing a suitable complete but minimal example still exhibiting the problem. This is a general recommendation as to how to ask for help, and sometimes it's hard or even impossible to do, but that's rare. More commonly it even happens that while doing so one will find the answer to his own question.

        Now, as side notes to comment you code:

        • I wouldn't use all those nested if's; I'd recommend you to read up something about next, last and while you're there also redo,
        • I wouldn't assign to $dummy[++$#dummy], but stick with push, since it's what it's actually for.
Re: problem with variables
by monkey_boy (Priest) on Oct 19, 2005 at 08:47 UTC
    Try comparing your numbers to a fixed number of decimal places.
    e.g.
    if (sprintf("%.3f",$x) == sprintf("%.3f",$y)) { print "IS equal to 3 dp\n"; };



    This is not a Signature...
      Using sprintf("%.5f",$x) works!! thanks. But it is still strange that if there are more decimal numbers, why they are not printed to the screen ?

      Luca

        Because they may be thirteen decimal places to the right hand side of the decimal point. If the number, for example, is converted back to base ten as "19.9490000000001", the output will show a rounded-off version that doesn't include the most insignificant digits. In other words, more precision is stored than is displayed. Part of the reason that not all digits are displayed is because of the high liklihood that they're tainted by base-2 to base-10 conversion errors.


        Dave

Re: problem with variables
by Delusional (Beadle) on Oct 19, 2005 at 11:03 UTC
    Wouldn't
    if ($a == $b) { ... }

    Be correctly written as:
    if ($a eq $b) { ... }

    ?

      No, eq is for string comparison, which definitely wouldn't work here. In Perl == does numeric comparison.

      See perlop.

      You may be thinking of (korn) shell where it's the other way round.


      s^^unp(;75N=&9I<V@`ack(u,^;s|\(.+\`|"$`$'\"$&\"\)"|ee;/m.+h/&&print$&
        Works fine for me:
        #!/usr/bin/perl use strict; use warnings; my $num1 = 1234.56789; my $num2 = 1234.56789111; if ( $num1 == $num2 ) { print "equal.\n"; } else { print "Please don't try to check for equality of floats that way.\ +n"; } if ( sprintf( "%.3f", $num1 ) == sprintf( "%.3f", $num2 ) ) { print "Yup, they show up as equal.\n"; } if ( sprintf( "%.3f", $num1 ) eq sprintf( "%.3f", $num2 ) ) { print "Yup. This works too.\n"; }
        output:
        Please don't try to check for equality of floats that way. Yup, they show up as equal. Yup. This works too.