Take the 2-minute tour ×

Trivial question: I am just wondering, if one wants to raise an int or a long to the power of another int or long, does


suffice, or do I need

(long)(0.5 + std::pow((double)a,(double)b))


share|improve this question
what happened when you tried it? –  Hunter McMillen Aug 17 '11 at 14:02

7 Answers 7

up vote 4 down vote accepted

There are two considerations. The first is roundoff due to inexactness of floating point representations; this won't be a problem in this case, because integers are perfectly representable as long as the number of bits is less than the bits of significand in the floating point. If long is 32 bits and double significand is 53 bits, as it usually is, there won't be a problem.

The second consideration is the accuracy of the pow function itself. If there's any possibility that it will come out lower than the actual value, adding 0.5 to round the result is the only way to ensure the proper result.

The expected result of your pow will always be an integer, so there's no harm in adding 0.5. I'd recommend that version.

share|improve this answer
Great answer. I guess it is best not to assume anything about the accuracy of the pow function and just use v2. –  Cookie Aug 17 '11 at 14:19
Any ideas or reasons there isn't an integer form of pow in the standard library? –  Thomas Matthews Aug 17 '11 at 16:55
@Thomas, just guessing - a combination of not needing it very often and pow being a good enough substitute. –  Mark Ransom Aug 17 '11 at 16:58

You should write your own function. That way you can handle overflow yourself, and you don't have to worry about rounding errors.

But of the two I would use the second.

share|improve this answer

The second example may be more suitable as it rounds the result off to the nearest integer rather than truncates it.

share|improve this answer
Remember that (int)0.999 = 0 but (int)(0.999+0.5) = 1 –  AlexK Aug 17 '11 at 14:10

Why not just use a loop. There can't be more than 31/63 integer multiplications without overflow anyway.

share|improve this answer
One more own function in the code base I guess. Plus efficiency. But I agree that it is an option. –  Cookie Aug 17 '11 at 14:16
Here is an implementation with only 5/6 multiplications. stackoverflow.com/questions/101439/… –  adrianm Aug 17 '11 at 14:49
Nice link! Cheers! –  Cookie Aug 17 '11 at 14:59

First one would be sufficient but you need to check the result double value before casting since it can be pretty larger than long's max.

share|improve this answer

Your first example is valid. It would be best to use a static_cast<>() instead of a c-style cast for clarity however. The second example you posted would give an incorrect result for small values and is superfluous.

share|improve this answer
When would the second example yield incorrect results? And couldn't it happen in the first example that say 3 ^ 3 becomes 8.9999999999 and in the cast becomes 8? I don't expect this to happen for small numbers but once numbers become large, is this not a possibility? –  Cookie Aug 17 '11 at 14:05
What values would it give an incorrect result for? –  john Aug 17 '11 at 14:05
@myself: It would give wrong results for -ve a and odd b of course, but apart from that? –  john Aug 17 '11 at 14:07
If you have a negative exponent or compute a power where the result is less than 1. Considering the result will be cast back to an integer value that's going to be a problem anyway. –  Ephphatha Aug 17 '11 at 14:09
@Ephphatha: Less than 1 (and not 0) can't happen with ints or longs. –  Cookie Aug 17 '11 at 14:11

It should be sufficient to do:

static_cast<long>(std::pow(static_cast<double>(a), b));

You should realise that this will overflow on large values of b.

share|improve this answer

Your Answer


