cosh() function in C++ STL

The cosh() is an inbuilt function in C++ STL which returns the hyperbolic cosine of an angle given in radians.

Syntax :

cosh(data_type x)

Parameter :The function accepts one mandatory parameter x which specifies the hyperbolic angle in radian. The parameter can be of double, float or long double datatype.

Return Value: The function returns the hyperbolic cosine of the argument. If the magnitude of the result is too large to be represented by a value of the return type, the function returns inf.

Below programs illustrate the above method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// cosh() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    double x = 4.1, result;
  
    result = cosh(x);
    cout << "cosh(4.1) = " << result << endl;
  
    // x in Degrees
    double xDegrees = 90;
    x = xDegrees * 3.14159 / 180;
  
    result = cosh(x);
    cout << "cosh(90 degrees) = " << result << endl;
  
    return 0;
}

chevron_right


Output:

cosh(4.1) = 30.1784
cosh(90 degrees) = 2.50918

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the
// cosh() function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    int x = -4;
    double result;
  
    result = cosh(x);
    cout << "cosh(-4) = " << result << endl;
  
    // x in Degrees
    double xDegrees = 90;
    x = xDegrees * 3.14159 / 180;
  
    result = cosh(x);
    cout << "cosh(90 degrees) = " << result << endl;
  
    return 0;
}

chevron_right


Output:

cosh(-4) = 27.3082
cosh(90 degrees) = 1.54308

Errors and Exceptions: The function returns no matching function for call to error when a string or character is passed as an argument.

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the 
// cosh() function when string passed
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    string x = "gfg";
    double result;
  
    result = cosh(x);
    cout << "cosh("gfg") = " << result << endl;
  
    return 0;
}

chevron_right


Output:

prog.cpp:14:20: error: no matching function for call to 'cosh(std::__cxx11::string&)'
result = cosh(x);

Program 4: When argument is too large.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to demonstrate the cosh()
// function
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
    double x = 3000.0, result;
  
    result = cosh(x);
    cout << "cosh(3000.0) = " << result << endl;
  
    return 0;
}

chevron_right


Output:

cosh(3000.0) = inf


My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.