Swift Program to find the hyperbolic arctangent of the given value


This tutorial will discuss how to write a Swift program to find the hyperbolic arctangent of given radian value 

atanh(a) = 1/2 * ln(1+a/1-a)

In Swift, we can calculate the hyperbolic arctangent of the given radian value using the in-built atanh() function. This function returns the hyperbolic arctangent value of the specified number. Here, the specified number represents an angle.

Syntax

Following is the syntax −

atanh(Num)

Here, the value of Num can be of integer, float, or double type. The value of Num should be in between -1 to 1 range. If the value of Num is outside the given range then it will return NaN.

Formula

If the given value is in degrees then we can convert degrees to radians using the following formula −

Radians = Degrees * (pi / 180)

Below is a demonstration of the same −

Input

Suppose our given input is −

Radian value = 0.1

Output

The desired output would be −

Inverse hyperbolic tan(0.1) is 0.10033534773107558

Algorithm

Following is the algorithm −

Step 1 − Import Foundation library to use mathematic functions.

import Foundation

Step 2 − Declare variables to store the radian values.

Step 3 − If the value is in degrees then, use the following formula −

Radians = Degrees * (pi / 180)

If the value is in radian ignore this step.

Step 4 − Find the hyperbolic arctangent value using the atanh() function −

var res1 = atanh(cNum1)
var res2 = atanh(cNum2)

Step 5 − Print the output

Example 1

Finding hyperbolic arctangent of given radian value

The following program shows how to find the hyperbolic arctangent of the given radian value.

import Foundation import Glibc var cNum1 : Double = 0.3 var cNum2 : Double = -2.1 // Calculating the hyperbolic arctangent of the radian value // Using atanh() function // For positive radian value var res1 = atanh(cNum1) // For negative radian value var res2 = atanh(cNum2) print("Inverse hyperbolic tan(\(cNum1)) is ", res1) print("Inverse hyperbolic tan(\(cNum2)) is ", res2)

Output

Inverse hyperbolic tan(0.3) is 0.30951960420311175
Inverse hyperbolic tan(-2.1) is -nan

Here, in the above code, we find the hyperbolic arctangent value of the given radian using the atanh() function −

var res1 = atanh(cNum1)
var res2 = atanh(cNum2)

Display the result:Inverse hyperbolic tan(0.3) is 0.30951960420311175 and Inverse hyperbolic tan(-2.1) is -nan

Example 2

Finding hyperbolic arctangent of given degrees value

The following program shows how to find the hyperbolic arctangent value of the given degrees.

import Foundation import Glibc var ttNum1 = 30.0 var ttNum2 = 180.0 // Convert degrees into radian var radian1 = ttNum1 * (Double.pi / 180) var radian2 = ttNum2 * (Double.pi / 180) // Calculating the hyperbolic arctangent value // Using atanh() function var res1 = atanh(radian1) var res2 = atanh(radian2) print("Inverse hyperbolic tan(\(ttNum1)) is ", res1) print("Inverse hyperbolic tan(\(ttNum2)) is ", res2)

Output

Inverse hyperbolic tan(30.0) is 0.581285011694723
Inverse hyperbolic tan(180.0) is -nan

Here, in the above code, we calculate the value of hyperbolic arctangent of the given degrees. Here, first we convert the degrees into radian using the following code −

var radian1 = ttNum1 * (Double.pi / 180)
var radian2 = ttNum2 * (Double.pi / 180)

And then calculate the hyperbolic arctangent value using the atanh() function −

var res1 = atanh(radian1)
var res2 = atanh(radian2)

Display the result: Inverse hyperbolic tan(30.0) is 0.581285011694723 and Inverse hyperbolic tan(180.0) is -nan.

Updated on: 07-Oct-2022

85 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements