Swift Tutorial Swift References

Swift - pow() Function



The Swift pow() function returns the base raise to the power of exponent.

Syntax

In Foundation framework, it is defined as follows:

public func hypot(_ x: CGFloat, _ y: CGFloat) -> CGFloat
public func hypot(_ x: Float, _ y: Float) -> Float
public func hypot(_ x: Float80, _ y: Float80) -> Float80
public func pow(_ __x: Double, _ __y: Double) -> Double
public func pow(_ x: Decimal, _ y: Int) -> Decimal

Parameters

x Specify the base.
y Specify the exponent.

Return Value

Returns the base raise to the power of exponent.
if x is negative and y is a fraction, it returns -nan.

Example:

In the example below, pow() function is used to calculate the base raised to the power of exponent.

import Foundation

print("pow(2.0, 3.0) = \(pow(2.0, 3.0))")
print("pow(5.0, -3.0) = \(pow(5.0, -3.0))")
print("pow(10.0, 3.5) = \(pow(10.0, 3.5))")
print("pow(-10.0, 3.5) = \(pow(-10.0, 3.5))")

The output of the above code will be:

pow(2.0, 3.0) = 8.0
pow(5.0, -3.0) = 0.008
pow(10.0, 3.5) = 3162.2776601683795
pow(-10.0, 3.5) = -nan

❮ Swift Math Functions