Swift programming
integer power calculation
Swift language tips
exponentiation in Swift
Swift tutorials

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.

swift
1import Foundation
2
3let base = 2
4let exponent = 10
5let result = Int(pow(Double(base), Double(exponent)))
6print(result)

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.

swift
1func intPow(_ base: Int, _ exponent: Int) -> Int {
2    precondition(exponent >= 0, "Exponent must be non-negative")
3
4    var result = 1
5    var currentBase = base
6    var currentExponent = exponent
7
8    while currentExponent > 0 {
9        if currentExponent % 2 == 1 {
10            result *= currentBase
11        }
12        currentExponent /= 2
13        if currentExponent > 0 {
14            currentBase *= currentBase
15        }
16    }
17
18    return result
19}
20
21print(intPow(3, 5))
22print(intPow(2, 10))

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:

swift
1import Foundation
2
3func signedPow(_ base: Double, _ exponent: Int) -> Double {
4    if exponent >= 0 {
5        return pow(base, Double(exponent))
6    }
7    return 1.0 / pow(base, Double(-exponent))
8}
9
10print(signedPow(2.0, -3))

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.

swift
1func safeIntPow(_ base: Int, _ exponent: Int) -> Int? {
2    precondition(exponent >= 0, "Exponent must be non-negative")
3
4    var result = 1
5    var currentBase = base
6    var currentExponent = exponent
7
8    while currentExponent > 0 {
9        if currentExponent % 2 == 1 {
10            let step = result.multipliedReportingOverflow(by: currentBase)
11            if step.overflow { return nil }
12            result = step.partialValue
13        }
14
15        currentExponent /= 2
16
17        if currentExponent > 0 {
18            let square = currentBase.multipliedReportingOverflow(by: currentBase)
19            if square.overflow { return nil }
20            currentBase = square.partialValue
21        }
22    }
23
24    return result
25}
26
27print(safeIntPow(2, 30) as Any)

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 pow if 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 pow for 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.

Course illustration
Course illustration

All Rights Reserved.