Solved

Checking Overflow/Underflow on math

Posted on 1997-05-19
5
927 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
5 Comments
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now