Need loop help

This is a discussion on Need loop help within the C++ Programming forums, part of the General Programming Boards category; I need help to figure out what went wrong... As I am just learning how to program this, I was ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Need loop help

    I need help to figure out what went wrong... As I am just learning how to program this, I was testing out a 'for' statment:

    Code:
    //SomeCrazyGuy
    //'For' Looping Test
    
    #include <iostream.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int main ()
    {
        float a;
        
        for(a=0;a<=1;a=a+.01) {
        cout<< a <<endl;
    }
        
        system("pause");
        return 0;
    }
    ~~~

    While the result was find for a short moment, something quite odd happened:

    0
    .1
    .2
    .3
    ...
    .81
    .82
    .83
    .839999
    .849999
    ...
    .999999
    Press any key to continue . . .

    Mainly from curiosity, but also from a need, I wish to know how this went wrong, when it went wrong, and how it can be fixed so I may avoid this in future programs.

    Thank you for any and all help you provide, it is greatly appreciated! n_n

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    It's called Round-off error.
    The decimal fraction 0.1 cannot be precisely represented in binary. It is a repeater fraction 0.00011001100110... It is like the repeater fraction 1/3 = 0.33333 in base 10.
    Round-off arises from the finite nature of the computing machine. The machine is used to represent the mathematician's number system, which uses infinitely long representations. Although round-off may be a small effect in a single computation, errors may accumulate over the incredible number of arithmetic operations that can occur in a problem requiring only a few seconds of machine time. The greatest loss of significance in results occurs when two numbers of about the same size are subtracted so that most of the leading digits cancel out. This effect can arise unexpectedly in almost any long computation.

    Read this:
    http://mathworld.wolfram.com/RoundoffError.html
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  3. #3
    Registered User Russell's Avatar
    Join Date
    May 2004
    Posts
    71
    Quote Originally Posted by SomeCrazyGuy
    Code:
    //SomeCrazyGuy
    //'For' Looping Test
    
    #include <iostream.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int main ()
    {
        float a;
        
        for(a=0;a<=1;a=a+.01) {
        cout<< a <<endl;
    }
        
        system("pause");
        return 0;
    }
    Also, when incrementing a variable, a+=.01 is suffice.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Quote Originally Posted by SomeCrazyGuy
    I am just learning how to program
    Quote Originally Posted by Russel
    Also, when incrementing a variable, a+=.01 is suffice.
    Most beginners don't start out using the op= form of assignment. Did you?

    but also from a need, I wish to know how this went wrong
    You decided to use a computer. A computer uses a binary number system(i.e base 2), which means there are only 1's and 0's for digits and they are represented on your computer chip by either an electric charge being present or no electric charge being present. Humans tend to use a decimal number system(i.e base 10) where the digits run from 0-9. Just as 1/3 can't be represented exactly using a decimal number--it's .33333....<forever> so at some point you just have to approximate it with .3333333--it's the same way with binary numbers, and some numbers can't be represented exactly.

    when it went wrong, and how it can be fixed so I may avoid this in future programs
    I don't think it is possible to fix, but you can minimize those errors, but you will need to start doing a lot of reading about the problem.
    Last edited by 7stud; 04-10-2005 at 02:56 AM.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    Quote Originally Posted by SomeCrazyGuy
    I need help to figure out what went wrong... As I am just learning how to program this, I was testing out a 'for' statment:

    Code:
    //SomeCrazyGuy
    //'For' Looping Test
    
    #include <iostream>  // Drop the ".h"
    #include <cstdlib>  // Drop the ".h" add the "c"
    
    using namespace std;
    
    int main ()
    {
        float a;
        
        for(a=0;a<=1;a=a+.01) {
        cout<< a <<endl;
    }
        
        system("pause");
        return 0;
    }
    Should make some changes to your headers.
    "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

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    So in other words, I need to find an alternative way to go about doing this?... Oh well... I'll figure out a way to continue through this when the time comes. Thanks for the help (and I am almost glad to hear that it isn't my fault)!

  7. #7
    Registered User Kirdra's Avatar
    Join Date
    Aug 2002
    Posts
    105
    I think you need to use integer type data (int/char) to iterate through loops.

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    21
    There is a way to make it look more like you intended, but it doesn't fix the intrinsic problem just hides it.

    Code:
    //SomeCrazyGuy
    //'For' Looping Test
    
    #include <iostream.h>
    #include <stdlib.h>
    
    using namespace std;
    
    int main ()
    {
        float a;
        
        for(a=0;a<=1;a=a+.01) {
        cout.precision(2);
        cout<< a <<endl;
    }
        
        system("pause");
        return 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Huh... I just thought of something... If I was to multiply each value by 1.0000009536752259018191354934077 (1/((209715/2097152)*10)), would that then negate any sort of flaws created by the flaw in this issue?... Or would the flaws creating the issues then affect that value and further flaw any output? I'm gona test this and tell you what I got...

  10. #10
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    With everything I tried, I got either the same result, or this new result:

    .04
    .05
    .0600001
    .0700001
    ...
    .0900001
    .1
    .11
    .12
    ...so on...

    ~~~

    So I suppose there is no way, beyond pliangs suggestion, to at at least mask the flaw... Thanks! n_n'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21