aboutsummaryrefslogtreecommitdiff
path: root/newlib/libm/mathfp/s_sineh.c
blob: 6b3480d73a2b78959c502d5aecec298af58211ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

/* @(#)z_sineh.c 1.0 98/08/13 */
/******************************************************************
 * The following routines are coded directly from the algorithms
 * and coefficients given in "Software Manual for the Elementary
 * Functions" by William J. Cody, Jr. and William Waite, Prentice
 * Hall, 1980.
 ******************************************************************/

/*
FUNCTION
        <<sinh>>, <<sinhf>>, <<cosh>>, <<coshf>>, <<sineh>>---hyperbolic sine or cosine

INDEX
        sinh
INDEX
        sinhf
INDEX
        cosh
INDEX
        coshf

ANSI_SYNOPSIS
        #include <math.h>
        double sinh(double <[x]>);
        float  sinhf(float <[x]>);
        double cosh(double <[x]>);
        float  coshf(float <[x]>);
TRAD_SYNOPSIS
        #include <math.h>
        double sinh(<[x]>)
        double <[x]>;

        float  sinhf(<[x]>)
        float <[x]>;

        double cosh(<[x]>)
        double <[x]>;

        float  coshf(<[x]>)
        float <[x]>;

DESCRIPTION
        <<sinh>> and <<cosh>> compute the hyperbolic sine or cosine
        of the argument <[x]>.
        Angles are specified in radians.   <<sinh>>(<[x]>) is defined as
        @ifinfo
        . (exp(<[x]>) - exp(-<[x]>))/2
        @end ifinfo
        @tex
        $${e^x - e^{-x}}\over 2$$
        @end tex
        <<cosh>> is defined as
        @ifinfo
        . (exp(<[x]>) - exp(-<[x]>))/2
        @end ifinfo
        @tex
        $${e^x + e^{-x}}\over 2$$
        @end tex

        <<sinhf>> and <<coshf>> are identical, save that they take 
        and returns <<float>> values.

RETURNS
        The hyperbolic sine or cosine of <[x]> is returned.

        When the correct result is too large to be representable (an
        overflow),  the functions return <<HUGE_VAL>> with the
        appropriate sign, and sets the global value <<errno>> to
        <<ERANGE>>.

PORTABILITY
        <<sinh>> is ANSI C.
        <<sinhf>> is an extension.
        <<cosh>> is ANSI C.
        <<coshf>> is an extension.

*/

/******************************************************************
 * Hyperbolic Sine 
 *
 * Input:
 *   x - floating point value
 *
 * Output:
 *   hyperbolic sine of x
 *
 * Description:
 *   This routine calculates hyperbolic sines.
 *
 *****************************************************************/

#include <float.h>
#include "fdlibm.h"
#include "zmath.h"

static const double q[] = { -0.21108770058106271242e+7,
                             0.36162723109421836460e+5,
                            -0.27773523119650701667e+3 };
static const double p[] = { -0.35181283430177117881e+6,
                            -0.11563521196851768270e+5,
                            -0.16375798202630751372e+3,
                            -0.78966127417357099479 };
static const double LNV = 0.6931610107421875000;
static const double INV_V2 = 0.24999308500451499336;
static const double V_OVER2_MINUS1 = 0.13830277879601902638e-4;

double
_DEFUN (sineh, (double, int),
        double x _AND
        int cosineh)
{
  double y, f, P, Q, R, res, z, w;
  int sgn = 1;
  double WBAR = 18.55;

  /* Check for special values. */
  switch (numtest (x))
    {
      case NAN:
        errno = EDOM;
        return (x);
      case INF:
        errno = ERANGE;
        return (ispos (x) ? z_infinity.d : -z_infinity.d);
    }

  y = fabs (x);

  if (!cosineh && x < 0.0)
    sgn = -1;

  if ((y > 1.0 && !cosineh) || cosineh)
    {
      if (y > BIGX)
        {
          w = y - LNV;
          
          /* Check for w > maximum here. */
          if (w > BIGX)
            {
              errno = ERANGE;
              return (x);
            }

          z = exp (w);

          if (w > WBAR)
            res = z * (V_OVER2_MINUS1 + 1.0);
        }

      else
        {
          z = exp (y);
          if (cosineh)
            res = (z + 1 / z) / 2.0;
          else
            res = (z - 1 / z) / 2.0;
        }

      if (sgn < 0)
        res = -res;
    }
  else
    {
      /* Check for y being too small. */
      if (y < z_rooteps)
        {
          res = x;
        }
      /* Calculate the Taylor series. */
      else
        { 
          f = x * x;
          Q = ((f + q[2]) * f + q[1]) * f + q[0];
          P = ((p[3] * f + p[2]) * f + p[1]) * f + p[0];
          R = f * (P / Q); 

          res = x + x * R;
        }
    }

  return (res);
}