Keep It Simple, Stupid PerlMonks

### Re: False positive on inequality comparison

by toolic (Bishop)
 on Apr 03, 2009 at 20:08 UTC ( #755333=note: print w/replies, xml ) Need Help??

in reply to False positive on inequality comparison

why is it doing this?
My floating point comparison does not work. Why ?
• Comment on Re: False positive on inequality comparison

Replies are listed 'Best First'.
Re^2: False positive on inequality comparison
by Nkuvu (Priest) on Apr 03, 2009 at 21:54 UTC

I'm familiar with potential problems with floating point inequality when the values are the results of some computation. But if I assign 1.2345 to \$var_a and then assign 1.2345 to \$var_b, then perform the equality check, I would expect them to be equal. Direct assignments should not cause different floating point representations, as far as I know.

Once that value is used in any sort of arithmetic, all bets are off.

As an additional test, I wrote a short script (which I should have done earlier, but it still doesn't really explain anything):

```#!/usr/bin/perl
use strict;
use warnings;

my %values;
while (my \$line = <DATA>) {
if (\$line =~ /(\w)_(r|ch)ange\s(\S+)/) {
\$values{\$1}{\$2."ange"} = \$3;
}
}

for my \$ key(sort keys %values) {
if (\$values{\$key}{'change'} != \$values{\$key}{'range'}) {
print "Not ";
}
print "Equal:\nChange: \$values{\$key}{change}\nRange : \$values{\$key
+}{range}\n\n";
}

__DATA__
var_a_change 101.945484
var_a_range 101.945484
var_b_change -80.5498336616321
var_b_range -80.5498336616321
var_c_change 45142.388774415
var_c_range 45142.388774415
var_d_change -159.905028384
var_d_range -159.905028384
var_e_change -159.905028384
var_e_range -159.905028384
var_f_change 181.019658002611
var_f_range 181.019658002611
var_g_change 1.57615706403821
var_g_range 1.57615706403821

Every single comparison there passes the equality check. And the only thing this script is doing differently than my actual script is reading the values from Excel. That may be key, I don't know.

I believe you've hit the nail on the thumb right here.

And the only thing this script is doing differently than my actual script is reading the values from Excel.

Depending on how the values were added to Excel (or calculated there), the representations may be different even though they look the same.

This is another instance of a more subtle problem. You can't truly trust any data that comes from outside your program. The real world always seems to be messier than it needs to be.<shrug/>

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://755333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2021-11-28 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?