Here you can find the source of tanh(double x)
Parameter | Description |
---|---|
x | the argument |
public static double tanh(double x)
//package com.java2s; /*/*w w w . j a va 2 s .co m*/ * Copyright 1998-2007 The Brookings Institution, NuTech Solutions,Inc., Metascape LLC, and contributors. * All rights reserved. * This program and the accompanying materials are made available solely under the BSD license "ascape-license.txt". * Any referenced or included libraries carry licenses of their respective copyright holders. */ public class Main { /** * A function that returns the hyperbolic tangent of its argument. * Note that this method has its worst precision around |x| = 1.0e-6, * but it always provides at least 11 significant digits * @param x the argument */ public static double tanh(double x) { double absX = Math.abs(x); if (absX > 22.0) { // tanh(22) equals 0.9999999999999999998 which equals 1.0 to within double precision return (x > 0.0) ? 1.0 : -1.0; } else if (absX > 1.0e-6) { double expX = Math.exp(x); double expMX = Math.exp(-x); return (expX - expMX) / (expX + expMX); } else { // absX <= 1.0e-6 return x; } } }