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 7 2189 po******@gmail.com writes: 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
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Klingon function calls do not have "parameters" -- they have
"arguments" and they ALWAYS WIN THEM."
Pascal Bourguignon <pj*@informatimago.com> writes: po******@gmail.com writes:
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.
--
Måns Rullgård mr*@inprovide.com
On 11 Jun 2006 06:28:18 -0700, po******@gmail.com wrote: 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;
Giorgos Keramidas wrote: 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. u.********@gmail.com wrote: Giorgos Keramidas wrote: 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.
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
>> if (INT_MAX - a < b) { overflow; } a += b;
Thats gonna be some seriously slow code.
Still, a wrong answer is only marginally improved by being provided quickly.
b
Brian Raiter <br******@muppetlabs.com> writes: if (INT_MAX - a < b) { overflow; } a += b;
Thats gonna be some seriously slow code.
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.
--
Måns Rullgård mr*@inprovide.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Frances Del Rio |
last post by:
pls, why is this not working?
<SCRIPT language=JavaScript type="text/javascript">
var br = '<SCRIPT language=Javascript'
br += 'src="js_pop.js" type="text/javascript">'
br += '</SCRIPT>'
var...
|
by: Mandy Memphis |
last post by:
If I perform a mousedown within a document, move the mouse outside the
browser window, and then release the mouse button, the document.onmouseup
event does not fire. Is there any way to detect a...
|
by: Simon Harvey |
last post by:
Hi everyone,
I'm having a frustrating problem and I don't know how to fix it without
totally redoing a very complicated couple of pages on my site. I really hope
some kind soul can help me :-)
...
|
by: questions? |
last post by:
I have a problem involves under flow/over flow.
###################################################
# include <stdio.h>
# include <math.h>
# include <stdlib.h>
double rate;
double t;...
|
by: VK |
last post by:
I wandering about the common proctice of some UA's producers to spoof
the UA string to pretend to be another browser (most often IE).
Shouldn't it be considered as a trademark violation of the...
|
by: Jean-François Michaud |
last post by:
Hello people,
I was wondering how it I could implement keep with next logic on lines
of text contained within a paragraph.
I need to have at least 10 lines of text within a paragraph to be...
|
by: =?Utf-8?B?QWxiZXJ0IEZ1?= |
last post by:
Hi,
my function: myAdjustForm.Show();
I am using the Show method to show the pop-up window to the users
in my C# program.
I found that I have invoke this function TWICE at the beginning...
|
by: jboswell |
last post by:
Here's where I stand. I have two queries. The first calculates a 7-day moving average (Avg7Day) for every day. Here's how it works:
SELECT T1.SITE_NO, T1.DATE, T1.FLOW, Avg(T2.FLOW) AS Avg7Day,...
|
by: RobG |
last post by:
When using createEvent, an eventType parameter must be provided as an
argument. This can be one of those specified in DOM 2 or 3 Events, or
it might be a proprietary eventType.
My problem is...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |