|
|||||||
![]() |
|
|
Thread Tools | Search this Thread | Display Modes |
|
|
#1 |
|
Very odd error in looping - possible bug in AWK maths library?
I use AWK (C-like programming language used to parse tables - and, yes, I know that's primitive of me). Check out the following behaviour
for (x=0.01;x<=0.05;x+=0.01) Stops at 0.05 as expected for (x=0.01;x<=0.06;x+=0.01) Stops at 0.05 ![]() for (x=0.01;x<=0.06000001;x+=0.01) Stops at 0.06 as expected for (x=0.1;x<=0.6;x+=0.1) Stops at 0.6, as expected for (x=0.01;x<=0.10;x+=0.01) Stops at 0.10 as expected and steps through 0.06 just fine Possible maths library bug? Anybody else able to confirm this perhaps with C (I am not sure, but I presume AWK uses C's maths libraries)? Mac OS X 10.8.2 iMac 27" i7
__________________
My first was a Mac+. Now I own an iPhone with 3.5x the pixels, a colour display, WiFi, 512x the RAM, >1500x the data storage, and 100x the speed. And it fits in the palm of my hand.
Last edited by VulchR; Dec 17, 2012 at 11:39 AM. |
|
|
|
0
|
|
|
#2 |
|
Comparing floating point numbers for equality can lead to unexpected results as you have encountered here. This is because some numbers which can be precisely represented as decimals can't be represented as floating point numbers.
Reimplementing your program in C and printing the values of x with a large degree of precision gives me this: Code:
double x;
for (x = 0.01; x <= 0.06; x += 0.01)
printf("%4.20f\n", x);
Code:
0.01000000000000000000 0.02000000000000000000 0.02999999999999999900 0.04000000000000000100 0.05000000000000000300 Instead of your second example: Code:
for (x=0.01;x<=0.06;x+=0.01) Code:
for (x = 1; x <= 6; x += 1) actual_x = x / 100.0 The standard reference on this kind of thing is What Every Computer Scientist Should Know About Floating-Point Arithmetic. |
|
|
|
0
|
|
|
#3 |
|
This doesn't surprise me too much and I don't think it's an actual bug. Representing decimal fractions in binary is difficult. .5 is easy because it takes only one bit of precision after the decimal point. .06 requires much more.
If you must compare fractional values I'd suggest making sure you're using the highest precision floating point type in AWK, using a larger comparison number, and using only < rather than <=. 2^-4 is -0.0625 so that wouldn't be a bad value to try depending on the precision you need and have available. Another trick if you're reading strings and converting them to numbers is to pad the numbers with enough zeros that everything becomes an integer. Precision issues mostly go out the window at that point. |
|
|
|
0
|
|
|
#4 |
|
This is how floating point numbers work. You can verify this by running either python or irb from the command line and typing in
Code:
(0.05 + 0.01) <= 0.06 The best way to do this would be as boffo pointed out: Keep things in the integer world for comparison purposes, then only convert to floating point when actually needed for performing a calculation. |
|
|
|
0
|
|
|
#5 | ||
|
Quote:
Quote:
__________________
My first was a Mac+. Now I own an iPhone with 3.5x the pixels, a colour display, WiFi, 512x the RAM, >1500x the data storage, and 100x the speed. And it fits in the palm of my hand.
|
|||
|
|
1
|
![]() |
|
| Tags |
| awk, bug, looping |
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 09:21 AM.





was a Mac+. Now I own an iPhone with 3.5x the pixels, a colour display, WiFi, 512x the RAM, >1500x the data storage, and 100x the speed. And it fits in the palm of my hand.




Linear Mode
