What is the tanh() function in Swift?
Overview
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
Foundationin our code to use thetanh()function. We can import it like this:import Foundation.
Syntax
tanh(num)
Parameter
This function requires a number representing an angle in radians as a parameter.
To convert degrees to radians, use:
radians = degrees * ( pi / 180.0 )
Return value
This function returns the hyperbolic tangent of a number sent as a parameter.
Example
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)));
Explanation
- Line 2: We add the
Foundationheader required fortanh()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().