Solved

Overflow calculation

Posted on 2011-03-18
11
398 Views
Last Modified: 2012-05-11
Is it possible to write a bit of C code to perform an arithmetical operation and detect overflow without using exceptions, nor surpressing them, and avoiding the use of assembler?

I was thinking about something like

long ratPerformAdd(a,b long, BOOL *overflow) {
    ........
}

where either the sum gets returned or the flag overflow gets set?

I'm looking for a platform independant (but 32 bit) operation which I can use on several machines without resorting to assembler.
0
Comment
Question by:BigRat
11 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 35167250
If a*b < 0, then a+b will not overflow
i.e., if a and b have different sign, then no worries about overflow when adding.

If same signs, then if a+b has different sign than b, then overflow.
0
 
LVL 27

Author Comment

by:BigRat
ID: 35167455
Hmmm, that was not the sort of thing I was looking for, unless one splits the longs into 16 bit quantities and adds them together. Of course I'll need subtraction and multiplication, since division is really no problem.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35167751
>> unless one splits the longs into 16 bit quantities and adds them together.
To do the add, just add the two long values and return the overflow status. No need to split into short ints.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35167955
Do you want some code for ratPerformAdd?
0
 
LVL 86

Expert Comment

by:jkr
ID: 35168702
Would ''matherr()' (http://www.kernel.org/doc/man-pages/online/pages/man3/matherr.3.html - scroll down for an example) be an option?
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 32

Accepted Solution

by:
phoffric earned 250 total points
ID: 35168856
>> that was not the sort of thing I was looking for
Here's another addition overflow detection approach:

If both a and b are positive, then overflow if and only if
    a > LONG_MAX - b
since that implies that a + b is greater than the maximum long value

If both a and b are negative, then overflow iff
    a < LONG_MIN - b

Note: these macros are defined in <limits.h>
0
 
LVL 32

Expert Comment

by:phoffric
ID: 35168944
For subtraction, just convert to addition and reuse the addition overflow detection scheme.


For multiplication, you have overflow if one arg is INT_MIN and the other is -1.
For other cases, take the absolute values of a and b (call them A and B).

   If you have long long type (i.e., a 64 bit integer), then you test for multiplication overflow:
     long long val64 = A*B;
   and see whether there are excess bits using a mask (i.e., long long mask = ~0x7FFFFFFF)

   Or, if 64 bit integer is not available, then you have an overflow if
      A > INT_MAX/B      or       B > INT_MAX/A



For division, a/b, you have overflow if a == INT_MIN and b = -1. Then a/b = a, but it should be -a.
0
 
LVL 27

Author Comment

by:BigRat
ID: 35214436
Sorry about the delay, I'm still at home with the flu, but will be back in the office on Monday. meanwhile I think that someting alongs the lines of ID 35168856 will be the solution.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37419049
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
LVL 27

Author Comment

by:BigRat
ID: 37406149
Sorry, I forgot this question, since I never got around to implementing the code from it.

I have just tried to close this question, but can't. Somehow I'm supposed to generate a cancel auto-close request before I can close it? How complicated!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How do I recover data from my iphone 3g 4 44
libcurl and C++ - Post JSON Data 8 1,064
C hashtable library 3 71
Problem to save 10 121
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

708 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

20 Experts available now in Live!

Get 1:1 Help Now