Solved

Check if an int has overflowed

Posted on 2000-03-03
11
198 Views
Last Modified: 2010-04-02
If an int recieves a number which is to big, the extra bits get truncated, and the resulting number can be either positive or negative.  Is there a way to check to see if this has happened?  I know I can use a double which would simply go to infinity if such a thing was to happen but I am trying to deal with ints.
0
Comment
Question by:mvoiers
[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
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2581073
Yep,you can use double and check it if it's higher than the highest value of unsigned int.
if(doub>0xFFFFFFFF)
......

0
 
LVL 5

Expert Comment

by:Wyn
ID: 2581087
You can use
UINT_MAX
INT_MAX
....const
0
 

Author Comment

by:mvoiers
ID: 2581142
Maybe I was not clear.  I did state that I wanted to deal with ints and not doubles and the above solution clearly deals with doubles.  I realize I could use a double and then compare it to INT_MAX but then I would be using a double and not an int.  Say for example,

int x;
cin>>x;
cout<<x;

if the user types in INT_MAX+1 the value printed will be -INT_MAX.  Is there a way to check between the cin and cout statements that the user entered a value for int greater then int_max?

There is no way to get C++ to throw an exception is an int overflows?

0
Independent Software Vendors: 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!

 
LVL 86

Expert Comment

by:jkr
ID: 2581168
What OS are you using? Win32 raises a EXCEPTION_INT_OVERFLOW (SEH, not C++) in this case...
0
 

Author Comment

by:mvoiers
ID: 2581216
I am using Solaris.  This is a little practice assignment at work, we need to maintain portability and also ANSI C standards.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 2581217
jkr, I don't think that is true.  

mvoiers, there is no "universal" way to detected overflow.   but there are ways for any particular mathematical operation.  so if you can provide a mathematical operation, we can suggest a way to detect a problem.  

In the case you provided, the math is being done by the C++ run-time library, so you can't detect the problem.  The solution is to do the amth yourself.  Allow the user to input the number as a string, then you convert the number to an integer.  As you process each digit of the string you save the values generated from the previous digits.  If after processing any digit, the new value is less than the value you got from the previous digits, then you overflowed.
0
 

Author Comment

by:mvoiers
ID: 2581237
Ok...I guess I got my answer.  I was really trying to get at the fact that if I have a function that takes an int  and someone passes in something huge there is no way for that function to check if it has overflowed, unlike double which just goes to INFINITY.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2581284
What do you mean by "someone"  if you mean a fucntion that calls your function, well that function has to pass an int, so it can't pass anythign larger than an int.  Overflows don't occur this way, then occur when you perform calculations or when you assign a value to a "smaller" number.  You can test for errors in these circumstances.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2581344
>>jkr, I don't think that is true

"EXCEPTION_INT_OVERFLOW
 
 The result of an integer operation caused a carry out of the most significant bit of the result."

(See help on 'GetExceptionCode()')
0
 
LVL 22

Expert Comment

by:nietod
ID: 2581455
But I don't think it is being used is it?  The x86 CPU doesn't cause an exception when this happens (probably no CPUs do.) so this would have to be manually tested for every time an int is manipulated in a way that this could happen.  That is a lot of work and it is not done in the VC code I've seen.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2581551
Hmm, you're right - tested it and it didn't work ;-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 104
I could not build boost code, 10 116
Beginner to Unreal Engine 4 5 119
Precision Problem in C++ 7 67
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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…
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.

734 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