Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libm / mathfp / s_tanh.c
blobed036645bd97b7e84afd412d51833ee7aaae89d2
2 /* @(#)z_tanh.c 1.0 98/08/13 */
3 /*****************************************************************
4 * The following routines are coded directly from the algorithms
5 * and coefficients given in "Software Manual for the Elementary
6 * Functions" by William J. Cody, Jr. and William Waite, Prentice
7 * Hall, 1980.
8 *****************************************************************/
12 FUNCTION
13 <<tanh>>, <<tanhf>>---hyperbolic tangent
15 INDEX
16 tanh
17 INDEX
18 tanhf
20 SYNOPSIS
21 #include <math.h>
22 double tanh(double <[x]>);
23 float tanhf(float <[x]>);
25 DESCRIPTION
27 <<tanh>> computes the hyperbolic tangent of
28 the argument <[x]>. Angles are specified in radians.
30 <<tanh(<[x]>)>> is defined as
31 . sinh(<[x]>)/cosh(<[x]>)
33 <<tanhf>> is identical, save that it takes and returns <<float>> values.
35 RETURNS
36 The hyperbolic tangent of <[x]> is returned.
38 PORTABILITY
39 <<tanh>> is ANSI C. <<tanhf>> is an extension.
43 /******************************************************************
44 * Hyperbolic Tangent
46 * Input:
47 * x - floating point value
49 * Output:
50 * hyperbolic tangent of x
52 * Description:
53 * This routine calculates hyperbolic tangent.
55 *****************************************************************/
57 #include <float.h>
58 #include "fdlibm.h"
59 #include "zmath.h"
61 #ifndef _DOUBLE_IS_32BITS
63 static const double LN3_OVER2 = 0.54930614433405484570;
64 static const double p[] = { -0.16134119023996228053e+4,
65 -0.99225929672236083313e+2,
66 -0.96437492777225469787 };
67 static const double q[] = { 0.48402357071988688686e+4,
68 0.22337720718962312926e+4,
69 0.11274474380534949335e+3 };
71 double
72 tanh (double x)
74 double f, res, g, P, Q, R;
76 f = fabs (x);
78 /* Check if the input is too big. */
79 if (f > BIGX)
80 res = 1.0;
82 else if (f > LN3_OVER2)
83 res = 1.0 - 2.0 / (exp (2 * f) + 1.0);
85 /* Check if the input is too small. */
86 else if (f < z_rooteps)
87 res = f;
89 /* Calculate the Taylor series. */
90 else
92 g = f * f;
94 P = (p[2] * g + p[1]) * g + p[0];
95 Q = ((g + q[2]) * g + q[1]) * g + q[0];
96 R = g * (P / Q);
98 res = f + f * R;
101 if (x < 0.0)
102 res = -res;
104 return (res);
107 #endif /* _DOUBLE_IS_32BITS */