[PYTHON] Floating point calculation for the awk command

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
$

Workaround

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

Floating point calculation for the awk command
Command for the current directory Python
Ask for Pi with the bc command
Calculation result after the decimal point in Python
Floating point police 24:00
See here for the amount of free memory of the free command
Floating point numbers are not the same as decimal numbers
Install the pip command
Linux command for self-collection
Search for large files on Linux from the command line