Solved

Checking Overflow/Underflow on math

Posted on 1997-05-19
5
961 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 20 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

728 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