50 *
51 * Note: 22 was chosen so that fl(1.0+2/(expm1(2*22)+2)) == 1.
52 *
53 * Special cases:
54 * tanh(NaN) is NaN;
55 * only tanh(0)=0 is exact for finite argument.
56 */
57
58 #include "libm.h"
59 #include "libm_protos.h"
60 #include <math.h>
61
62 static const double
63 one = 1.0,
64 two = 2.0,
65 small = 1.0e-10,
66 big = 1.0e10;
67 /* INDENT ON */
68
69 double
70 tanh(double x) {
71 double t, y, z;
72 int signx;
73 volatile double dummy __unused;
74
75 if (isnan(x))
76 return (x * x); /* + -> * for Cheetah */
77 signx = signbit(x);
78 t = fabs(x);
79 z = one;
80 if (t <= 22.0) {
81 if (t > one)
82 z = one - two / (expm1(t + t) + two);
83 else if (t > small) {
84 y = expm1(-t - t);
85 z = -y / (y + two);
86 } else {
87 /* raise the INEXACT flag for non-zero t */
88 dummy = t + big;
89 #ifdef lint
90 dummy = dummy;
91 #endif
92 return (x);
93 }
94 } else if (!finite(t))
95 return (copysign(1.0, x));
96 else
97 return (signx == 1 ? -z + small * small : z - small * small);
98
99 return (signx == 1 ? -z : z);
100 }
|
50 *
51 * Note: 22 was chosen so that fl(1.0+2/(expm1(2*22)+2)) == 1.
52 *
53 * Special cases:
54 * tanh(NaN) is NaN;
55 * only tanh(0)=0 is exact for finite argument.
56 */
57
58 #include "libm.h"
59 #include "libm_protos.h"
60 #include <math.h>
61
62 static const double
63 one = 1.0,
64 two = 2.0,
65 small = 1.0e-10,
66 big = 1.0e10;
67 /* INDENT ON */
68
69 double
70 tanh(double x)
71 {
72 double t, y, z;
73 int signx;
74 volatile double dummy __unused;
75
76 if (isnan(x))
77 return (x * x); /* + -> * for Cheetah */
78 signx = signbit(x);
79 t = fabs(x);
80 z = one;
81 if (t <= 22.0) {
82 if (t > one)
83 z = one - two / (expm1(t + t) + two);
84 else if (t > small) {
85 y = expm1(-t - t);
86 z = -y / (y + two);
87 } else {
88 /* raise the INEXACT flag for non-zero t */
89 dummy = t + big;
90 #ifdef lint
91 dummy = dummy;
92 #endif
93 return (x);
94 }
95 } else if (!finite(t))
96 return (copysign(1.0, x));
97 else
98 return ((signx != 0) ? -z + small * small : z - small * small);
99
100 return ((signx != 0) ? -z : z);
101 }
|