Wie gibt man den hyperbolischen Tangens eines Double-Werts in Java zurück?


Es gibt eine Methode in der Java-Klasse Math, die den hyperbolischen Tangens eines Double-Werts zurückgibt. Die Methode heißt tanh() und hat die folgende Signatur:

public static double tanh(double x)

Hier ist ein Beispiel, das zeigt, wie man tanh() verwendet:

double x = 2.0;
double tanhValue = Math.tanh(x);
System.out.println("The hyperbolic tangent of " + x + " is " + tanhValue);

Die Ausgabe dieses Codes wird sein:

The hyperbolic tangent of 2.0 is 0.9640275800758169

Es gibt auch eine Möglichkeit, den hyperbolischen Tangens mithilfe der Formel tanh(x) = (e^x - e^-x) / (e^x + e^-x) zu berechnen. Hier ist ein Beispiel, das diese Formel verwendet:

double x = 2.0;
double tanhValue = (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x));
System.out.println("The hyperbolic tangent of " + x + " is " + tanhValue);

Dieses Beispiel gibt die gleiche Ausgabe wie das vorherige Beispiel zurück.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.