xref: /csrg-svn/lib/libm/common_source/tanh.c (revision 42657)
134126Sbostic /*
224607Szliu  * Copyright (c) 1985 Regents of the University of California.
334126Sbostic  * All rights reserved.
434126Sbostic  *
5*42657Sbostic  * %sccs.include.redist.c%
634126Sbostic  *
734126Sbostic  * All recipients should regard themselves as participants in an ongoing
834126Sbostic  * research project and hence should feel obligated to report their
934126Sbostic  * experiences (good or bad) with these elementary function codes, using
1034126Sbostic  * the sendbug(8) program, to the authors.
1124607Szliu  */
1224607Szliu 
1324607Szliu #ifndef lint
14*42657Sbostic static char sccsid[] = "@(#)tanh.c	5.4 (Berkeley) 06/01/90";
1534126Sbostic #endif /* not lint */
1624607Szliu 
1724607Szliu /* TANH(X)
1824607Szliu  * RETURN THE HYPERBOLIC TANGENT OF X
1924607Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
2024607Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2124607Szliu  * REVISED BY K.C. NG on 2/8/85, 2/11/85, 3/7/85, 3/24/85.
2224607Szliu  *
2324607Szliu  * Required system supported functions :
2424607Szliu  *	copysign(x,y)
2524607Szliu  *	finite(x)
2624607Szliu  *
2724607Szliu  * Required kernel function:
2824607Szliu  *	expm1(x)	...exp(x)-1
2924607Szliu  *
3024607Szliu  * Method :
3124607Szliu  *	1. reduce x to non-negative by tanh(-x) = - tanh(x).
3224607Szliu  *	2.
3324607Szliu  *	    0      <  x <=  1.e-10 :  tanh(x) := x
3424607Szliu  *					          -expm1(-2x)
3524607Szliu  *	    1.e-10 <  x <=  1      :  tanh(x) := --------------
3624607Szliu  *					         expm1(-2x) + 2
3724607Szliu  *							  2
3824607Szliu  *	    1      <= x <=  22.0   :  tanh(x) := 1 -  ---------------
3924607Szliu  *						      expm1(2x) + 2
4024607Szliu  *	    22.0   <  x <= INF     :  tanh(x) := 1.
4124607Szliu  *
4224607Szliu  *	Note: 22 was chosen so that fl(1.0+2/(expm1(2*22)+2)) == 1.
4324607Szliu  *
4424607Szliu  * Special cases:
4524607Szliu  *	tanh(NaN) is NaN;
4624607Szliu  *	only tanh(0)=0 is exact for finite argument.
4724607Szliu  *
4824607Szliu  * Accuracy:
4924607Szliu  *	tanh(x) returns the exact hyperbolic tangent of x nealy rounded.
5024607Szliu  *	In a test run with 1,024,000 random arguments on a VAX, the maximum
5124607Szliu  *	observed error was 2.22 ulps (units in the last place).
5224607Szliu  */
5324607Szliu 
5424607Szliu double tanh(x)
5524607Szliu double x;
5624607Szliu {
5724607Szliu 	static double one=1.0, two=2.0, small = 1.0e-10, big = 1.0e10;
5824607Szliu 	double expm1(), t, copysign(), sign;
5924607Szliu 	int finite();
6024607Szliu 
6131853Szliu #if !defined(vax)&&!defined(tahoe)
6224607Szliu 	if(x!=x) return(x);	/* x is NaN */
6331853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
6424607Szliu 
6524607Szliu 	sign=copysign(one,x);
6624607Szliu 	x=copysign(x,one);
6724607Szliu 	if(x < 22.0)
6824607Szliu 	    if( x > one )
6924607Szliu 		return(copysign(one-two/(expm1(x+x)+two),sign));
7024607Szliu 	    else if ( x > small )
7124607Szliu 		{t= -expm1(-(x+x)); return(copysign(t/(two-t),sign));}
7224607Szliu 	    else		/* raise the INEXACT flag for non-zero x */
7324607Szliu 		{big+x; return(copysign(x,sign));}
7424607Szliu 	else if(finite(x))
7524607Szliu 	    return (sign+1.0E-37); /* raise the INEXACT flag */
7624607Szliu 	else
7724607Szliu 	    return(sign);	/* x is +- INF */
7824607Szliu }
79