124607Szliu /* 224607Szliu * Copyright (c) 1985 Regents of the University of California. 324607Szliu * 424607Szliu * Use and reproduction of this software are granted in accordance with 524607Szliu * the terms and conditions specified in the Berkeley Software License 624607Szliu * Agreement (in particular, this entails acknowledgement of the programs' 724607Szliu * source, and inclusion of this notice) with the additional understanding 824607Szliu * that all recipients should regard themselves as participants in an 924607Szliu * ongoing research project and hence should feel obligated to report 1024607Szliu * their experiences (good or bad) with these elementary function codes, 1124607Szliu * using "sendbug 4bsd-bugs@BERKELEY", to the authors. 1224607Szliu */ 1324607Szliu 1424607Szliu #ifndef lint 1524706Selefunt static char sccsid[] = 16*31853Szliu "@(#)tanh.c 4.3 (Berkeley) 8/21/85; 1.4 (ucb.elefunt) 07/13/87"; 17*31853Szliu #endif /* not lint */ 1824607Szliu 1924607Szliu /* TANH(X) 2024607Szliu * RETURN THE HYPERBOLIC TANGENT OF X 2124607Szliu * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS) 2224607Szliu * CODED IN C BY K.C. NG, 1/8/85; 2324607Szliu * REVISED BY K.C. NG on 2/8/85, 2/11/85, 3/7/85, 3/24/85. 2424607Szliu * 2524607Szliu * Required system supported functions : 2624607Szliu * copysign(x,y) 2724607Szliu * finite(x) 2824607Szliu * 2924607Szliu * Required kernel function: 3024607Szliu * expm1(x) ...exp(x)-1 3124607Szliu * 3224607Szliu * Method : 3324607Szliu * 1. reduce x to non-negative by tanh(-x) = - tanh(x). 3424607Szliu * 2. 3524607Szliu * 0 < x <= 1.e-10 : tanh(x) := x 3624607Szliu * -expm1(-2x) 3724607Szliu * 1.e-10 < x <= 1 : tanh(x) := -------------- 3824607Szliu * expm1(-2x) + 2 3924607Szliu * 2 4024607Szliu * 1 <= x <= 22.0 : tanh(x) := 1 - --------------- 4124607Szliu * expm1(2x) + 2 4224607Szliu * 22.0 < x <= INF : tanh(x) := 1. 4324607Szliu * 4424607Szliu * Note: 22 was chosen so that fl(1.0+2/(expm1(2*22)+2)) == 1. 4524607Szliu * 4624607Szliu * Special cases: 4724607Szliu * tanh(NaN) is NaN; 4824607Szliu * only tanh(0)=0 is exact for finite argument. 4924607Szliu * 5024607Szliu * Accuracy: 5124607Szliu * tanh(x) returns the exact hyperbolic tangent of x nealy rounded. 5224607Szliu * In a test run with 1,024,000 random arguments on a VAX, the maximum 5324607Szliu * observed error was 2.22 ulps (units in the last place). 5424607Szliu */ 5524607Szliu 5624607Szliu double tanh(x) 5724607Szliu double x; 5824607Szliu { 5924607Szliu static double one=1.0, two=2.0, small = 1.0e-10, big = 1.0e10; 6024607Szliu double expm1(), t, copysign(), sign; 6124607Szliu int finite(); 6224607Szliu 63*31853Szliu #if !defined(vax)&&!defined(tahoe) 6424607Szliu if(x!=x) return(x); /* x is NaN */ 65*31853Szliu #endif /* !defined(vax)&&!defined(tahoe) */ 6624607Szliu 6724607Szliu sign=copysign(one,x); 6824607Szliu x=copysign(x,one); 6924607Szliu if(x < 22.0) 7024607Szliu if( x > one ) 7124607Szliu return(copysign(one-two/(expm1(x+x)+two),sign)); 7224607Szliu else if ( x > small ) 7324607Szliu {t= -expm1(-(x+x)); return(copysign(t/(two-t),sign));} 7424607Szliu else /* raise the INEXACT flag for non-zero x */ 7524607Szliu {big+x; return(copysign(x,sign));} 7624607Szliu else if(finite(x)) 7724607Szliu return (sign+1.0E-37); /* raise the INEXACT flag */ 7824607Szliu else 7924607Szliu return(sign); /* x is +- INF */ 8024607Szliu } 81