exception handling for int overflow

Pages: 12
SIK: yes, that was the question.
Then will the following modification to Cubbis function also not work for you?

1
2
3
4
5
6
7
8
9
10
11
12
int mypow(int n, int p)
{
   if (p < 0)     throw std::runtime_error("negative powers not allowed");
   if (p == 0)    return 1;
   if (p == 1)    return n;

   double f = pow(n, p);
   int z = (int)f;

   if (f != (double)z)     throw std::overflow_error("result overflow error in int");
   return z;
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12