Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
math
angle
communitycreator

What is Math.tanh() in Java?

Tarun Telang

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Java has a built-in Math class defined in the java.lang package that contains important functions to perform basic mathematical operations.

The Math class includes the tanh() method, which is used to compute a specified angle’s hyperbolic tangent, denoted as tanh.

Method definition

public static double tanh(double angle);

Parameter

angle represents the double value that corresponds to the angle for which we have to find the hyperbolic tangent in radians.

Return value

The function returns the double value that corresponds to the hyperbolic tangent of angle.

If the value of angle is NaNnot a number, then the returned value is NaN.

Note: To convert an angle measured in radians to an equivalent angle measured in degrees and vice versa, you can use the built-in Math.toDegrees(double angle_in_randians) and Math.toRadians(double angle_in_degrees) methods.

Code

class HelloWorld {
public static void main( String args[] ) {
double result = Math.tanh(1);
System.out.println("tanh(1) = " + result);
result = Math.tanh(0);
System.out.println("tanh(0) = " + result);
result = Math.tanh(Math.toRadians(30));
System.out.println("tanh(30°) = " + result);
result = Math.tanh(Math.toRadians(45));
System.out.println("tanh(45°) = " + result);
result = Math.tanh(Math.toRadians(60));
System.out.println("tanh(60°) = " + result);
result = Math.tanh(Math.toRadians(90));
System.out.println("tanh(90°) = " + result);
result = Math.tanh(Double.NaN);
System.out.println("tanh(NaN) = " + result);
result = Math.tanh(Double.POSITIVE_INFINITY);
System.out.println("tanh(∞) = " + result);
}
}
Example code for Math.tanh() in Java

RELATED TAGS

java
math
angle
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring