erikwiffin / 0.30000000000000004

Floating Point Math Examples

Home Page:https://0.30000000000000004.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

awk example is deceptive

gromnitsky opened this issue · 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