0.30000000000000004 icon indicating copy to clipboard operation
0.30000000000000004 copied to clipboard

awk example is deceptive

Open gromnitsky opened this issue 2 years ago • 0 comments

$ awk --version | head -1
GNU Awk 5.1.1, API: 3.1 (GNU MPFR 4.1.0-p13, GNU MP 6.2.1)

The example indeed prints 0.3, but 0.1 + 0.2 != 0.3:

$ awk 'BEGIN { print 0.1 + 0.2 }'
0.3
$ awk 'BEGIN { print (0.1 + 0.2) == 0.3 }'
0

It's because

$ awk 'BEGIN { printf "%0.25f", 0.1 + 0.2 }'
0.3000000000000000444089210
$ awk 'BEGIN { printf "%0.25f", 0.3 }'
0.2999999999999999888977698

gromnitsky avatar Jan 17 '23 19:01 gromnitsky