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.

Mathematical representation of the inverse hyperbolic cosine function.

Syntax

Here’s how the acosh() function is used.

acosh(number)
//number can be real, float, or double.

Note: We need to import Foundation in our code to use the acosh() function. This can be done using the import Foundation command.

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 Swift
import Foundation
//positive number
print("The value of acosh(1.5) :", acosh(1.5));
// 1
print("The value of acosh(1.0) :", acosh(1.0));
//less than 1
print("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 Foundation header that is required for acosh() function.
  • Line 5: We calculate the inverse hyperbolic cosine for 1.5 using acosh(). 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 return nan.

Free Resources