|
|
1.1 root 1: /*
2: * libm/tanh.c
3: * Hyperbolic tangent.
4: */
5:
6: #include <math.h>
7:
8: #if EMU87
9: #include "emumath.h"
10: #endif
11:
12: /*
13: * To get a correct result for very small |x|,
14: * the code below just returns x for |x| < THRESHOLD.
15: * We can derive a theoretical value for THRESHOLD from the series:
16: * tanh(x) = x - x^3/3 + 2*x^5/17 - ...
17: * so if |x| < sqrt(3 * DBL_EPSILON) then x^3/3 < x * DBL_EPSILON
18: * and the low-order terms must be insignificant.
19: * The threshold value below, arrived at empirically, is somewhat larger;
20: * it is for IEEE fp, the DECVAX value must be slightly different but...
21: */
22: #define THRESHOLD 7.000900090031528e-06
23:
24: double
25: tanh(x) double x;
26: {
27: double r;
28: register int s;
29:
30: if (fabs(x) < THRESHOLD)
31: return x;
32: /*
33: * Hart (6.2.28) gives an alternative formula for |x| < 1,
34: * unused here; should it be used?
35: */
36: if (s = (x < 0.0))
37: x = -x;
38: r = exp(-2.0*x);
39: r = (1.0-r) / (1.0+r);
40: return s ? -r : r;
41: }
42:
43: /* end of tanh.c */
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.