Awk – High Precision Arithmetic in Awk

arithmeticawkfloating point

I am looking for a way to tell awk to do high-precision arithmetic in a substitution operation. This involves, reading a field from a file and substituting it with a 1% increment on that value. However, I am losing precision there. Here is a simplified reproduction of the problem:

 $ echo 0.4970436865354813 | awk '{gsub($1, $1*1.1)}; {print}'
   0.546748

Here, I have a 16 digit after decimal precision but awk gives only six. Using printf, I am getting the same result:

$ echo 0.4970436865354813 | awk '{gsub($1, $1*1.1)}; {printf("%.16G\n", $1)}'
0.546748

Any suggestions on to how to get the desired precision?

Best Answer

$ echo 0.4970436865354813 | awk -v CONVFMT=%.17g '{gsub($1, $1*1.1)}; {print}'
0.54674805518902947

Or rather here:

$ echo 0.4970436865354813 | awk '{printf "%.17g\n", $1*1.1}'
0.54674805518902947

is probably the best you can achieve. Use bc instead for arbitrary precision.

$ echo '0.4970436865354813 * 1.1' | bc -l
.54674805518902943
Related Question