Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

VulchR

macrumors 68040
Original poster
Jun 8, 2009
3,373
14,242
Scotland
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 :eek:
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
 
Last edited:

boffo

macrumors newbie
Oct 21, 2006
16
0
Lambeth
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);

which prints out

Code:
0.01000000000000000000
0.02000000000000000000
0.02999999999999999900
0.04000000000000000100
0.05000000000000000300

You can see that the next value would be greater than 0.06, and so the x <= 0.06 test would fail.

Instead of your second example:

Code:
for (x=0.01;x<=0.06;x+=0.01)

you are better off doing something like

Code:
for (x = 1; x <= 6; x += 1)
  actual_x = x / 100.0

I'm not familiar enough with awk to try this for you but something along these lines should work.

The standard reference on this kind of thing is

What Every Computer Scientist Should Know About Floating-Point Arithmetic.
 

Mac_Max

macrumors 6502
Mar 8, 2004
404
1
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.
 

ytk

macrumors 6502
Jul 8, 2010
252
5
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

Both Python and Ruby will return false. Additionally, Python by default doesn't round off floating point numbers, so you'll find that 0.05 + 0.01 actually returns 0.060000000000000005.

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.
 

VulchR

macrumors 68040
Original poster
Jun 8, 2009
3,373
14,242
Scotland
C...
you are better off doing something like

Code:
for (x = 1; x <= 6; x += 1)
  actual_x = x / 100.0

I'm not familiar enough with awk to try this for you but something along these lines should work.

The standard reference on this kind of thing is

What Every Computer Scientist Should Know About Floating-Point Arithmetic.

This doesn't surprise me too much and I don't think it's an actual bug. ...

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.

This is how floating point numbers work...

Thanks to all of you for your helpful replies. I think my error was to assume that the floating point representation used an exponent in base 10 (after reading it seems that the IEEE standard is base 2, which of course makes more sense). That's why the loop doesn't work with 0.06 but does with 0.6. Sigh ... been computing for 30 years, but feel like a noob... :eek:
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.