K
Karthik
Hi,
I am writing this application that needs a lot of arithmetic
calculations.
I was wondering if C++ language specifies any way of detecting
arithmetic overflows.
Let us consider the following program.
#include <iostream>
using namespace std;
int main() {
short a = -10000;
short b = a * 8;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
}
My implementation says it to be -
$ ./a.out
a = -10000
b = -14464
How would I find if an overflow had occured ?
I was looking at overflow_error class provided by stdexcept library in
C++. That does not seem to be raised automatically.
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
try {
short a = -10000;
short b = a * 8;
} catch ( overflow_error & r ) {
// No. arithmetic exception is not caught here.
}
}
This being the case how would i catch arithmetic overflows ?
I am writing this application that needs a lot of arithmetic
calculations.
I was wondering if C++ language specifies any way of detecting
arithmetic overflows.
Let us consider the following program.
#include <iostream>
using namespace std;
int main() {
short a = -10000;
short b = a * 8;
cout << " a = " << a << endl;
cout << " b = " << b << endl;
}
My implementation says it to be -
$ ./a.out
a = -10000
b = -14464
How would I find if an overflow had occured ?
I was looking at overflow_error class provided by stdexcept library in
C++. That does not seem to be raised automatically.
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
try {
short a = -10000;
short b = a * 8;
} catch ( overflow_error & r ) {
// No. arithmetic exception is not caught here.
}
}
This being the case how would i catch arithmetic overflows ?