Swift Tutorial Swift References

Swift - exp() Function



The Swift exp() function returns e raised to the power of specified number, i.e., ex. Please note that e is the base of the natural system of logarithms, and its value is approximately 2.718282.

Syntax

In Foundation framework, it is defined as follows:

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

Parameters

x Specify the exponent of e.

Return Value

Returns e raised to the power of specified number.

Example:

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

import Foundation

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

The output of the above code will be:

exp(-2.0) = 0.1353352832366127
exp(-1.0) = 0.36787944117144233
exp(0.0) = 1.0
exp(1.0) = 2.718281828459045
exp(2.0) = 7.38905609893065

❮ Swift Math Functions