For loop in awk, a bug or something else?

Seems for loop in awk has bug?

For first awk, i expect to get 4 output: 1.5, 1.4, 1.3, 1.2, but there are only 3.

$ awk 'BEGIN {for (i=1.5;i>=1.2;i-=0.1) print i}'
1.5
1.4
1.3

$ awk 'BEGIN {for (i=15;i>=12;i-=1) print i}'
15
14
13
12

It is not a bug, it has to do with floating points. The are not as precise as you might think, due to the way they are represented.
That's why you shouldn't use them as a control of a loop.

1 Like