olala
olala

Reputation: 4456

Floating point calculation difference between inside perl and inside shell

I'm very puzzled by this. Here is what I encountered.

I wrote a perl code to calculate the difference of two numbers and based on whether the difference is above 0.5, it decides whether to print that line or not.

To give a toy example:

a   0.9 0.4
b   0.8 0.2
c   0.7 0.3
d   0.81 0.3
e   0.79 0.3

So the code is supposed to print out line a,b,d but not c,e. I have tried two ways of doing this:

1.Directly test if the difference is greater than or equal to 0.5 and if so, print:

chomp;
my @array = split/\t/;
my $diff = $array[1] - $array[2];
if ($diff >=0.5) {
    print $_;
 }

2.Output the difference first into a file and then use awk to select:

 print $_,"\t",$diff,"\n";

Then with awk:
awk '{ if ($NF >= 0.5) print}' file 

I was surprised to find that the two ways generated two different results for my actual file(million of lines). The awk way gave slightly more results. I looked at where the two outputs differ and it seems using all perl way (1) have missed some lines.

Does anyone know why this is the case? Is there something wrong with doing this calculation and conditional testing in perl?

Upvotes: 2

Views: 204

Answers (1)

WayneB
WayneB

Reputation: 19

You might want to look at the Math::BigFloat Perl module, look at the methods related to precision. Can you supply the input values for a few of the lines that give different results between your perl script and the awk script?

Upvotes: 1

Related Questions