?
Solved

Checking Overflow/Underflow on math

Posted on 1997-05-19
5
Medium Priority
?
966 Views
Last Modified: 2012-05-04
How does one check on overflow/underflow for the following C++ types: long, short, float, double, for add, subtract, multiply, divide.  Example:      long L;      L = 2147483647;
      L++;      // Check flag to see if it overflowed.Using assembly language, I can easily accomplish this under Intel/Win32 via:      seto al      mov fOverflow, alAnd for floating point, I can use _clearfp( ).  However, this is Microsoft compiler and Intel CPU specific.  How does one do this in plain C++??Is there a way?More sample code:      long L;
      L = 2147483647;
      L++
----
      short I;
      I = 32767;
      I++;
----
      double d;
      d = 1.7976931348623158e+308;
      d = d * d;
----
      float f;
      f = 16777216.000000;
      f++;
0
Comment
Question by:Shrif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1163567
There is a Microsoft extension (Structured Exception Handling) in Visaul C++ to do it.

__try
{
...
}
__except( GetExceptionCode() == STATUS_INTEGER_OVERFLOW )
{
}

You can get more information by seeing the documentation that comes with Visual C++:
Visaul C++ Books/C/C++/Programming Techniques/Structured Exception Handling/.

0
 

Author Comment

by:Shrif
ID: 1163568
I was very clear in stating in my question that I am seeking a non-platform specific answer.  I cannot rely on assembly language, or Visual C++.In addition, so that it is clear, I need the solution to also work for the largest possible type as well.  I say this because an obvious way to check for overflow on an integer is to perform the same operation with a larget size, a long, and compare the results.  This technique will fail for the largest size, double, which also needs to be handled.
0
 
LVL 4

Expert Comment

by:md041797
ID: 1163569
There is an extension to ANSI C that provides for signal (SIGFPE), but it's not guarantted to be implemented by every compiler, and the handling of errno is not standardized either.  Anyway, it would cause a jump to your handler and that's not good for C++.

I would recommend an explicit check as follows:

Check = L = 2147483647;
L++;
if (Check != --L) //then overflow


0
 

Author Comment

by:Shrif
ID: 1163570
The proposed solution does not work.  If you add 1 to the value, you may overflow -- that's true, but that doesn't mean that subtracting from the result will not return you back to where you started.  It will.  Sorry.
0
 
LVL 5

Accepted Solution

by:
icd earned 40 total points
ID: 1163571
I am not aware of any 'standard' way to do this but I observe the following:-

For any integer data type (char, int, long, etc)

c = a + b; If b is positive c > a unless overflow
           if b is negative c < a unless underflow

c = a - b; if b is positive c < a unless underflow
           if b is negative c > a unless overflow

c = a * b; abs(c) >= abs(a) if b >= 1 unless overflow
           c == 0 if b = 0

c = a / b; cannot produce over or underflow but can produce
           an error if b == 0

You could encapsulate these rules in a new data type.

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question