What is cosh() in D?
Overview
The cosh() function returns the hyperbolic cosine of a number.
The figure below shows the mathematical representation of the cosh() function:
Note: Import
std.mathin the code to use thecosh()function. We can import it like this:import std.math
Syntax
cosh(num)
Parameter
This function requires a number that represents an angle in radians as a parameter.
To convert degrees to radians, use the following formula:
radians = degrees * ( pi / 180.0 )
Return value
cosh() returns the hyperbolic cosine of a number sent as a parameter.
Example
The code below shows how to use the cosh() function in D:
import core.stdc.stdio;import std.stdio;//header required for functionimport std.math;int main(){//positive number in radianswriteln("The value of cosh(2.3): ", cosh(2.3));// negative number in radianswriteln("The value of cosh(-2.3): ", cosh(-2.3));//converting the degrees angle into radians and then applying cosh()// degrees = 180.0// PI = 3.14159265// result first converts degrees to radians then apply coshdouble result=cosh(180.0 * (PI / 180.0));writeln("The value of cosh(180.0 * (PI / 180.0)): ", result);//exceptional outputwriteln ("The value of cosh(real.infinity) : ",cosh(real.infinity));writeln ("The value of cosh(-real.infinity) : ",cosh(-real.infinity));writeln ("The value of cosh(real.nan) : ",cosh(real.nan));writeln ("The value of cosh(-real.nan) : ",cosh(-real.nan));return 0;}
Explanation
-
Line 4: We add the header
std.mathrequired for thecosh()function. -
Line 9: We calculate hyperbolic cosine of a positive angle given in radians using
cosh(). -
Line 12: We calculate hyperbolic cosine of a negative angle given in radians using
cosh(). -
Line 18: We convert the degrees into radians and then apply the
cosh()function. The variableresultfirst converts degrees into radians and then appliescosh(). -
Lines 22 to 26: We calculate the hyperbolic cosine of exceptional numbers using
cosh().