sl@0: /* S_TANH.C sl@0: * sl@0: * Portions Copyright (c) 1993-1999 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: */ sl@0: sl@0: sl@0: /* @(#)s_tanh.c 5.1 93/09/24 */ sl@0: /* sl@0: * ==================================================== sl@0: * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. sl@0: * sl@0: * Developed at SunPro, a Sun Microsystems, Inc. business. sl@0: * Permission to use, copy, modify, and distribute this sl@0: * software is freely granted, provided that this notice sl@0: * is preserved. sl@0: * ==================================================== sl@0: */ sl@0: sl@0: /* sl@0: sl@0: FUNCTION sl@0: <>, <>---hyperbolic tangent sl@0: sl@0: INDEX sl@0: tanh sl@0: INDEX sl@0: tanhf sl@0: sl@0: ANSI_SYNOPSIS sl@0: #include sl@0: double tanh(double <[x]>); sl@0: float tanhf(float <[x]>); sl@0: sl@0: TRAD_SYNOPSIS sl@0: #include sl@0: double tanh(<[x]>) sl@0: double <[x]>; sl@0: sl@0: float tanhf(<[x]>) sl@0: float <[x]>; sl@0: sl@0: sl@0: DESCRIPTION sl@0: sl@0: <> computes the hyperbolic tangent of sl@0: the argument <[x]>. Angles are specified in radians. sl@0: sl@0: <)>> is defined as sl@0: . sinh(<[x]>)/cosh(<[x]>) sl@0: sl@0: <> is identical, save that it takes and returns <> values. sl@0: sl@0: RETURNS sl@0: The hyperbolic tangent of <[x]> is returned. sl@0: sl@0: PORTABILITY sl@0: <> is ANSI C. <> is an extension. sl@0: sl@0: */ sl@0: sl@0: /* Tanh(x) sl@0: * Return the Hyperbolic Tangent of x sl@0: * sl@0: * Method : sl@0: * x -x sl@0: * e - e sl@0: * 0. tanh(x) is defined to be ----------- sl@0: * x -x sl@0: * e + e sl@0: * 1. reduce x to non-negative by tanh(-x) = -tanh(x). sl@0: * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) sl@0: * -t sl@0: * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) sl@0: * t + 2 sl@0: * 2 sl@0: * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x) sl@0: * t + 2 sl@0: * 22.0 < x <= INF : tanh(x) := 1. sl@0: * sl@0: * Special cases: sl@0: * tanh(NaN) is NaN; sl@0: * only tanh(0)=0 is exact for finite argument. sl@0: */ sl@0: sl@0: #include "FDLIBM.H" sl@0: sl@0: static const double one=1.0, two=2.0, tiny = 1.0e-300; sl@0: sl@0: /** sl@0: Calculate hyperbolic tangent. sl@0: @return hyperbolic tangent of x. sl@0: @param x Angle expressed in radians (180 degrees = PI radians). sl@0: */ sl@0: EXPORT_C double tanh(double x) __SOFTFP sl@0: { sl@0: double t,z; sl@0: __int32_t jx,ix; sl@0: sl@0: /* High word of |x|. */ sl@0: GET_HIGH_WORD(jx,x); sl@0: ix = jx&0x7fffffff; sl@0: sl@0: /* x is INF or NaN */ sl@0: if(ix>=0x7ff00000) { sl@0: if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ sl@0: else return one/x-one; /* tanh(NaN) = NaN */ sl@0: } sl@0: sl@0: /* |x| < 22 */ sl@0: if (ix < 0x40360000) { /* |x|<22 */ sl@0: if (ix<0x3c800000) /* |x|<2**-55 */ sl@0: return x*(one+x); /* tanh(small) = small */ sl@0: if (ix>=0x3ff00000) { /* |x|>=1 */ sl@0: t = expm1(two*fabs(x)); sl@0: z = one - two/(t+two); sl@0: } else { sl@0: t = expm1(-two*fabs(x)); sl@0: z= -t/(t+two); sl@0: } sl@0: /* |x| > 22, return +-1 */ sl@0: } else { sl@0: z = one - tiny; /* raised inexact flag */ sl@0: } sl@0: return (jx>=0)? z: -z; sl@0: }