Thread: spontaneous round-off type error in loop

  1. #1
    jdmikros
    Guest

    spontaneous round-off type error in loop

    Hello, looking for some help with some peculiar behavior. Check out the code and output below. BTW, I am on a Win2k box using MinGW to compile.

    Code:
    =======
    .
    .
    .
    float i=0.0,j=0.0;
    .
    .
    .

    for (j=0.0; j<=0.01;j=j+.001 )
    {
    // fprintf(FID, "%6.3f", j);
    fprintf(stderr, "\n%.14f",j);
    }
    .
    .
    .

    Output
    =====

    0.00000000000000
    0.00100000004750
    0.00200000009499
    0.00300000002608
    0.00400000018999
    0.00500000035390
    0.00600000051782
    0.00700000068173
    0.00800000037998
    0.00900000054389


    Where is the .0004750 error coming from on the 1st time through the loop? The problem is that this is kicking me out of my for loop early.

    Thanks in advance,
    Jesse

  2. #2
    jdmikros
    Guest

    Spontaneous error (same as below w/ code tags)

    Hello, looking for some help with some peculiar behavior. Check out the code and output below. BTW, I am on a Win2k box using MinGW to compile.

    Code:
    =======
    Code:
    .
    .
    .
    float i=0.0,j=0.0;
    .
    .
    .
    
    for (j=0.0; j<=0.01;j=j+.001 )
    {
         // fprintf(FID, "%6.3f", j);
         fprintf(stderr, "\n%.14f",j);
    }
    .
    .
    .
    Output
    =====
    [code]
    0.00000000000000
    0.00100000004750
    0.00200000009499
    0.00300000002608
    0.00400000018999
    0.00500000035390
    0.00600000051782
    0.00700000068173
    0.00800000037998
    0.00900000054389
    [/code/]

    Where is the .0004750 error coming from on the 1st time through the loop? The problem is that this is kicking me out of my for loop early.

    Thanks in advance,
    Jesse

  3. #3

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    I tried your code on my machine and got the same results. But, using type double instead of float got rid of that extra stuff. You may still need to alter the condition of the for loop because some numbers just cannot be represented with 100% accuracy in the binary format that they get stored as in a computer.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    jdmikros
    Guest

    got it

    howdy,

    thanks for the quick replies.

    I downloaded that pdf, and will take a look at it later.

    I usually use doubles, and am using them now on this program per your advice.

    thanks again!

    later,
    Jesse

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM