Home  >  Article  >  Backend Development  >  C++ program to find hyperbolic cosine value given radians value

C++ program to find hyperbolic cosine value given radians value

PHPz
PHPzforward
2023-08-31 23:25:081355browse

C++ program to find hyperbolic cosine value given radians value

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. Hyperbolic functions are used in hyperbolic geometry to calculate angles and distances. They also appear in the solutions of a large number of linear differential equations, cubic equations, etc. For a given angle $\theta$. The hyperbolic cosine function cosh$(\theta)$ is as follows

$$\mathrm{cos(x)\:=\:\frac{e^x\: \:e^{-x}}{2}\:=\:\frac{e^{2x } 1}{2e^x}\:=\:\frac{1 e^{-2x}}{2e^{-x}}}$$

In this article we will discuss techniques for obtaining the value of cosh$(\theta)$ in C when the angle is given in radians.

cosh() function

This cosh$(\theta)$ operation requires the cosh() function in the cmath package in C. This function takes an angle in radians as input and returns a hyperbolic cosine result. Simple syntax is used here:

grammar

#include < cmath >
cosh( <angle in radian> )

algorithm

  • Take angle x (in radians) as input
  • Use cosh(x) to calculate cosh (x)
  • Return results.

Example

#include <iostream>
#include <cmath>
using namespace std;

float solve( float x ) {
   float answer;
   answer = cosh( x );
   return answer;
}

int main()
{
   cout << "The value of cosh( pi/2 ) is: " << solve( 3.14159 / 2 ) << endl;
   cout << "The value of cosh( pi ) is: " << solve( 3.14159 ) << endl;
   cout << "The value of cosh with an angle of 90 degrees is: " << solve( 90 * 3.14159 / 180 ) << endl;
   cout << "The value of cosh with an angle of 45 degrees is: " << solve( 45 * 3.14159 / 180 ) << endl;
}

Output

The value of cosh( pi/2 ) is: 2.50918
The value of cosh( pi ) is: 11.5919
The value of cosh with an angle of 90 degrees is: 2.50918
The value of cosh with an angle of 45 degrees is: 1.32461

In this example, the first two input values ​​are in radians, while the last two input values ​​are in degrees and have been converted to radians using the following formula:

$$\mathrm{\theta_{rad}\:=\:\theta_{deg}\:\times\:\frac{\pi}{180}}$$

in conclusion

In C, use the cosh() function to determine the hyperbolic cosine of a given angle in radians. The cmath header file must be included in our C code to use this function, even though it is part of the standard library. If the result is too large, the cosh() function sets the error code to ERANGE and returns the value HUGE_VAL (which can be positive or negative, depending on the value of x). Although the C90 version of C had a double return type, later versions of C overloaded methods for float and long double in addition to improving generic (template) usage of integers. The article uses various parameters of the function in radians or degrees; however, for degrees, the value is converted to radians using the formula given above.

The above is the detailed content of C++ program to find hyperbolic cosine value given radians value. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete