I'm trying to write what I expected to be a reasonably
simple awk routine to generate a list of numbers as part
of a larger bash shell script (since I'll need this done for
many different files).
For example:
echo | awk '{
x = 0.0; y = 0.0;
do {
printf("%.1f\n",x)
if ( x < 3.0 ) { y = 0.3 }
else if ( x < 6.0 ) { y = 1.0 }
else if ( x < 15.0 ) { y = 3.0 }
else if ( x < 25.0 ) { y = 5.0 }
else { y = 10.0 }
x = x + y;
} while ( x < 55.0 )
}'
I expected this to produce this list of numbers:
0 0.3 0.6 ... 2.4 2.7 3.0 4.0 5.0 6.0 9.0 12 15 20 25 35 45
55
what I get is:
0 0.3 0.6 ... 2.7 3.0 3.3 4.3 5.3 ...
when x = 3 in the loop (printed x after each "if"
conditional) the comparison seems to fail. i.e. ( x < 3 )
returns true, even when x = 3, and y remains equal to 0.3
for one iteration longer than it should ( as if I used "<="
instead of "<" ). I manage to get the right results if I
initialize x = 0.0001 and use printf("%.1f\n",x). Fine, but
why is this necessary? (Its terribly unsatisfying).
I found post 8067
(http://www.computing.net/unix/wwwboard/forum/8067
.html) which suggests it might be because awk treats the
value of x as a string, but I tried adding 0 to x ( x += 0; or
x = x + 0 ) and the results are the same.
I've tried every variation I can think of and searched a
number of forums (but my question is so general I'm not
sure I know how to search for it). I think I even has a case
where ( x < 3 ) did not work inside the "do" loop but
( x < 55 ) did work as the while condition.... but I haven't done
that again.
Can anyone explain why my " x < 3 " is acting like
" x <= 3 "? I would greatly appreciate your help (and I might be
able to move on and get more work done).
Thank you!