This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Best way to detect integer overflow in C/C++
how do we check if any arithmetic operation like addition, multiplication or subtraction could result in an overflow?
Check the size of the operands first, and use std::numeric_limits. For example, for addition:
#include <limits>
unsigned int a, b; // from somewhere
unsigned int diff = std::numeric_limits<unsigned int>::max() - a;
if (diff < b) { /* error, cannot add a + b */ }
You cannot generally and reliably detect arithmetic errors after the fact, so you have to do all the checking before.
You can easily template this approach to make it work with any numeric type.
Related
This question already has answers here:
Why is unsigned integer overflow defined behavior but signed integer overflow isn't?
(6 answers)
Is signed integer overflow still undefined behavior in C++?
(3 answers)
Why is the phrase: "undefined behavior means the compiler can do anything it wants" true?
(2 answers)
Closed 3 months ago.
Firstly, I defined two bit integers, and multiply them. The answer must overflow, and I got a negative number. Then, I defined two 64-bit integers, and multiply them. The answer must overflow, and I expected to get a negative number. But, the result was zero. I don't know why this happen, could anybody give me some help?
The code is below.
#include <iostream>
int main ()
{
int x = 1e9, y = 1e9;
printf("%d\n", x * y);
int64_t a = 36028797018963968;
int64_t b = 36028797018963968;
printf("%ld\n", a * b);
return 0;
}
The result is below.
ubuntu#VM-0-2-ubuntu:~/Projects/Test$ g++ test.cpp -o a
ubuntu#VM-0-2-ubuntu:~/Projects/Test$ ./a
-1486618624
0
This question already has answers here:
Overflowing of Unsigned Int
(3 answers)
C/C++ unsigned integer overflow
(4 answers)
Closed 5 years ago.
There is the ULARGE_INTEGER union for compilers that don't support 64 bit arithmetic.
What would happen in the following code if the addition on the last line overflows?
ULARGE_INTEGER u;
u.LowPart = ft->dwLowDateTime;
u.HighPart = ft->dwHighDateTime;
u.LowPart += 10000; //what if overflow?
Related question:
What is the point of the ULARGE_INTEGER union?
ULARGE_INTEGER is composed of two unsigned values. Unsigned values are guaranteed to wrap round, so in some sense they can't "overflow".
If wrap round does occur, u.LowPart will end up being less than 10,000. What you probably want is:
u.LowPart += 10000;
if (u.LowPart < 10000) u.HighPart++;
... but what compiler still doesn't support 64-bit integers these days? They have been required by the C++ standard since 2011, and the C standard since 1999. So what you really want is:
u.QuadPart += 10000; // Forget about legacy compilers that doen't support 64 bits.
This question already has answers here:
How do I detect unsigned integer overflow?
(31 answers)
Closed 6 years ago.
Edit (IMPORTANT): This question was more specific than the question this was marked as a duplicate against. This was asking how to do it with a boolean function. But now I know that it just doesn't work like that. This question shows that a + b = c can only be overflow checked if you write if(c - b == a) and that there is no operator independent way of checking.
Once the overflow happened, you cannot detect it
-lorro
I have been looking for a way to detect integer overflow in C++ using an if statement.
Possible pseudo code:
#include <iostream>
#include <...>
using namespace std;
bool isOverflow(...);
int main()
{
int a = INT_MAX + 1;
if (isOverflow(...))
{
cout << "Overflow" << endl;
}
else
{
cout << "No Overflow" << endl;
}
return 0;
}
bool isOverflow
{
...
}
OK to be honest this pseudo code preference may not work, but i've seen this question asked many times and have not found any useful answers. It may require unsigned or unsigned long long, although I'm not necessarily encouraging the use of those.
EDIT:
I would like to use it with a multiplication sentence with 3 numbers:
a * a * b
I am aware that there is a pow function in <math.h> but that's off topic.
I am also aware that if I want an accurate int result from pow I would use:
int(pow(base, index) + 0.5)
It depends on what sort of operation you are using and what are the types of the operands.
e.g. if you want to detect an overflow after addition, and both operands are unsigned integers, then an overflow will have occurred if the result is less than the sum of both operands.
bool overflow;
if (a+b < a)
overflow = true;
else
overflow = false;
For signed integers, you can refer to the excellent post here
This question already has answers here:
Handle arbitrary length integers in C++
(3 answers)
Closed 7 years ago.
Hi I am doing an algorithm question require get the full result of
5208334^2, which is 27126743055556
I was able to do it with by represent integer using Charracter array. However can we have any better way (shorter or faster) to do that? any idea is welcome ?
Updated:
For my case, both long long and int64 work, just that I did not cast value before return:
int val (int n1, n2) {
........
return (long long) n1 * n2;
}
This number fits into long long(present in GCC and after c++11) type or int64(for some other compilers before c++11). Thus the simplest solution is to use this type.
This question already has answers here:
How do I detect unsigned integer overflow?
(31 answers)
Closed 7 years ago.
What's the easiest way to catch an overflow exception in C++?
For example, when I'm writing something like
int a = 10000, b = 100000;
int c = a * b;
or (optionally)
std::cout << a * b;
I'd like to catch an exception (or notification). How to do so? Maybe there is any native solution for GNU C++, isn't there?
You do the following:
if (b > 0 && a > MAX_REPRESENTABLE_VALUE/b)
{
throw your_exception("Message");
}
Note that a > MAX_REPRESENTABLE_VALUE/b is equivalent to a*b > MAX_REPRESENTABLE_VALUE mathematically but you have to use the former form if you are doing it with limited-precision arithmetic.
See the header climits for constants for MAX_REPRESENTABLE_VALUE: http://www.cplusplus.com/reference/climits/
From the standard section 5 Expressions
If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. [ Note: most existing implementations of C++ ignore integer overflows. Treatment of division by zero, forming a remainder using a zero divisor, and all floating point exceptions vary among machines, and is usually adjustable by a library function. —end note ]
Emphasis mine.
It's not an exception, it's an error (if that pleases you). The reason is that arithmetic overflow cannot be determined before runtime. It is a runtime error subclassing from superclass error.
What you are looking for is arithmetic overflow.
Check out C++ documentation for overflow_error class here.