On Wed, Oct 29, 2008 at 3:16 AM, Alan McKinnon <[EMAIL PROTECTED]> wrote: > On Wednesday 29 October 2008 00:55:42 Jorge Peixoto de Morais Neto wrote: >> >> I mean to really know C, >> >> that is, read a rigorous book such as "C: A Reference Manual" and be >> >> able to write portable programs with well-defined behavior. Speaking >> >> of well-defined behavior, do you know what happens when you cast a >> >> float to an int, and the float is too big to fit into the int? >> > >> > Did oyu try it yourself and see? >> >> The point is that the behavior in this situation is "undefined". It >> might do anything. Programming in C is different than programming in >> Python. > > Most likely the compiler will try to treat the float as an int and use the > first 4 bytes of the float, ignoring the rest. Float is 4 bytes; the questions should be reworded s/float/double/g
But somehow, SSE version or fisttp (or whatever) doesn't set CF/OF on overflow, the returned int is simply (1 << 31) ^ (1 << 31 - 1) > This is insane though. I cannot think of any reason why one would ever want to > treat the first 32 bits of a float as an int. It's not like you are casting a > long to an int which can make sense - just discard the high bits. > I reckon the standard would say this is undefined. Most compiler would bomb > out with a compile error but give you an obscure flag to proceed anyway. If > you want to commit suicide, C is quite happy to pass you the pills as long s > you ask nicely gcc does this without errors Code [x86/64 only]: #include <stdio.h> #include <math.h> #include <stdint.h> int main() { double f = 1.5e99; int i = (int)f; uint16_t z; asm("pushf; popw %0;" : "=r"(z)); printf("%d %d %d\n", i, (z & (1 << 11)), (z & 1) ); return 0; } In short, your mileage shall vary, so sayeth the standard. > -- -- Andrey Vul A: Because it messes up the order in which people normally read text. Q: Why is top-posting such a bad thing? A: Top-posting. Q: What is the most annoying thing in e-mail?