Hello
In the awk command, the result of a floating point calculation such as echo 2.3 | awk'($ 1 * 100)% 10 == 0'`` `does not match the exact calculation (this condition is`
$ 1 * 100is not
230``` but false).
$ echo 2.3 | awk '($1*100)%10==0'
$
$ diff -u <(seq 2 .1 3) <(seq 2 .01 3 | awk '($1*100)%10==0')
2
2.1
-2.2
-2.3
2.4
2.5
2.6
$
The same is true for Python, so it doesn't seem to be a problem only with awk.
$ diff -u <(seq 2 .1 3) <(seq 2 .01 3 | python3 -c 'import sys; [print(i, end="") for i in sys.stdin if (float(i)*100)%10==0]')
2
2.1
-2.2
-2.3
2.4
2.5
2.6
$
I devised a way to write the condition as int ($ 1 * 100 + 0.5)% 10 == 0
, and in this example, a match was obtained.
$ echo 2.3 | awk 'int($1*100+0.5)%10==0'
2.3
$ diff -q <(seq 2 .1 3) <(seq 2 .01 3 | awk 'int($1*100+0.5)%10==0')
$
Recommended Posts