<>Robert's solution (rounding with sprintf) is pretty good, except it requires that you
know something about the numbers. For example, they must differ by more than 0.01 to
be considered different. What happens when the two numbers are;
  0.00001000001
  0.00001000000
Now you need to check for differences beyond 0.00001, actually  0.00000000001

Perhaps a better way, one that doesn't require any knowledge of the range of the numbers,
is to check the ratio of the two numbers. This should eliminate (or reduce) the dependence
on their magnitudes. So, instead of  comparing the original numbers (or the rounded
numbers) compare their ratio;
    abs(1-$sum1/$sum2) <= 1e-12
If the numbers are "equal", the ratio will be something like 0.9999999999 or 1.0000000001,
depending on which of the two numbers is bigger. The abs and "1-" removes the dependence
on which of the two numbers is bigger, and shifts the value to near zero, giving a single
result such as 0.0000000001.
 
On my Winblows 98 machine the difference between the two numbers is -2.8421709430404e-014
So a cutoff value of 1e-12 is probably suitable. Other platforms may need a different cutoff value
depending on their native precision. I suspect 1e-10 might be a good, platform independent, value.

use strict;  ## added

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");
print "Difference =".($sum1-$sum2)."\n";      ## what is the actual diff between the numbers?
print "Ratio      = ".($sum1/$sum2)."\n"; 
print "(1-Ratio)  = ".(1-$sum1/$sum2)."\n\n";

if ( abs(1-$sum1/$sum2) <= 1e-12 ) {  ## new test
    print("EQUAL\n");
} else {
    print("NOT EQUAL\n");
}

-----------------------------------------
OUTPUT:
sum1 = -237.15
sum2 = -237.15
Difference =-2.8421709430404e-014
Ratio      = 1
(1-Ratio)  = -2.22044604925031e-016

EQUAL


J. Sluka
InPharmix Inc.


robert wrote:
use "sprintf" to set the floating point field to 2 decimal places. (or
more, if you want them...)  

$float1=-135.176		# final values before rounding
$float2=-135.184		

$float1=sprintf("%.2f",$float1);    # force $float1 to be rounded at 2
decimal places
$float2=sprintf("%.2f",$float2);    # ditto $float2

print "$float1\n$float2\n";

if ($float1 == $float2) {
	print "   values are EQUAL\n";
}
else { print "   values are NOT EQUAL\n"; }


the example above will return

-135.18
-135.18
   values are EQUAL

change 
$float1 = 255.733
$float2 = 255.735

and this will return

255.73
255.74 
   values are NOT EQUAL


	

  
-----Original Message-----
From: John Deighan

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