xref: /csrg-svn/lib/libm/common_source/tanh.c (revision 24607)
1*24607Szliu /*
2*24607Szliu  * Copyright (c) 1985 Regents of the University of California.
3*24607Szliu  *
4*24607Szliu  * Use and reproduction of this software are granted  in  accordance  with
5*24607Szliu  * the terms and conditions specified in  the  Berkeley  Software  License
6*24607Szliu  * Agreement (in particular, this entails acknowledgement of the programs'
7*24607Szliu  * source, and inclusion of this notice) with the additional understanding
8*24607Szliu  * that  all  recipients  should regard themselves as participants  in  an
9*24607Szliu  * ongoing  research  project and hence should  feel  obligated  to report
10*24607Szliu  * their  experiences (good or bad) with these elementary function  codes,
11*24607Szliu  * using "sendbug 4bsd-bugs@BERKELEY", to the authors.
12*24607Szliu  */
13*24607Szliu 
14*24607Szliu #ifndef lint
15*24607Szliu static char sccsid[] = "@(#)tanh.c	1.1 (ELEFUNT) 09/06/85";
16*24607Szliu #endif not lint
17*24607Szliu 
18*24607Szliu /* TANH(X)
19*24607Szliu  * RETURN THE HYPERBOLIC TANGENT OF X
20*24607Szliu  * DOUBLE PRECISION (VAX D FORMAT 56 BITS, IEEE DOUBLE 53 BITS)
21*24607Szliu  * CODED IN C BY K.C. NG, 1/8/85;
22*24607Szliu  * REVISED BY K.C. NG on 2/8/85, 2/11/85, 3/7/85, 3/24/85.
23*24607Szliu  *
24*24607Szliu  * Required system supported functions :
25*24607Szliu  *	copysign(x,y)
26*24607Szliu  *	finite(x)
27*24607Szliu  *
28*24607Szliu  * Required kernel function:
29*24607Szliu  *	expm1(x)	...exp(x)-1
30*24607Szliu  *
31*24607Szliu  * Method :
32*24607Szliu  *	1. reduce x to non-negative by tanh(-x) = - tanh(x).
33*24607Szliu  *	2.
34*24607Szliu  *	    0      <  x <=  1.e-10 :  tanh(x) := x
35*24607Szliu  *					          -expm1(-2x)
36*24607Szliu  *	    1.e-10 <  x <=  1      :  tanh(x) := --------------
37*24607Szliu  *					         expm1(-2x) + 2
38*24607Szliu  *							  2
39*24607Szliu  *	    1      <= x <=  22.0   :  tanh(x) := 1 -  ---------------
40*24607Szliu  *						      expm1(2x) + 2
41*24607Szliu  *	    22.0   <  x <= INF     :  tanh(x) := 1.
42*24607Szliu  *
43*24607Szliu  *	Note: 22 was chosen so that fl(1.0+2/(expm1(2*22)+2)) == 1.
44*24607Szliu  *
45*24607Szliu  * Special cases:
46*24607Szliu  *	tanh(NaN) is NaN;
47*24607Szliu  *	only tanh(0)=0 is exact for finite argument.
48*24607Szliu  *
49*24607Szliu  * Accuracy:
50*24607Szliu  *	tanh(x) returns the exact hyperbolic tangent of x nealy rounded.
51*24607Szliu  *	In a test run with 1,024,000 random arguments on a VAX, the maximum
52*24607Szliu  *	observed error was 2.22 ulps (units in the last place).
53*24607Szliu  */
54*24607Szliu 
55*24607Szliu double tanh(x)
56*24607Szliu double x;
57*24607Szliu {
58*24607Szliu 	static double one=1.0, two=2.0, small = 1.0e-10, big = 1.0e10;
59*24607Szliu 	double expm1(), t, copysign(), sign;
60*24607Szliu 	int finite();
61*24607Szliu 
62*24607Szliu #ifndef VAX
63*24607Szliu 	if(x!=x) return(x);	/* x is NaN */
64*24607Szliu #endif
65*24607Szliu 
66*24607Szliu 	sign=copysign(one,x);
67*24607Szliu 	x=copysign(x,one);
68*24607Szliu 	if(x < 22.0)
69*24607Szliu 	    if( x > one )
70*24607Szliu 		return(copysign(one-two/(expm1(x+x)+two),sign));
71*24607Szliu 	    else if ( x > small )
72*24607Szliu 		{t= -expm1(-(x+x)); return(copysign(t/(two-t),sign));}
73*24607Szliu 	    else		/* raise the INEXACT flag for non-zero x */
74*24607Szliu 		{big+x; return(copysign(x,sign));}
75*24607Szliu 	else if(finite(x))
76*24607Szliu 	    return (sign+1.0E-37); /* raise the INEXACT flag */
77*24607Szliu 	else
78*24607Szliu 	    return(sign);	/* x is +- INF */
79*24607Szliu }
80