Swift Tutorial Swift Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

Swift - expm1() Function



The Swift expm1() function returns e raised to the power of specified number minus 1, i.e., ex-1. 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 expm1(_ x: CGFloat) -> CGFloat
public func expm1(_ x: Float) -> Float
public func expm1(_ x: Float80) -> Float80
public func expm1(_ __x: Double) -> Double

Parameters

x Specify the exponent of e.

Return Value

Returns e raised to the power of specified number minus 1, i.e., ex-1.

Example:

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

import Foundation

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

The output of the above code will be:

expm1(-2.0) = -0.8646647167633873
expm1(-1.0) = -0.6321205588285577
expm1(0.0) = 0.0
expm1(1.0) = 1.718281828459045
expm1(2.0) = 6.38905609893065

❮ Swift Math Functions

5