Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

atanh() function in C++ STL

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

The atanh() is an inbuilt function in C++ STL that returns the inverse hyperbolic tangent of an angle given in radians. The function belongs to <cmath> header file.
Syntax: 

atanh(data_type x)

Parameter: This function accepts one mandatory parameter x which specifies the inverse hyperbolic angle in radian which lies in the range [-1, 1]. The parameter can be of double, float, or long double datatype. 
Return Value: This function returns the inverse hyperbolic sine of the argument in radians depending on the parameter passed in the argument. The different return values are given in the table below: 

Parameter Passed(x)Return Value
-1<x<1Finite Value
x>1 or x<-1NaN(Not a Number)
x=-1-inf
x=1+inf

According to C++ 11 standard, there are various prototypes available for atanh() function,

DatatypePrototype
For doubledouble atanh(double x);

Explicit conversion is required from (int, float or long double) to double,

DatatypePrototype
For int

int a = 0;

double b = atanh(double(a));

For float

float a = 0;

double b = atanh(double(a));

For long double

long double a = 0;

double b = atanh(double(a));

Time Complexity: O(1)
Auxiliary Space: O(1)

Below programs illustrate the above method: 
Example 1: 

CPP




// C++ program to illustrate
// the atanh() function
// all return values
#include <bits/stdc++.h>
using namespace std;
 
// Driver Code
int main()
{
    // Return value when -1<x<1
    int x = 0;
    // Function call to calculate atanh(x) value
    double result = atanh(x);
 
    cout << "atanh(0) = " << result << " radians\n";
    cout << "atanh(0) = " << result * 180 / 3.141592
         << " degrees\n";
 
    // Return value when x=-1
    x = -1;
    result = atanh(x);
 
    cout << "\natanh(-1) = " << result << " radians\n";
    cout << "atanh(-1) = " << result * 180 / 3.141592
         << " degrees\n";
 
    // Return value when x=1
    x = 1;
    result = atanh(x);
 
    cout << "\natanh(1) = " << result << " radians\n";
    cout << "atanh(1) = " << result * 180 / 3.141592
         << " degrees\n";
 
    // Return value when x<-1 or x>1
    x = -2;
    result = atanh(x);
 
    cout << "\natanh(-2) = " << result << " radians\n";
    cout << "atanh(-2) = " << result * 180 / 3.141592
         << " degrees\n";
 
    return 0;
}

Output

atanh(0) = 0 radians
atanh(0) = 0 degrees

atanh(-1) = -inf radians
atanh(-1) = -inf degrees

atanh(1) = inf radians
atanh(1) = inf degrees

atanh(-2) = -nan radians
atanh(-2) = -nan degrees

Errors and Exceptions: The function returns a no matching function error message when a string or any other data_type except the one which the function excepts is passed. 
Example: 

CPP




// C++ program to demonstrate
// the atanh() function
// string passed
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    string x = "gfg";
 
    // Function call to calculate atanh(x) value
    double result = atanh(x);
 
    cout << "atanh(50.0) = " << result << " radians"
         << endl;
    cout << "atanh(50.0) = " << result * 180 / 3.141592
         << " degrees" << endl;
    return 0;
}

The above program generates an error if no matching function for call as a string is passed as an argument.

C++




// C++ program to demonstrate
// the atanh() function
// when input 2 is passed.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
    double x = 2.0;
   // Function call to calculate atanh(x) value
    double result = atanh(x);
    cout << "result = " << result << endl;
    return 0;
}

Output

result = -nan

The above code generates nan as output which means not a number this is because the input is outside the range of [-1,1].

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 


My Personal Notes arrow_drop_up
Last Updated : 06 Mar, 2023
Like Article
Save Article
Similar Reads