Comparing floating points means to ask whether one number falls within a
RANGE of values.
You have to say it like this:
use strict;
use warnings;
my $bob = 630.239;
my $test = 630.24;
my $range1 = 0.001;
my $range2 = 0.00001;
no warnings;
if( ($test <= ($bob+$range1) ) && ($test => ($bob - $range1) ) ){
print("$test is equal to $bob within $range1 \n");
}
if( ($test <= ($bob+$range2) ) && ($test => ($bob - $range2) ) ){
print("$test is equal to $bob within $range2\n");
} else {
print("$test is not equal to $bob within $range2\n");
}
> -----Original Message-----
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of John Deighan
> Sent: Monday, July 25, 2005 1:09 AM
> To: [email protected]
> Subject: comparing floating point numbers
>
>
> Is there a safe way to compare 2 floating point numbers in Perl? I
> have 2 numbers, one of which was created by accumulating a series of
> other numbers into it - the other was entered directly. My debugger
> says that they're both '630.24', and when I print them, they both
> print as '630.24'. However, the == test fails and the != test
> succeeds. I can only think of taking their difference and checking if
> that's less than something like 0.001. Is there a better way?
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs