[Haskell-beginners] Overflow when reading C types

Daniel Fischer daniel.is.fischer at googlemail.com
Tue Dec 3 00:14:09 UTC 2013


On Monday 02 December 2013, 18:14:20, Isaac Dupree wrote:
> C conversions from floating-point to signed or unsigned integral also 
> saturate.

Not really. Clause 6.3.1.4 is quite explicit:

"When a finite value of real floating type is converted to an integer type 
other than _Bool, the fractional part is discarded (i.e., the value is 
truncated toward zero). If the value of the integral part cannot be 
represented by the integer type, the behavior is undefined."

and adds a footnote to make it unmistakeably clear that the remaindering 
operation need not be carried out when the target type is unsigned.

> 
> Unsigned types being Z/nZ is mathematically sound, but C is not very 
> dedicated to this interpretation.

It's mandated in 6.2.5 (9),

"A computation involving unsigned operands can never overflow, because a 
result that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value that can 
be represented by the resulting type."

> Furthermore, C signed arithmetic is undefined behavior if you overflow.

That cannot be stressed too much.


More information about the Beginners mailing list