Detecting under/over flow

P

pocmatos

Hi all,

What the best way to detect under/over flow in a program with a lot of
computations?

For example:
#include <iostream>
#include <limits>

using namespace std;

int main() {

int max = std::numeric_limits<int>::max();
int overflow = max + 1;

cout << "initial: " << max << endl << "overflow: " << overflow <<
endl;

return 0;

}

This silently outputs (even by compiling with -Wall using g++):
$ ./overflow
initial: 2147483647
overflow: -2147483648

The nice thing would be to have something like an exception thrown when
this happens since after an overflow, a program which makes use of a
lot of computations is useless after that.

Any ideas on how to detecting these and underflows? (I'm interested
also in non-portable solutions if you know about any)

Thanks,

Paulo Matos
 
P

Pascal Bourguignon

Hi all,

What the best way to detect under/over flow in a program with a lot of
computations?

For example:
#include <iostream>
#include <limits>

using namespace std;

int main() {

int max = std::numeric_limits<int>::max();
int overflow = max + 1;

cout << "initial: " << max << endl << "overflow: " << overflow <<
endl;

return 0;

}

This silently outputs (even by compiling with -Wall using g++):
$ ./overflow
initial: 2147483647
overflow: -2147483648

The nice thing would be to have something like an exception thrown when
this happens since after an overflow, a program which makes use of a
lot of computations is useless after that.

Any ideas on how to detecting these and underflows? (I'm interested
also in non-portable solutions if you know about any)

This is not in the C standard. That is, it depends on the compiler
you're using. If by chance, you're using gcc, then you can use this
option:

-ftrapv
This option generates traps for signed overflow on addition,
subtraction, multiplication operations.

There are also options for floating-point overflows; man gcc
 
?

=?iso-8859-1?Q?M=E5ns_Rullg=E5rd?=

Pascal Bourguignon said:
Hi all,

What the best way to detect under/over flow in a program with a lot of
computations?
[...]
The nice thing would be to have something like an exception thrown when
this happens since after an overflow, a program which makes use of a
lot of computations is useless after that.

Any ideas on how to detecting these and underflows? (I'm interested
also in non-portable solutions if you know about any)

This is not in the C standard. That is, it depends on the compiler
you're using. If by chance, you're using gcc, then you can use this
option:

-ftrapv
This option generates traps for signed overflow on addition,
subtraction, multiplication operations.

There are also options for floating-point overflows; man gcc

Better to design the code such that overflows are not a problem in the
first place. It is likely to run much faster that way.
 
G

Giorgos Keramidas

Hi all,

What the best way to detect under/over flow in a program with a
lot of computations?

Prevent it from happening?
For example:
#include <iostream>
#include <limits>

using namespace std;

int main() {

int max = std::numeric_limits<int>::max();
int overflow = max + 1;

cout << "initial: " << max << endl <<
"overflow: " << overflow << endl;

return 0;

}

This silently outputs (even by compiling with -Wall using g++):
$ ./overflow
initial: 2147483647
overflow: -2147483648

I usually check when two numbers `a' and `b' are added, like this:

if (INT_MAX - a < b) {
overflow;
}
a += b;

This way, you can detect the overflow before it happens. In your
case `a' is `overflow' and `b' is 1, so this could be written as:

int overflow = std::numeric_limits<int>::max();
int more = 1;

if (std::numeric_limits<int>::max() - more < overflow)
cerr "Overflowed" << endl;
overflow += more;
 
U

u.int.32.t

Giorgos said:
I usually check when two numbers `a' and `b' are added, like this:

if (INT_MAX - a < b) {
overflow;
}
a += b;

Thats gonna be some seriously slow code.
 
K

Kai-Uwe Bux

Thats gonna be some seriously slow code.

And if a is negative, it has undefined behavior. If you want it to be
correct in all cases, it's going to be much slower.


Best

Kai-Uwe Bux
 
?

=?iso-8859-1?Q?M=E5ns_Rullg=E5rd?=

Brian Raiter said:
Still, a wrong answer is only marginally improved by being provided
quickly.

True. However, in this particular case, the correct solution is not
always in finding a way to detect the overflow, but to prevent it from
happening in the first place. Then no check will be required, and the
answer will both arrive quickly and be correct. In many cases
involving overflowing arithmetic, simply performing the operations in
a different order is all that is required. Other times, clever use of
known limits to the range of input values can be the solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top