1

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)
Cyrus
  • 84,225
  • 14
  • 89
  • 153
Enlico
  • 23,259
  • 6
  • 48
  • 102
  • even though the mathematical result is an integer, the value `log(.01)` is for sure a real as it represents the natural logarithm. On top of that, there are floating-point approximations which make `log(0.01)` to be slightly different numerically then mathematically. Dito for `log(10)`. Ergo, the division will not be perfect. – kvantour Oct 10 '19 at 09:35
  • Possible duplicate of [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – kvantour Oct 10 '19 at 09:35

1 Answers1

4

There is more than meets the eye:

$ awk 'BEGIN {
  printf "%.16f\n", log(.01)/log(10)
}'
-1.9999999999999996
James Brown
  • 36,089
  • 7
  • 43
  • 59