Besides, the extensive library of Mathematical functions offered by this language ranges from simple functions such as calculating a square root to more functions such as calculating complex trigonometric functions and much more.
In this Linux Hint article about the C language, we will explain how to use the cosh() function, which is one of the basic functions for calculating hyperbolic functions.
To help you understand how to use cosh(), we will explain this function theoretically, introduce its syntax, the types of input and output arguments, and the calling methods.
Then, we will implement the use of this function with practical examples using code snippets in images.
Syntax of the cosh() Function in C Language
Description of the cosh() Function in C Language
The cosh() function is one of the three basic functions for computing hyperbolic functions provided by the C programming language. The other two are sinh(), which calculates the hyperbolic sine of a function, and tanh(), which calculates the hyperbolic tangent.
The cosh() function returns the hyperbolic cosine of X, whose formula is as follows:
The function cosh() is part of the mathematical library of C. Therefore, its use must be defined beforehand in our “.c” code or otherwise in the “.h” header with the following declaration:
Once the “math” library is defined in our code, we can now use the cosh() function, the complementary sinh(), and tanh() functions provided by the C math library.
This function is located in the “libm” library or Math library, so you should call it in the compile command with the following “-lm” command.
How to Get the Hyperbolic Cosine of x in Radians with the Function cosh() in the Language C
Like all functions in the C Mathematical library for solving trigonometric functions, whose input arguments are angles, these variables are expressed in radians.
In this example, we will find the hyperbolic cosine of the angle “x”, expressed in radians, using the cosh() function of the C math library.
As a first step, we include the “.h” headers of the libraries. We will use and define the two variables in this example.
Both are of type double. “x” is the input argument from which we get the hyperbolic cosine, and “r” is the variable to which cosh() returns the result.
In this example, we assign the radian equivalent of 45 degrees to the variable “x”, which corresponds to x = 0.785398 rad. We get the hyperbolic cosine from it.Using the printf() function. We will display the value of “x” and the result of “r” in the command console.
Once we have written the code, we compile with the command.
And we execute with the following command in the Linux console:
In the image below we see the result in the console commands.
How to Convert a Value Expressed in Degrees to Radians and Obtain the Hyperbolic Cosine with the Function cosh() in the Language C
The Mathematical functions provided by the “math” library that involve calculations related to angles are expressed in radians.
In general, the technical data sheets in all their branches contain the formulas for the calculations in degrees. So, it is more convenient to convert these values before sending them as input arguments to these functions.
Here, we show you a convenient way to obtain an angle expressed in degrees in radians. The formula for this conversion is:
So, to obtain the hyperbolic cosine of an angle measure expressed in degrees, in the same code as in the previous example, we define the constant “pi”, add the variable “degree” of type double, and apply to it the conversion formula described above.
Based on this formula, the variable “degrees” contains the angle measured in degrees and the result in radians is returned in “x” to be sent later as an input argument to cosh(). Below, you can see the code for this mode.
In the figure below, you can see the result of running this code on the screen. We have expressed the angle value in degrees in the variable “degrees”, then we convert it and send it as an input argument to cosh().
Conclusion
In this Linux Hint article, we explained how to calculate the hyperbolic cosine of a variable using the cosh() function of the C language mathematical library.
We showed a theoretical description of this function and then applied it in practical examples, attaching images of code fragments that show how to obtain the hyperbolic cosine of the variable x.
We also gave you a hint on how to convert angles expressed in degrees to radians, in order to get a complete handling of the cosh() function and be able to implement it with different angular units of measure.
We hope that this article was useful for you. For more tips like these on the C language, use the search engine on our website.