POK
|
00001 /* 00002 * POK header 00003 * 00004 * The following file is a part of the POK project. Any modification should 00005 * made according to the POK licence. You CANNOT use this file or a part of 00006 * this file is this part of a file for your own project 00007 * 00008 * For more information on the POK licence, please see our LICENCE FILE 00009 * 00010 * Please follow the coding guidelines described in doc/CODING_GUIDELINES 00011 * 00012 * Copyright (c) 2007-2009 POK team 00013 * 00014 * Created by julien on Fri Jan 30 14:41:34 2009 00015 */ 00016 00017 /* @(#)s_tanh.c 5.1 93/09/24 */ 00018 /* 00019 * ==================================================== 00020 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 00021 * 00022 * Developed at SunPro, a Sun Microsystems, Inc. business. 00023 * Permission to use, copy, modify, and distribute this 00024 * software is freely granted, provided that this notice 00025 * is preserved. 00026 * ==================================================== 00027 */ 00028 00029 #ifdef POK_NEEDS_LIBMATH 00030 00031 /* Tanh(x) 00032 * Return the Hyperbolic Tangent of x 00033 * 00034 * Method : 00035 * x -x 00036 * e - e 00037 * 0. tanh(x) is defined to be ----------- 00038 * x -x 00039 * e + e 00040 * 1. reduce x to non-negative by tanh(-x) = -tanh(x). 00041 * 2. 0 <= x <= 2**-55 : tanh(x) := x*(one+x) 00042 * -t 00043 * 2**-55 < x <= 1 : tanh(x) := -----; t = expm1(-2x) 00044 * t + 2 00045 * 2 00046 * 1 <= x <= 22.0 : tanh(x) := 1- ----- ; t=expm1(2x) 00047 * t + 2 00048 * 22.0 < x <= INF : tanh(x) := 1. 00049 * 00050 * Special cases: 00051 * tanh(NaN) is NaN; 00052 * only tanh(0)=0 is exact for finite argument. 00053 */ 00054 00055 #include <libm.h> 00056 #include "math_private.h" 00057 00058 static const double one=1.0, two=2.0, tiny = 1.0e-300; 00059 00060 double 00061 tanh(double x) 00062 { 00063 double t,z; 00064 int32_t jx,ix; 00065 00066 /* High word of |x|. */ 00067 GET_HIGH_WORD(jx,x); 00068 ix = jx&0x7fffffff; 00069 00070 /* x is INF or NaN */ 00071 if(ix>=0x7ff00000) { 00072 if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ 00073 else return one/x-one; /* tanh(NaN) = NaN */ 00074 } 00075 00076 /* |x| < 22 */ 00077 if (ix < 0x40360000) { /* |x|<22 */ 00078 if (ix<0x3c800000) /* |x|<2**-55 */ 00079 return x*(one+x); /* tanh(small) = small */ 00080 if (ix>=0x3ff00000) { /* |x|>=1 */ 00081 t = expm1(two*fabs(x)); 00082 z = one - two/(t+two); 00083 } else { 00084 t = expm1(-two*fabs(x)); 00085 z= -t/(t+two); 00086 } 00087 /* |x| > 22, return +-1 */ 00088 } else { 00089 z = one - tiny; /* raised inexact flag */ 00090 } 00091 return (jx>=0)? z: -z; 00092 } 00093 00094 #endif