How to get the Power of some Integer in Swift language?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Swift does not have a built-in integer exponent operator such as **, so you need to choose an approach explicitly. For small convenience code, pow with Double conversion is fine. For exact integer math, a custom integer-power function is usually the better answer.
Use pow when floating-point math is acceptable
The standard library and Foundation expose pow, but it works with floating-point types rather than Int.
This is simple and often good enough for small values. The downside is that you are going through Double, which means very large values can lose precision before you convert back to Int.
So pow is convenient, but it is not the best tool when exact integer arithmetic matters.
Write an exact integer-power function
For integer-only logic, exponentiation by squaring is a good approach. It is exact for Int values and runs in logarithmic time with respect to the exponent.
This is usually the right answer when you want exact integer results and the exponent is non-negative.
Decide how to handle negative exponents
Negative exponents do not produce integer results in general. 2^-3 is 0.125, not an Int, so an integer-power API must either reject negative exponents or return a floating-point value instead.
Here is one possible Double-based version:
The key is not to pretend a negative exponent still fits naturally into an integer API.
Protect against overflow when needed
Even exact integer arithmetic can overflow quickly. Swift gives you multipliedReportingOverflow when overflow must be handled explicitly.
Returning an optional is often the cleanest way to signal that the result does not fit in the chosen integer type.
Choose the API by meaning, not habit
A useful rule is:
- use
powif floating-point math is acceptable - use a custom integer function when exact integer results matter
- reject or redesign negative exponents for integer-only APIs
- add overflow handling when the numbers may get large
That makes the choice explicit instead of relying on hidden conversions.
Common Pitfalls
The most common mistake is assuming Swift has a built-in integer exponent operator. It does not.
Another common issue is converting through Double and then assuming the result is always exact after converting back to Int.
People also forget that negative exponents usually imply fractional results, which do not belong in an Int API.
Finally, integer exponentiation can overflow much sooner than expected. If the range matters, add checks instead of assuming the result will fit.
Summary
- Swift has
powfor floating-point values, not a built-in integer exponent operator. - For exact integer math, write a small exponentiation-by-squaring helper.
- Negative exponents usually require a floating-point return type.
- Large powers can overflow
Int, so add overflow checks when needed. - Pick the exponentiation approach that matches the numeric meaning of the code.

