Thread: Very confused over floating points in if-statements

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    12

    Very confused over floating points in if-statements

    Thanks in advance for your time.

    I was working with GLUT, and it was going well until for some inexplicable reason, my logic statement kept failing to... well, to be logical.

    I rewrote it for all intents and purposes in a stripped-down program just to test the block itself (as below) and it ends up failing to test true ( x == 1.0f ) on the 10th iteration as it (I think?) should be doing.

    I'd appreciate any insight on this, it's confused the heck out of me.

    Code:
    #include <iostream>
    using namespace std;
    
    int main( void )
    {
    	float x = 0.0f;
    	for ( float c = 0.0; c < 1; c += 0.1f )
    	{
    		x += 0.1f;
    		cout << "x is: " << x << endl;
    
                    // This doesn't test true, even when x == 1
    		if ( x == 1.0f )
    			cout << "X hit the contingency.\n";
    		else cout << "X has not yet hit.\n";
    	}
    
    	system("PAUSE");
    	return 0;
    }
    If you have any thoughts, please don't hesitate to post them. Thanks again.

  2. #2
    Registered User
    Join Date
    Jun 2006
    Location
    Hungary
    Posts
    4
    Well, try using double rather than float. Float is quite inaccurate. That can be the reason.

    Double is twice as accurate as float is.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Logical operators on floating types are something you should avoid . Unfortunately computers have an hard time dealing with these types.

    One solution here is to cast when testing it.

    Code:
    if ( static_cast<int>(x) == 1 ) // instead of if ( x == 1.0f )
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    12
    Ok. Thanks again to you both.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    you could try:

    Code:
    for ( float c = 0.0; c != 1; c += 0.1f )
    Some people recommend it.

    JOKE! PLEASE DON'T CONTINUE THE DEBATE!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >// This doesn't test true, even when x == 1
    >if ( x == 1.0f )
    Floating-point comparisons for equality are typically doomed to failure due to the imprecision of floating-point. This problem isn't unique to C++; it has to do with the representation of floating-point values. To compare two floating-point values, you do so with a fudge factor:
    Code:
    const double fudge = .01; // Or whatever is suitably "small" enough
    if ( fabs ( x - 1.0f ) <= fudge )
      // Equal (or close enough)
    My best code is written with the delete key.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Double is twice as accurate as float is.
    Not nessesarily. A double is at least the size of a float, and if one is larger, it will be the double. (floats have at least 6 digis of accuracy.)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. floating point number comparison
    By lehe in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2009, 07:11 PM
  2. Testing Floating Point Return Values
    By jason_m in forum C Programming
    Replies: 5
    Last Post: 08-15-2008, 01:37 PM
  3. Why so much trouble with floating points?
    By darketernal in forum C++ Programming
    Replies: 14
    Last Post: 06-09-2007, 05:16 AM
  4. CProg Fantasy Football version pi
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 155
    Last Post: 12-26-2006, 04:30 PM
  5. Efficiency of case statements
    By Yasir_Malik in forum C Programming
    Replies: 26
    Last Post: 05-23-2006, 11:36 AM