lua-users home
lua-l archive

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


On 06/12/2014 19:48, Tim Hill wrote:
It’s less “ints behave like C” than “ints follow the behavior of the underlying hardware”, which these days almost always means 2’s complement with overflow. I’m really not too sure clamping is better than overflow .. at least with overflow when it happens it’s (usually) pretty obvious; clamping has greater potential to go undetected.

That is called "Saturated Arithmetic" and is common in DSP processors. In many situations it is the preferred behavior. Let's say you are implementing a PID loop to control the speed of an electric motor. Adding a small positive correction value to a large positive previous value (that will overflow) will result in: Saturated Arith will give you still a large positive value (with the wrong speed.) 2's overflow will give you a large negative value, reversing the motor from (close to) full forward speed to (close to) full reverse.
The first error is much more tolerable than the second.

See:  http://en.wikipedia.org/wiki/Saturation_arithmetic

--
Roberto Waltman