00001
00002
00003
00004
00005
00006
00007
00008
00009 #include <float.h>
00010 #include <math.h>
00011 #include <errno.h>
00012 #include "localmath.h"
00013
00014 double
00015 tanh(double x)
00016 {
00017
00018
00019
00020
00021
00022 static double p[] = {
00023 -0.16134119023996228053e+4,
00024 -0.99225929672236083313e+2,
00025 -0.96437492777225469787e+0
00026 };
00027 static double q[] = {
00028 0.48402357071988688686e+4,
00029 0.22337720718962312926e+4,
00030 0.11274474380534949335e+3,
00031 1.0
00032 };
00033 int negative = x < 0;
00034
00035 if (__IsNan(x)) {
00036 errno = EDOM;
00037 return x;
00038 }
00039 if (negative) x = -x;
00040
00041 if (x >= 0.5*M_LN_MAX_D) {
00042 x = 1.0;
00043 }
00044 #define LN3D2 0.54930614433405484570e+0
00045 else if (x > LN3D2) {
00046 x = 0.5 - 1.0/(exp(x+x)+1.0);
00047 x += x;
00048 }
00049 else {
00050
00051 double g = x*x;
00052 x += x * g * POLYNOM2(g, p)/POLYNOM3(g, q);
00053 }
00054 return negative ? -x : x;
00055 }