Weird output simple program.

This is a discussion on Weird output simple program. within the C++ Programming forums, part of the General Programming Boards category; If i run: Code: #include <iostream> using namespace std; int main() { for (float x = 0; x < 10; ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Weird output simple program.

    If i run:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    
    for (float x = 0; x < 10; x+=0.1 ) {
        cout<< x <<endl;
        
        
    }
        cin.get();
        
        
    }
    It works fine until it gets to 7.7 then it runs:

    7.5
    7.6
    7.7
    7.79999
    7.89999
    7.99999
    8.09999
    8.2
    8.3

    Can anyone explain to me what the problem is?
    As you can see I'm just getting started.

    Thank you

  2. #2
    1479
    Join Date
    Aug 2003
    Posts
    253
    Use double instead of float....read up on the differnces.
    Knowledge is power and I want it all

    -0RealityFusion0-

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    The problem is that floating point numbers can't always be represented exactly in binary, and so small errors creep in. 0.1 is such a number, since 1/10 doesn't have an exact binary representation (0/2 + 0/4 + 0/8 + 1/16 + 0/32 + 1/64 + 1/128 + ...). So really you are adding 0.99999999999 (or something like that) each time, and eventually it throws off the result enough to show up in the output.

    The same problem will eventually appear if you use double, although you should be using double anyway for normal floating-point operations.
    Last edited by Daved; 11-02-2005 at 02:48 PM.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    lol on an interesting note fiddling with .net they somehow eliminated that error as:

    Code:
    // This is the main project file for VC++ application project 
    // generated using an Application Wizard.
    
    #include "stdafx.h"
    
    #using <mscorlib.dll>
    
    using namespace System;
    
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    
    {
    	Decimal x;
    for (x = 0; x < 10; x = x + 0.1 ) {
    	Console::WriteLine("{0}\n",Convert::ToString(x));
        
        
    }
        cin.get();
        
        
    }

    fixes it and has no error at all. though mind you this is purely .net so you dont get confused but i found that interesting that they were able to eliminate the error of floating points

    and since you are just getting started keep in mind that isnt standard C++ this is just a microsoft compiler and we happen to use it in school. just so you dont get confused.
    hooch

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    That didn't eliminate the problem, it just hid it. Simply changing the original code to use double will hide the problem in the same way.

    I'm assuming Decimal has greater precision than float, just as double has greater precision than float. That means that the error doesn't show up with such a small sample size.

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    i dont know i just remeber my teacher saying it was REALLY REALLY REALLY REALLY accurate. though i had never used it cause im too stubborn to type out Console::WriteLine vs cout or printf and ultimately want to get into game design at least i hope too anyway eventually so i find it useless. but even if i dont its fairly simple to pick it back up if need be.

    in other words i find regular C++ to be of more value is all.
    hooch

  7. #7
    1479
    Join Date
    Aug 2003
    Posts
    253
    Quote Originally Posted by ssjnamekultimately
    want to get into game design at least i hope too anyway eventually so i find it useless. In other words i find regular C++ to be of more value is all.
    If you want to go into game design your in the wrong place. There is a BIG difference in game design and game programming. For game design you will graduate with some degree in Art and take 3....maybe 4 courses for programming. The rest is art classes and animation.
    Last edited by RealityFusion; 11-03-2005 at 01:10 PM.
    Knowledge is power and I want it all

    -0RealityFusion0-

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    255
    oh well i always thought they were similar because they work in the same field. but of the two graphics vs programming i prefer the programming aspect cause i know theres two different groups there in games. i just ment game design as in making a game in whatever aspect it is programming, designing levels whatever you get the point. i just kind of find it easier to say oh game design to cover it all now.

    i didnt think game design was another word for animations is all
    hooch

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program looping with final output
    By hebali in forum C Programming
    Replies: 24
    Last Post: 02-28-2008, 10:58 AM
  2. Basic C input output program help
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 01-27-2008, 06:41 PM
  3. Help with small simple program
    By Sonor in forum C Programming
    Replies: 5
    Last Post: 04-02-2005, 07:40 AM
  4. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  5. sorting output on student info program
    By indigo0086 in forum C++ Programming
    Replies: 2
    Last Post: 11-05-2002, 11:29 AM

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