Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange output while comparing engineering numbers in AWK

Tags:

bash

awk

In my script, I need to compare engineering numbers and keep the ones that are above my threshold. My input file:

8.63384676823803E-1439,1.59426879513427E-1589,6.30982615035767E-2151,2.28965973937211E-8,7.2400080082093E-470

My code is:

b=5; awk -v var=$b -F "," '{a=$var; if (a >= 1E-10) {a=$var} else {a=0}; print a}' inputfile

However, for b = 5 (or 1/3/4) I get a = 7.2400080082093E-470, whereas plain comparison awk 'END {if ( 7.2400080082093E-470 >= 1E-10) print "wrong"}' inputfile seems to work fine.

Thanks for any tips.

like image 286
MirrG Avatar asked Mar 12 '26 06:03

MirrG


1 Answers

Your numbers are just too large (or small) for the AWK to understand them.

With GNU AWK you can use an arbitrary precision integer extension, as activated with -M command line switch to overcome that.

As of specific mechanism of why your comparison with a variable fails.

When it gets a number too large/small to understand, it treats it like a string instead.

%b=5; awk -v var=$b -F "," '{ a=$var; print a; }' inputfile 
7.2400080082093E-470

Then a lexicographical comparison of "7.2400080082093E-470" > 1E-10 yields "true"

(now let's forcibly convert it to number)

%b=5; awk -v var=$b -F "," '{ a=$var+0; print a; }' inputfile 
0

Numeric comparison "0 > 1E-10" yields "false", and it is the same thing that happens if you specify a big integer literal by hand (i.e. print 7.2400080082093E-470 --> 0).

With the arbitrary-precision arithmetics enabled:

b=5; awk -M -v var=$b -F "," '{ a=$var+0; print a; }' inputfile 
7.24001e-470
like image 116
zeppelin Avatar answered Mar 15 '26 01:03

zeppelin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!