Path: blob/master/src/java.base/share/native/libfdlibm/s_tanh.c
41152 views
/*1* Copyright (c) 1998, 2001, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/* Tanh(x)26* Return the Hyperbolic Tangent of x27*28* Method :29* x -x30* e - e31* 0. tanh(x) is defined to be -----------32* x -x33* e + e34* 1. reduce x to non-negative by tanh(-x) = -tanh(x).35* 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x)36* -t37* 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x)38* t + 239* 240* 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x)41* t + 242* 22.0 < x <= INF : tanh(x) := 1.43*44* Special cases:45* tanh(NaN) is NaN;46* only tanh(0)=0 is exact for finite argument.47*/4849#include "fdlibm.h"5051#ifdef __STDC__52static const double one=1.0, two=2.0, tiny = 1.0e-300;53#else54static double one=1.0, two=2.0, tiny = 1.0e-300;55#endif5657#ifdef __STDC__58double tanh(double x)59#else60double tanh(x)61double x;62#endif63{64double t,z;65int jx,ix;6667/* High word of |x|. */68jx = __HI(x);69ix = jx&0x7fffffff;7071/* x is INF or NaN */72if(ix>=0x7ff00000) {73if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */74else return one/x-one; /* tanh(NaN) = NaN */75}7677/* |x| < 22 */78if (ix < 0x40360000) { /* |x|<22 */79if (ix<0x3c800000) /* |x|<2**-55 */80return x*(one+x); /* tanh(small) = small */81if (ix>=0x3ff00000) { /* |x|>=1 */82t = expm1(two*fabs(x));83z = one - two/(t+two);84} else {85t = expm1(-two*fabs(x));86z= -t/(t+two);87}88/* |x| > 22, return +-1 */89} else {90z = one - tiny; /* raised inexact flag */91}92return (jx>=0)? z: -z;93}949596