Search code examples
awkprecision

awk and numeric comparison with number less than 1.7e-308


The following command

echo 1.8e-308 | gawk '$1<0.05'

produce no output, while this one

gawk 'BEGIN{if(8.2547e-309<0.05){print "true"}}'

print "true".

This answer explain why the first command produce no otuput.

A possible workaround is

echo 1.8e-308 | awk '$1+0 < 0.05 {print}'

My question is: there is a better solution? For example some wrapper of awk that allow not to modify each script.

The optimal solution possibly would not require to recompile awk to use mpfr library.


Solution

  • Try the following:

    echo 1.8e-308 | gawk '($1+0)<0.05'
    

    produces:

    1.8e-308
    

    Some other interesting observations:

    echo "True" | gawk '1.8e-308<0.05'
    

    gives

    True
    

    and

    echo "True" | gawk '1.8e-322<1.9e-322'
    

    gives

    True
    

    whereas

    echo "True" | gawk '1.8e-323<1.9e-323'
    

    gives nothing..