473,387 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Exceeding limits during arithmetic

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 1806
On Tue, 12 Jun 2007 14:27:03 +0100, Mike Aubury
<mi*********@aubit.comwrote in comp.lang.c:
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
Jun 12 '07 #2
"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
Jack Klein <ja*******@spamcop.netwrites:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 12 '07 #4
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
"osmium" <r1********@comcast.netwrites:
[...]
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>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 12 '07 #6
Keith Thompson said:
Jack Klein <ja*******@spamcop.netwrites:
[...]
>Here is a quick snippet for adding two positive signed ints:
<snip>
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
Richard Heathfield <rj*@see.sig.invalidwrites:
Keith Thompson said:
>Jack Klein <ja*******@spamcop.netwrites:
[...]
>>Here is a quick snippet for adding two positive signed ints:

<snip>
>Quick, but not quite correct; consider a == -1.

Read up. :-)
D'oh!

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 12 '07 #8
jacob navia wrote:
Mike Aubury wrote:
>Is there any standard (or even non-standard) way to detect limit
overflow 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.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 12 '07 #9
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 <limits.h>. Diddle to handle -ve input values.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 13 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Maurice LING | last post by:
Hi, I think I've hit a system limit in python when I try to construct a list of 200,000 elements. My error is malloc: vm_allocate (size = 2400256) failed...... Just wondering is this...
1
by: T.S.Negi | last post by:
Hi, A query is exceeding the length of varchar and nvarchar variable. Because I'm picking the data from each record from table and giving it to the query. suggest me some way to do it. ...
11
by: O.R.Senthil Kumaran | last post by:
Hi all, I am trying with the following Question: Q: Consider a function which, for a given whole number n, returns the number of ones required when writing out all numbers between 0 and n. For...
26
by: Bas Wassink | last post by:
Hi there, Does the ANSI standard say anything about incrementing variables past their limits ? When I compile code like this: unsigned char x = 255; x++; printf ( "%d\n", x );
0
by: jack | last post by:
I am getting this error on my web server runnint ASP.NET on Windows 2003, it kills the serving web pages. The only way to get the web pages to start serving is to restart the server. I can't find...
4
by: Nathan Sokalski | last post by:
During my postbacks, I try to assign the value from an Input tag to a property of a custom control. However, I keep recieving the following error: An exception of type...
4
by: Frederick Gotham | last post by:
Let's say we want to write a function which calculates the force which earth's gravity exerts on an object of particular mass at sea level. We could start off with: unsigned CalcForce(unsigned...
1
by: Wayne Shu | last post by:
Hei everyone: Just see the output of the following program #include <iostream> #include <cstdlib> #include <limits> int main() { std::cout << "minimum exponent of double: " <<
88
by: santosh | last post by:
Hello all, In K&R2 one exercise asks the reader to compute and print the limits for the basic integer types. This is trivial for unsigned types. But is it possible for signed types without...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.