Re: Integer arithmetic, multiplication overflow



On Fri, 10 Jun 2005 01:49:45 +0000, Timothy Little wrote:

> Tomek wrote:
>> For example in case of addition it's simple:
>> A+B > X => A+B overflows.
>> A+B - X = X - ((X-A) + (X-B)) - and all the terms can be
>> computed without overflow.
>
> I can see why (X-A) and (X-B) will never overflow, but how does this
> ensure that their sum doesn't overflow before you subtract it from X?
> Are you proposing to first check whether A+B would overflow, and only
> compute A+B - X if A+B > X?
>
>
> - Tim

Of course - this is useful only if we first check if A+B overflows.
The thing I was in particular wondering about was how to construct
general (X+1)-based integer operations having some working b-based
arithmetic, in which X is the biggest number. I mean - using only basic
arithmetic operations from b-based system, no bit shifts, tricks and so.
Schrager's algorithm, as suggested by Keith Duggar is helpful, but I am
not sure if it is enough - and actually, I have stopped wondering
and started to do the bits shifts ... :)

- Tomek


.



Relevant Pages

  • Re: Integer arithmetic, multiplication overflow
    ... Tomek wrote: ... I can see why (X-A) and (X-B) will never overflow, ...
    (sci.math)
  • How to handle SIGABRT without stopping the code
    ... I'm a newbie with signals and I need a little help. ... I have a program that does arithmetic operations among integers and I ... want to check when overflow happens. ... handler function but this topic is not so easy to manage for a newbie ...
    (comp.unix.programmer)
  • Re: How to handle SIGABRT without stopping the code
    ... I have a program that does arithmetic operations among integers and I ... want to check when overflow happens. ... If you're on Linux, a good source of information on these issues is the ... Read the chapters on "Signal handling" and ...
    (comp.unix.programmer)
  • Re: Arithmetic and C
    ... >> the C behave with the arithmetic operations? ... >If int can represent all of the values of unsigned short, ... However, overflow is very unlikely to happen from a + b, if a and b ... Dan Pop ...
    (comp.lang.c)
  • Re: How to handle SIGABRT without stopping the code
    ... I have a program that does arithmetic operations among integers and I ... want to check when overflow happens. ... the program stops? ... In other words, if I understood correctly, how can I build a handler ...
    (comp.unix.programmer)