How to ignore/catch unsigned int calculation overflow

L

linq936

I have some algorithm dealing with unsigned int calculation a lot, I am
trying to add some check for overflow. The initial thinking was very
easy, just do something like this,

int addition(unsigned int i1, unsigned int i2) {
if ( i1 + i2 < i1 || i1 + i2 < i2 ) {
...throw some expception...
}
return i1+i2;
}

I also define a minus function doing similar thing, but for simplicity,
I only show addition functin here.

But it does not work as I expected. For example I have the following
code,

unsigned int base=0xFFFF0000
unsigned len=0x10000
unsigned int high= addition(base,len)-1;

you can see that an exception is thrown since 0xFFFF0000 + 0x10000 is
0x0, but it is what I expect.

i know I can adjust the above equation to addition(base-1, len), but
for the real code, there are a lot of really long plus and minus mixed
calculation. I guess there is no universal rule to break addition and
minus.

So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?
 
P

Pete Becker

So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?

Do it at higher precision. Use, say, unsigned long or unsigned long ong
(assuming one of them's larger than unsigned int), and if the result is
too big for an int then you've got an overflow.
 
M

Michiel.Salters

I have some algorithm dealing with unsigned int calculation a lot, I am
trying to add some check for overflow.

Tricky, as unsigned int technically doesn't overflow in C++. It's just
integer
arithmetic modulo (UINT_MAX+1).
The initial thinking was very easy, just do something like this,

int addition(unsigned int i1, unsigned int i2) {
if ( i1 + i2 < i1 || i1 + i2 < i2 ) {
...throw some expception...
}
return i1+i2;
}

I also define a minus function doing similar thing, but for simplicity,
I only show addition functin here.

But it does not work as I expected. For example I have the following
code,

unsigned int base=0xFFFF0000
unsigned len=0x10000
unsigned int high= addition(base,len)-1;

you can see that an exception is thrown since 0xFFFF0000 + 0x10000 is
0x0, but it is what I expect.

OTOH, addition(0,0)-1 doesn't cause an exception (operator- doesn't
check)
but it should.
i know I can adjust the above equation to addition(base-1, len), but
for the real code, there are a lot of really long plus and minus mixed
calculation. I guess there is no universal rule to break addition and
minus.
So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?

Not with the built-in unsigned ints. They simply don't have enough bits
to
store both a value and the sequence of operations that resulted in that
value.
Simply said, all unsigned ints with all bits 0 are equal to 0U, even if
some
were the result of an overflow.

The best way to solve it is to use an intermediary type which does not
overflow.

HTH,
Michiel Salters
 
R

Robert Mabee

So the question is, for addition-minus-mixed calculation, is there any
way to ignore overflow in the middle and only catch it at the end?

How about a class containing the unsigned int and a sticky overflow
flag. You can test the flag when you want, or throw in the conversion
to unsigned int if the flag is set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top