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.