What is acosh() in Swift?
Overview
In Swift, the acosh() function returns the inverse hyperbolic cosine of a number. The image below shows the mathematical representation of the acosh() function.
Syntax
Here’s how the acosh() function is used.
acosh(number)
//number can be real, float, or double.
Note: We need to import
Foundationin our code to use theacosh()function. This can be done using theimport Foundationcommand.
Parameters
A number needs to be passed as an argument to the function. This number can be real, float, or double.
The value of the input argument should be greater than or equal to 1.
The value returned by acosh() is the inverse hyperbolic cosine of number.
Code example
Let’s run a script that uses the acosh() method.
import Swiftimport Foundation//positive numberprint("The value of acosh(1.5) :", acosh(1.5));// 1print("The value of acosh(1.0) :", acosh(1.0));//less than 1print("The value of acosh(0.0): ",acosh(0.0) );print("The value of acosh(-1.5): ",acosh(-1.5) );
Code Explanation
- Line 2: We add the
Foundationheader that is required foracosh()function. - Line 5: We calculate the inverse hyperbolic cosine for
1.5usingacosh(). This is the example we’ve used for an input argument greater than 1. We can see that the output is greater than 0. - Line 8: We calculate the inverse hyperbolic cosine for 1.0 using
acosh(). The output for this is 0. - Lines 11 and 12: We calculate the inverse hyperbolic cosine for two numbers lesser than 1, 0 and -1.5 respectively, using
acosh(). These do not compute, and returnnan.