Java examples for java.lang:Math Trigonometric Function
Returns the hyperbolic tangent of a double.
/*//from ww w . j a v a 2s .c o m * ------------------------------------------------------------------------= * Copyright (C) 1997 - 1998 by Visual Numerics, Inc. All rights reserved. * * Permission to use, copy, modify, and distribute this software is freely * granted by Visual Numerics, Inc., provided that the copyright notice * above and the following warranty disclaimer are preserved in human * readable form. * * Because this software is licenses free of charge, it is provided * "AS IS", with NO WARRANTY. TO THE EXTENT PERMITTED BY LAW, VNI * DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO ITS PERFORMANCE, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. * VNI WILL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING OUT OF THE USE * OF OR INABILITY TO USE THIS SOFTWARE, INCLUDING BUT NOT LIMITED TO DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, PUNITIVE, AND EXEMPLARY DAMAGES, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * ------------------------------------------------------------------------= */ //package com.java2s; public class Main { private static final double TANH_COEF[] = { -.25828756643634710, -.11836106330053497, .009869442648006398, -.000835798662344582, .000070904321198943, -.000006016424318120, .000000510524190800, -.000000043320729077, .000000003675999055, -.000000000311928496, .000000000026468828, -.000000000002246023, .000000000000190587, -.000000000000016172, .000000000000001372, -.000000000000000116, .000000000000000009 }; /** * Returns the hyperbolic tangent of a double. * * @param x * A double value. * @return The hyperbolic tangent of x. */ static public double tanh(double x) { double ans, y; y = Math.abs(x); if (Double.isNaN(x)) { ans = Double.NaN; } else if (y < 1.82501e-08) { // 1.82501e-08 = Math.sqrt(3.0*EPSILON_SMALL) ans = x; } else if (y <= 1.0) { ans = x * (1.0 + csevl(2.0 * x * x - 1.0, TANH_COEF)); } else if (y < 7.977294885) { // 7.977294885 = -0.5*Math.log(EPSILON_SMALL) y = Math.exp(y); ans = sign((y - 1.0 / y) / (y + 1.0 / y), x); } else { ans = sign(1.0, x); } return ans; } static double csevl(double x, double coef[]) { double b0, b1, b2, twox; int i; b1 = 0.0; b0 = 0.0; b2 = 0.0; twox = 2.0 * x; for (i = coef.length - 1; i >= 0; i--) { b2 = b1; b1 = b0; b0 = twox * b1 - b2 + coef[i]; } return 0.5 * (b0 - b2); } static private double sign(double x, double y) { double abs_x = ((x < 0) ? -x : x); return (y < 0.0) ? -abs_x : abs_x; } }