lua-users home
lua-l archive

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


Nick Barnes escribió:

> 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).

I stand semi-corrected. The results of unsigned math are predictable. This
does not mean that a given programmer will make the correct prediction,
though. :-)

If you were expecting x + y >= x (with x and y positive), you might be in
for a surprise. Of course, someone using unsigned arithmetic would probably
be aware that the semantics of all arithmetic operators had magically
changed to modular arithmetic, and would not have made the error of using
signed constants in the expression. (I don't suppose there are that many
1's complement implementations left in the world, but you never know...)

By contrast, with IEEE 754 math, it can be arranged that :

x >= 0 and y >= 0 => x + y >= x.

This is true since Infinity is a valid IEEE 754 value. However,
implementations of C may (and some do) trap overflow on some of these
operations. If they were IEEE 754 compliant, they would give the programmer
a way of turning this behaviour off.


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

Yes, it was to that which I was actually referring. I should have been more
explicit.

R.