The tanh() function in D returns a number’s hyperbolic tangent.
Figure 1 shows the mathematical representation of the tanh() function:
Note: We need to import
std.mathin our code to use thetanh()function. We can import it like this:import std.math.
tanh(num)
This function requires a number representing an angle in radians as a parameter.
To convert degrees to radians, use:
radians = degrees * ( PI / 180.0 )
tanh() returns the hyperbolic tangent of a number (radians) that is set as a parameter.
The code below shows the use of the tanh() 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 tanh(2.3) ", tanh(2.3));// negative number in radianswriteln("The value of tanh(-2.3) ", tanh(-2.3));//converting the degrees angle into radians and then applying tanh()// degrees = 45.0// PI = 3.14159265// result first converts degrees to radians then apply tanhdouble result=tanh(45.0 * (PI / 180.0));writeln("The value of tanh(45.0 * (PI / 180.0)) ", result);//exceptional outputwriteln ("The value of tanh(real.infinity) : ",tanh(real.infinity));writeln ("The value of tanh(-real.infinity) : ",tanh(-real.infinity));writeln ("The value of tanh(real.nan) : ",tanh(real.nan));writeln ("The value of tanh(-real.nan) : ",tanh(-real.nan));return 0;}
Line 4: We add the header std.math required for the tanh() function.
Line 9: We calculate the hyperbolic tan of positive numbers in radians using tanh().
Line 12: We calculate the hyperbolic tan of a negative number in radians using tanh().
Line 18: We convert the degrees into radians and then apply the tanh() function. The variable result first converts degrees to radians and then applies tanh().
Line 21 onwards: We calculate the hyperbolic tan of exceptional numbers using tanh()