Very odd error in looping - possible bug in AWK maths library?

Discussion in 'Mac Programming' started by VulchR, Dec 17, 2012.

  1. VulchR, Dec 17, 2012
    Last edited: Dec 17, 2012

    macrumors 68000

    VulchR

    Joined:
    Jun 8, 2009
    Location:
    Scotland
    #1
    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
     
  2. macrumors newbie

    Joined:
    Oct 21, 2006
    Location:
    Lambeth
    #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);
    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.
     
  3. macrumors 6502

    Joined:
    Mar 8, 2004
    #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.
     
  4. ytk
    macrumors regular

    Joined:
    Jul 8, 2010
    #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
    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.
     
  5. thread starter macrumors 68000

    VulchR

    Joined:
    Jun 8, 2009
    Location:
    Scotland
    #5
    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:
     

Share This Page