Search code examples
awkintegerroundinglogarithmnatural-logarithm

int() function in awk changes the argument even if this is integer already


I don't understand the following two results in awk

$ awk 'BEGIN {
  print    (log(.01)/log(10))
  print int(log(.01)/log(10))
}'
-2
-1

Version:

$ awk --version 
GNU Awk 5.0.1, API: 2.0 (GNU MPFR 4.0.2, GNU MP 6.1.2)

Solution

  • There is more than meets the eye:

    $ awk 'BEGIN {
      printf "%.16f\n", log(.01)/log(10)
    }'
    -1.9999999999999996