How should C++ handle overflow when casting large floating-point numbers to integers? I'm asking this because the code below shows some inconsistency when compiled with Visual C++ 2005:
void main()
{
float x = 1e20;
float y = 1e10;
float z = 1e5;
__int64 a = x;
__int64 b = y;
int c = y;
short d = y;
short e = z;
}
On my system:a = -9223372036854775808 (= 0x8000000000000000)
b = 10000000000
c = 2147483648 (= 0x80000000)
d = 0
e = -31072 (= 0x86A0)
Looking at the assembly code, the last three casts use SSE2 (cvtpd2si). It is defined to return 0x80000000 on overflow. But for the short int it just takes the lower 16-bit of this value. I've tried to look for a detailed C++ reference about float to int casting but couldn't find anything so far. My best guess is that this should cause an exception, but this exception is typically masked, so the resulting value is undefined. :surrender
Thoughts?
Nick












