449,054 Members | 1,327 Online
Need help? Post your question and get tips & solutions from a community of 449,054 IT Pros & Developers. It's quick & easy.

# Exceeding limits during arithmetic

 P: n/a 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; Jun 12 '07 #1
9 Replies

 P: n/a On Tue, 12 Jun 2007 14:27:03 +0100, Mike Aubury /* ... */ 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 Jun 12 '07 #2

 P: n/a "Mike Aubury" writes: 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. Jun 12 '07 #3

 P: n/a Jack Klein /* ... */ if ((INT_MAX - a) b) { c = a + b; } else { /* overflow handler */ } /* ... */ [...] Quick, but not quite correct; consider a == -1. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 12 '07 #4

 P: n/a Mike Aubury wrote: 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. Jun 12 '07 #5

 P: n/a "osmium" 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. -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 12 '07 #6

 P: n/a Keith Thompson said: Jack Klein Here is a quick snippet for adding two positive signed ints: Quick, but not quite correct; consider a == -1. Read up. :-) -- Richard Heathfield "Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk email: rjh at the above domain, - www. Jun 12 '07 #7

 P: n/a Richard Heathfield Jack Klein >Here is a quick snippet for adding two positive signed ints: >Quick, but not quite correct; consider a == -1. Read up. :-) D'oh! -- Keith Thompson (The_Other_Keith) ks***@mib.org San Diego Supercomputer Center <* "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" Jun 12 '07 #8

 P: n/a jacob navia wrote: Mike Aubury wrote: >Is there any standard (or even non-standard) way to detect limitoverflow in arithmetic in C ? .... snip ... > 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. Perfectly good idea, but non-standard. I am glad you are putting it in the implementation namespace. -- cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com Jun 12 '07 #9

 P: n/a Mike Aubury wrote: > 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 (INT_MAX / b < a) overflow(); else c = a * b; and #include . Diddle to handle -ve input values. -- cbfalconer at maineline dot net -- Posted via a free Usenet account from http://www.teranews.com Jun 13 '07 #10

### This discussion thread is closed

Replies have been disabled for this discussion.