base/math/s_tanh.c

Go to the documentation of this file.
00001 /* @(#)s_tanh.c 5.1 93/09/24 */ 00002 /* 00003 * ==================================================== 00004 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00005 * 00006 * Developed at SunPro, a Sun Microsystems, Inc. business. 00007 * Permission to use, copy, modify, and distribute this 00008 * software is freely granted, provided that this notice 00009 * is preserved. 00010 * ==================================================== 00011 */ 00012 00013 #if defined(LIBM_SCCS) && !defined(lint) 00014 static char rcsid[] = "$NetBSD: s_tanh.c,v 1.7 1995/05/10 20:48:22 jtc Exp $"; 00015 #endif 00016 00017 /* Tanh(x) 00018 * Return the Hyperbolic Tangent of x 00019 * 00020 * Method : 00021 * x -x 00022 * e - e 00023 * 0. tanh(x) is defined to be ----------- 00024 * x -x 00025 * e + e 00026 * 1. reduce x to non-negative by tanh(-x) = -tanh(x). 00027 * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) 00028 * -t 00029 * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) 00030 * t + 2 00031 * 2 00032 * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x) 00033 * t + 2 00034 * 22.0 < x <= INF : tanh(x) := 1. 00035 * 00036 * Special cases: 00037 * tanh(NaN) is NaN; 00038 * only tanh(0)=0 is exact for finite argument. 00039 */ 00040 00041 #include "math.h" 00042 #include "mathP.h" 00043 00044 #ifdef __STDC__ 00045 static const double one=1.0, two=2.0, tiny = 1.0e-300; 00046 #else 00047 static double one=1.0, two=2.0, tiny = 1.0e-300; 00048 #endif 00049 00050 #ifdef __STDC__ 00051 double tanh(double x) 00052 #else 00053 double tanh(x) 00054 double x; 00055 #endif 00056 { 00057 double t,z; 00058 int32_t jx,ix; 00059 00060 /* High word of |x|. */ 00061 GET_HIGH_WORD(jx,x); 00062 ix = jx&0x7fffffff; 00063 00064 /* x is INF or NaN */ 00065 if(ix>=0x7ff00000) { 00066 if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ 00067 else return one/x-one; /* tanh(NaN) = NaN */ 00068 } 00069 00070 /* |x| < 22 */ 00071 if (ix < 0x40360000) { /* |x|<22 */ 00072 if (ix<0x3c800000) /* |x|<2**-55 */ 00073 return x*(one+x); /* tanh(small) = small */ 00074 if (ix>=0x3ff00000) { /* |x|>=1 */ 00075 t = expm1(two*fabs(x)); 00076 z = one - two/(t+two); 00077 } else { 00078 t = expm1(-two*fabs(x)); 00079 z= -t/(t+two); 00080 } 00081 /* |x| > 22, return +-1 */ 00082 } else { 00083 z = one - tiny; /* raised inexact flag */ 00084 } 00085 return (jx>=0)? z: -z; 00086 }

Generated on Thu Nov 20 11:49:52 2008 for RTAI API by doxygen 1.3.8