Exceeding limits during arithmetic

M

Mike Aubury

Is there any standard (or even non-standard) way to detect limit overflow in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;
 
J

Jack Klein

Is there any standard (or even non-standard) way to detect limit overflow in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;

No, there is not. If the result of an arithmetic operation on signed
integer or floating point types is outside the range of the type, the
result is undefined behavior. If an arithmetic operation on unsigned
integer types overflows or underflows, the behavior is well-defined,
but no indication is possible.

If it is important, you need to check before you perform the
operation.

Here is a quick snippet for adding two positive signed ints:

#include <limits.h>

/* ... */

if ((INT_MAX - a) > b)
{
c = a + b;
}
else
{
/* overflow handler */
}

/* ... */

There are various cases for addition and subtraction. For addition,
an overflow can only occur when the two values have the same sign. For
subtraction, it can only occur when they have opposite signs.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
O

osmium

Mike Aubury said:
Is there any standard (or even non-standard) way to detect limit overflow
in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;

No. Sadly, I don't know of a single high level language that makes the
overflow flag, which the hardware designer so carefully set, throw that flag
away. If I were looking for a language that did this right, I would
immediately think of Ada, but that might be just wishful thinking.
 
K

Keith Thompson

Jack Klein said:
Here is a quick snippet for adding two positive signed ints:

#include <limits.h>

/* ... */

if ((INT_MAX - a) > b)
{
c = a + b;
}
else
{
/* overflow handler */
}

/* ... */
[...]

Quick, but not quite correct; consider a == -1.
 
J

jacob navia

Mike said:
Is there any standard (or even non-standard) way to detect limit overflow in
arithmetic in C ?

eg.

/* assuming 4 byte ints.. */
int a=2147483647;
int b=2147483647;
int c;

c=a*b;

If you are using the lcc-win32 compiler system you can use the intrinsic
function
bool _overflow(void);

That "pseudo" function will return 1 if the overflow flag is set, zero
otherwise.
 
K

Keith Thompson

osmium said:
No. Sadly, I don't know of a single high level language that makes the
overflow flag, which the hardware designer so carefully set, throw that flag
away. If I were looking for a language that did this right, I would
immediately think of Ada, but that might be just wishful thinking.

<OT>
Yes, Ada requires overflow to be detected; numeric overflow raises an
exception, which may be handled by the program. (I think some
compilers don't enable this check by default.) I'm sure there are
other languages that have similar rules, but of course the details
are off-topic.
</OT>
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top