I am surprised with behaviour of awk
while performing floating point calculations. It lead me to wrong calculation on table data.
$ awk 'BEGIN {print 2.3/0.1}'
23 <-- Ok
$ awk 'BEGIN {print int(2.3/0.1)}'
22 <-- Wrong!
$ awk 'BEGIN {print 2.3-2.2==0.1}'
0 <-- Surprise!
$ awk 'BEGIN {print 2.3-2.2>0.1}' <-- Din't produce any output :(
$ awk 'BEGIN {print 2.3-2.2<0.1}'
1 <-- Totally confused now ...
Can somebody throw light as to what's happing here?
EDIT 1
As pointed by @fedorqui, output of second last command goes to file named 0.1 because of redirection operator (>).
Then how am I supposed to perform greater than (>) operation?
Solution to it is also given by @fedorqui
$ awk 'BEGIN {print (2.3-2.2>0.1)}'
0 <-- Wrong!
The following section from the manual should help you understand the issue you're observing:
15.1.1.2 Floating Point Numbers Are Not Abstract Numbers
Unlike numbers in the abstract sense (such as what you studied in high school or college arithmetic), numbers stored in computers are limited in certain ways. They cannot represent an infinite number of digits, nor can they always represent things exactly. In particular, floating-point numbers cannot always represent values exactly. Here is an example:
$ awk '{ printf("%010d\n", $1 * 100) }' 515.79 -| 0000051579 515.80 -| 0000051579 515.81 -| 0000051580 515.82 -| 0000051582 Ctrl-d
This shows that some values can be represented exactly, whereas others are only approximated. This is not a “bug” in awk, but simply an artifact of how computers represent numbers.
A highly recommended reading:
What every computer scientist should know about floating-point arithmetic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With