Exception when casting doubles to ints

Peter Teichmann peter at peter-teichmann.de
Fri Jan 7 13:25:31 PST 2005

Am Donnerstag, 6. Januar 2005 09:50 schrieb alan buckley:
> It appears that when a double that is larger than the maximum
> int is cast to an int a floating point exception occurs. On cygwin
> you do not get an exception, and as the code where I found the
> problem was in lua I suspect other linux/unix variations also do not
> raise an exception. Can anyone confirm the unix behaviour?
> If this is the case should the RISC OS gcc match what unix does?

Floating point exceptions were also one problem when I was porting Quake to 
RISC OS. It turned out that on virtually all other OSes all FP exceptions are 
not enabled by default, but were on RISC OS.

> The only thing I'd say in defence of this behaviour is that you can cast an
> oversize int to a char and not get an exception so why should floats be
> different.

Floats are different because they follow the IEEE Standard 754-1985, where an 
overflow exception is raised if the result does not fit into the destination 
operands size. The IEEE Standard 754-1985 states that the default response to 
an exception shall be to proceed without a trap for all floating point 
exceptions (page 10, chapter 7). Even for divide-by-zero, and that is the 
smallest problem because there is a representation for infinity in the 
float/double FP formats. Try it out by yourself by dividing 1.0/0.0 in a 
Linux C program and print the result (+inf). Try even 0.0/0.0 (NaN).

Probably Unixlib should be changed to follow the Standard. I am wondering that 
SharedCLib does not follow this standard either...

Peter Teichmann

More information about the gcc mailing list