When I substituted 'eq' versus '==' it works.

Not sure why though...

Ken

At 03:07 PM 7/24/2005, John Deighan wrote:
At 02:20 PM 7/24/2005, Ed Chester wrote:

John Deighan::
> Is there a safe way to compare 2 floating point numbers in Perl? [snip]
> My debugger says that they're both '630.24' [snip]
> However, the == test fails and the != test succeeds

can you post code with the comparison == that fails ?
if the debugger says they're the same, they're very very probably the same.
are you sure the variable (or whatever) you're giving to == are really what you
want them to be?
there are lots of ways to compare numbers, right down to looping over the bits
and logically
XNORing them.

Sorry about the lack of sample code, but I know that people who work with floating point numbers know about this problem, and I was wondering what the best solution was. Here is sample code with output. Note that I'm not formatting the output or rounding or anything - just printing out the contents of the 2 variables.

my $sum1 = -237.15;
my $sum2;
$sum2 += -26.35;
$sum2 += -26.35;
$sum2 += -26.35;
$sum2 += -26.35;
$sum2 += -26.35;
$sum2 += -26.35;
$sum2 += -26.35;
$sum2 += -26.35;
$sum2 += -26.35;

print("sum1 = $sum1\n");
print("sum2 = $sum2\n");
if ($sum1 == $sum2) {
        print("EQUAL\n");
        }
else {
        print("NOT EQUAL\n");
        }

OUTPUT:

sum1 = -237.15
sum2 = -237.15
NOT EQUAL


_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to