The **tanh()**** **function in Swift returns a number’s hyperbolic tangent.

Below is the mathematical representation of the `tanh()`

function:

Note:We need to import`Foundation`

in our code to use the`tanh()`

function. We can import it like this:`import Foundation`

.

tanh(num)

This function requires a `number`

representing an `angle`

in `radians`

as a parameter.

To convert `degrees`

to `radians`

, use:

radians = degrees * ( pi / 180.0 )

This function returns the hyperbolic tangent of a number sent as a parameter.

The code below shows the use of the `tanh()`

function in Swift:

import Swiftimport Foundation//positive number in radiansprint("The value of tanh(2.3) :", tanh(2.3));// negative number in radiansprint("The value of tanh(-2.3) :", tanh(-2.3));//converting the degrees angle into radians and then applying tanh()// degrees = 45.0// PI = 3.14159265print("The value of tanh(45.0 * (PI / 180.0)) :", tanh(45.0 * (Double.pi / 180.0)));

- Line 2: We add the
`Foundation`

header required for`tanh()`

function. - Line 5: We calculate the hyperbolic tangent of the positive number in radians using
`tanh()`

. - Line 8: We calculate the hyperbolic tangent of the negative number in radians using
`tanh()`

.

- Line 13: We converted the angle in degrees to radians and then calculated its hyperbolic tangent using
`tanh()`

.

TRENDING TOPICS