I’ve found an interesting case where the same C++ code yields different results on different system.
#include <cstdio>
int main()
{
int a=20, b=14;
if(a*1.0/b*(a+1)/(b+1)==2) printf("YES!");
else printf("NO!");
}
Compiled on Ubuntu Linux 12.04 using GCC 4.6.3 it outputs YES!
Compiled on Windows 7 using GCC 4.6.2 it outputs NO!
However, using:
double c = a*1.0/b*(a+1)/(b+1);
if (c==2) printf("YES!");
...
will return YES! on both machines.
Any ideas why this difference emerges? Is this caused by compiler version mismatch (pathlevel version number shouldn’t matter THAT much)? And why does it actually output NO! on the Windows machine, while this condition is obviously true?
This is only a guess, you would need to look at the assembly output from the compiler to know for sure.
It is possible that one compiler left intermediate results in a floating-point register while the other wrote the results to memory, rounding it from 80 bits to 64. It’s also possible that one uses SSE and the other does not.