Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / math / tanh.c
1 /*
2  * (c) copyright 1988 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  *
5  * Author: Ceriel J.H. Jacobs
6  */
7 /* $Id: tanh.c,v 1.4 1994/06/24 11:44:25 ceriel Exp $ */
8
9 #include        <float.h>
10 #include        <math.h>
11 #include        <errno.h>
12 #include        "localmath.h"
13
14 double
15 tanh(double x)
16 {
17         /*      Algorithm and coefficients from:
18                         "Software manual for the elementary functions"
19                         by W.J. Cody and W. Waite, Prentice-Hall, 1980
20         */
21
22         static double p[] = {
23                 -0.16134119023996228053e+4,
24                 -0.99225929672236083313e+2,
25                 -0.96437492777225469787e+0
26         };
27         static double q[] = {
28                  0.48402357071988688686e+4,
29                  0.22337720718962312926e+4,
30                  0.11274474380534949335e+3,
31                  1.0
32         };
33         int     negative = x < 0;
34
35         if (__IsNan(x)) {
36                 errno = EDOM;
37                 return x;
38         }
39         if (negative) x = -x;
40
41         if (x >= 0.5*M_LN_MAX_D) {
42                 x = 1.0;
43         }
44 #define LN3D2   0.54930614433405484570e+0       /* ln(3)/2 */
45         else if (x > LN3D2) {
46                 x = 0.5 - 1.0/(exp(x+x)+1.0);
47                 x += x;
48         }
49         else {
50                 /* ??? avoid underflow ??? */
51                 double g = x*x;
52                 x += x * g * POLYNOM2(g, p)/POLYNOM3(g, q);
53         }
54         return negative ? -x : x;
55 }