Comparing a double with another calculated double fails on 32Bit Linux only

Hi,

Why does the following two "if"-statements result in two different behaviors (on Linux 32Bit only).

On Windows (with MS-Compiler) or on Linux 64Bit both if-statements jumps to the else case, like expected.

But if the following code will be compiled with "g++ -m32 ..." the second "if"-statement does not jump to the else case.

#include <iostream>
int main( int argc, char* argv[] )
{
double BaseValue = 1.0;
double Delta = 0.3;
double Value = 1.3;

double BasePlusDelta = BaseValue + Delta;

std::cout << "precalculated condition: ";
if( Value > BasePlusDelta )
{
std:: cout << "condition result == true " << std::endl;
}
else
{
std:: cout << "condition result == false " << std::endl;
}

std::cout << std::endl;

std::cout << "direkt calculated condition: ";
if( Value > BaseValue + Delta )
{
std:: cout << "condition result == true " << std::endl;
}
else
{
std:: cout << "condition result == false " << std::endl;
}
}

Hint:
Looking at the generated assembler code it seems, that:
On 32Bit Linux the Floating Point unit will be used.
On 64Bit Linux the SSE2 Unit will be used.
Since I am not an assembler programer this may be a missinterpretation.



Floating point math is not what you intuitively expect it to be. The machine can store only discrete values, so there is a lot of rounding, and different systems use different precision. Do not expect the 0.3, 1.0 and 1.3 be exactly those values.

Temporary values may be pushed to memory, or kept in register that might store more digits.
Thank you for your answer.

It is clear that i can't expect same results on different systems.

In my case I do the same calculation with identical input variables on the same system.

The only difference is that:

1. case: store the calculation result in a variable and use this variable in the folowing if-statement

2. case: do the same calculation directly inside the if-statement

Since all used variables are double-values no type conversion should happen

So the question is:
Does only the assignment of the calculation result to an variable manipulate the result itself?
Topic archived. No new replies allowed.