I'm trying to cast a double
to an unsigned int
. The double
is guaranteed to be >= 0. Different compilers yield different results, though. Here is the example:
#include <stdio.h>
int main(int argc, char *argv[])
{
double x = 5140528219;
unsigned int y = (unsigned int) x;
printf("%x\n", y);
return 0;
}
Visual C seems to simply kill all bits >= 32 because it converts the double
to 0x32663c5b. Gcc, however, seems to clip the whole number to UINT_MAX
because the result is 0xffffffff.
Now there are some threads that mention compiler bugs in Visual C when it comes to converting double
to unsigned int
so I was wondering whether the behaviour I'm seeing here is a bug in Visual C as well or whether the conversion of double
to unsigned int
is just implementation dependent and thus undefined?
Any ideas?
My Visual C version is quite old (15.00.30729.01 for x64).