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.
| [reply] |
|
|
Interesting article!! Thanks
| [reply] |
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. | [reply] [d/l] [select] |
|
|
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} ;
}
}
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
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...
| [reply] [d/l] |
|
|
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
| [reply] |
|
|
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.
| [reply] |
Re: problem with variables
by Delusional (Beadle) on Oct 19, 2005 at 11:03 UTC
|
if ($a == $b) {
...
}
Be correctly written as:
if ($a eq $b) {
...
}
? | [reply] [d/l] [select] |
|
|
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$&
| [reply] [d/l] [select] |
|
|
#!/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.
| [reply] [d/l] [select] |
|
|
|
|