Swift Tutorial Swift References

Swift - exp2() Function



The Swift exp2() function returns 2 raised to the power of specified number, i.e., 2x.

Syntax

In Foundation framework, it is defined as follows:

public func exp2(_ x: CGFloat) -> CGFloat
public func exp2(_ x: Float) -> Float
public func exp2(_ x: Float80) -> Float80
public func exp2(_ __x: Double) -> Double

Parameters

x Specify the exponent of 2.

Return Value

Returns 2 raised to the power of specified number, i.e., 2x.

Example:

In the example below, exp2() function is used to calculate 2 raised to the power of specified number.

import Foundation

print("exp2(-2.0) = \(exp2(-2.0))")
print("exp2(-1.0) = \(exp2(-1.0))")
print("exp2(0.0) = \(exp2(0.0))")
print("exp2(1.0) = \(exp2(1.0))")
print("exp2(2.0) = \(exp2(2.0))")

The output of the above code will be:

exp2(-2.0) = 0.25
exp2(-1.0) = 0.5
exp2(0.0) = 1.0
exp2(1.0) = 2.0
exp2(2.0) = 4.0

❮ Swift Math Functions