lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


At 2001-12-13 19:36:12+0000, RLake@oxfam.org.uk writes:

> By contrast, with 2's complement representation of integers, all normal
> arithmetic operations are closed (except for division by 0) BUT sometimes
> the result is wildly incorrect. (And according to the C standard and
> contrary to popular belief, unpredictably so, even if many programs do
> assume that the results will be in some modulus or other.)

This is not the case for unsigned integral types.  The C standard says:

"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." (6.2.5)

and 

"if the new type is unsigned, the value is converted by repeatedly
adding or subtracting one more than the maximum value that can be
represented in the new type until the value is in the range of the new
type." (6.3.1.3).

It is true for signed integral types (behaviour on underflow or
overflow is implementation-defined).

Nick Barnes