Swift Tutorial Swift References

Swift - tanh() Function



The Swift tanh() function returns hyperbolic tangent of a value. The hyperbolic tangent of x is defined as:

tanh

where e is an Euler's number.

Syntax

In Foundation framework, it is defined as follows:

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

Parameters

x Specify the value.

Return Value

Returns the hyperbolic tangent of a value.

Example:

In the example below, tanh() function is used to find out the hyperbolic tangent of a value.

import Foundation

print("tanh(-4.0) = \(tanh(-4.0))")
print("tanh(-2.0) = \(tanh(-2.0))")
print("tanh(0.0) = \(tanh(0.0))")
print("tanh(2.0) = \(tanh(2.0))")
print("tanh(4.0) = \(tanh(4.0))")

The output of the above code will be:

tanh(-4.0) = -0.999329299739067
tanh(-2.0) = -0.9640275800758169
tanh(0.0) = 0.0
tanh(2.0) = 0.9640275800758169
tanh(4.0) = 0.999329299739067

❮ Swift Math Functions