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:

Mathematical representation of hyperbolic cosine function

Note: Import std.math in the code to use the cosh() 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 function
import std.math;
int main()
{
//positive number in radians
writeln("The value of cosh(2.3): ", cosh(2.3));
// negative number in radians
writeln("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 cosh
double result=cosh(180.0 * (PI / 180.0));
writeln("The value of cosh(180.0 * (PI / 180.0)): ", result);
//exceptional output
writeln ("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.math required for the cosh() 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 variable result first converts degrees into radians and then applies cosh().

  • Lines 22 to 26: We calculate the hyperbolic cosine of exceptional numbers using cosh().

Free Resources