Solved

Checking Overflow/Underflow on math

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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