xref: /csrg-svn/lib/libm/common_source/tanh.c (revision 34126)
1*34126Sbostic /*
224607Szliu  * Copyright (c) 1985 Regents of the University of California.
3*34126Sbostic  * All rights reserved.
4*34126Sbostic  *
5*34126Sbostic  * Redistribution and use in source and binary forms are permitted
6*34126Sbostic  * provided that this notice is preserved and that due credit is given
7*34126Sbostic  * to the University of California at Berkeley. The name of the University
8*34126Sbostic  * may not be used to endorse or promote products derived from this
9*34126Sbostic  * software without specific prior written permission. This software
10*34126Sbostic  * is provided ``as is'' without express or implied warranty.
11*34126Sbostic  *
12*34126Sbostic  * All recipients should regard themselves as participants in an ongoing
13*34126Sbostic  * research project and hence should feel obligated to report their
14*34126Sbostic  * experiences (good or bad) with these elementary function codes, using
15*34126Sbostic  * the sendbug(8) program, to the authors.
1624607Szliu  */
1724607Szliu 
1824607Szliu #ifndef lint
19*34126Sbostic static char sccsid[] = "@(#)tanh.c	5.2 (Berkeley) 04/29/88";
20*34126Sbostic #endif /* not lint */
2124607Szliu 
2224607Szliu /* TANH(X)
2324607Szliu  * RETURN THE HYPERBOLIC TANGENT OF X
2424607Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
2524607Szliu  * CODED IN C BY K.C. NG, 1/8/85;
2624607Szliu  * REVISED BY K.C. NG on 2/8/85, 2/11/85, 3/7/85, 3/24/85.
2724607Szliu  *
2824607Szliu  * Required system supported functions :
2924607Szliu  *	copysign(x,y)
3024607Szliu  *	finite(x)
3124607Szliu  *
3224607Szliu  * Required kernel function:
3324607Szliu  *	expm1(x)	...exp(x)-1
3424607Szliu  *
3524607Szliu  * Method :
3624607Szliu  *	1. reduce x to non-negative by tanh(-x) = - tanh(x).
3724607Szliu  *	2.
3824607Szliu  *	    0      <  x <=  1.e-10 :  tanh(x) := x
3924607Szliu  *					          -expm1(-2x)
4024607Szliu  *	    1.e-10 <  x <=  1      :  tanh(x) := --------------
4124607Szliu  *					         expm1(-2x) + 2
4224607Szliu  *							  2
4324607Szliu  *	    1      <= x <=  22.0   :  tanh(x) := 1 -  ---------------
4424607Szliu  *						      expm1(2x) + 2
4524607Szliu  *	    22.0   <  x <= INF     :  tanh(x) := 1.
4624607Szliu  *
4724607Szliu  *	Note: 22 was chosen so that fl(1.0+2/(expm1(2*22)+2)) == 1.
4824607Szliu  *
4924607Szliu  * Special cases:
5024607Szliu  *	tanh(NaN) is NaN;
5124607Szliu  *	only tanh(0)=0 is exact for finite argument.
5224607Szliu  *
5324607Szliu  * Accuracy:
5424607Szliu  *	tanh(x) returns the exact hyperbolic tangent of x nealy rounded.
5524607Szliu  *	In a test run with 1,024,000 random arguments on a VAX, the maximum
5624607Szliu  *	observed error was 2.22 ulps (units in the last place).
5724607Szliu  */
5824607Szliu 
5924607Szliu double tanh(x)
6024607Szliu double x;
6124607Szliu {
6224607Szliu 	static double one=1.0, two=2.0, small = 1.0e-10, big = 1.0e10;
6324607Szliu 	double expm1(), t, copysign(), sign;
6424607Szliu 	int finite();
6524607Szliu 
6631853Szliu #if !defined(vax)&&!defined(tahoe)
6724607Szliu 	if(x!=x) return(x);	/* x is NaN */
6831853Szliu #endif	/* !defined(vax)&&!defined(tahoe) */
6924607Szliu 
7024607Szliu 	sign=copysign(one,x);
7124607Szliu 	x=copysign(x,one);
7224607Szliu 	if(x < 22.0)
7324607Szliu 	    if( x > one )
7424607Szliu 		return(copysign(one-two/(expm1(x+x)+two),sign));
7524607Szliu 	    else if ( x > small )
7624607Szliu 		{t= -expm1(-(x+x)); return(copysign(t/(two-t),sign));}
7724607Szliu 	    else		/* raise the INEXACT flag for non-zero x */
7824607Szliu 		{big+x; return(copysign(x,sign));}
7924607Szliu 	else if(finite(x))
8024607Szliu 	    return (sign+1.0E-37); /* raise the INEXACT flag */
8124607Szliu 	else
8224607Szliu 	    return(sign);	/* x is +- INF */
8324607Szliu }
84