I've got a function f which returns a double. I'm calling that function within an if statement as follows:
if (f(...) > 0.1) cout << 1;
else cout << 0;
I've included print statements within f to display the return value. Sometimes f returns a value under 0.1 but a 1 gets printed anyway. Changing the code to:
double value = f(...);
if (value > 0.1) cout << 1;
else cout << 0;
seems to fix the problem. Unfortunately I haven't been able to consistently replicate the issue so I don't know the conditions under which this happens. Can anybody speculate? Thanks.
What compiler and version are you using and with what options are you compiling?
There were bugs in some versions of gcc where gcc would generate 32-bit floating point temporaries (floats)
into which it wanted to store a double, which meant loss of precision.