File size: 5,634 Bytes
7885a28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Translated into C++ by SciPy developers in 2024.
 * Original header with Copyright information appears below.
 */

/*                                                     sindg.c
 *
 *     Circular sine of angle in degrees
 *
 *
 *
 * SYNOPSIS:
 *
 * double x, y, sindg();
 *
 * y = sindg( x );
 *
 *
 *
 * DESCRIPTION:
 *
 * Range reduction is into intervals of 45 degrees.
 *
 * Two polynomial approximating functions are employed.
 * Between 0 and pi/4 the sine is approximated by
 *      x  +  x**3 P(x**2).
 * Between pi/4 and pi/2 the cosine is represented as
 *      1  -  x**2 P(x**2).
 *
 *
 *
 * ACCURACY:
 *
 *                      Relative error:
 * arithmetic   domain      # trials      peak         rms
 *    IEEE      +-1000       30000      2.3e-16      5.6e-17
 *
 * ERROR MESSAGES:
 *
 *   message           condition        value returned
 * sindg total loss   x > 1.0e14 (IEEE)     0.0
 *
 */
/*							cosdg.c
 *
 *	Circular cosine of angle in degrees
 *
 *
 *
 * SYNOPSIS:
 *
 * double x, y, cosdg();
 *
 * y = cosdg( x );
 *
 *
 *
 * DESCRIPTION:
 *
 * Range reduction is into intervals of 45 degrees.
 *
 * Two polynomial approximating functions are employed.
 * Between 0 and pi/4 the cosine is approximated by
 *      1  -  x**2 P(x**2).
 * Between pi/4 and pi/2 the sine is represented as
 *      x  +  x**3 P(x**2).
 *
 *
 * ACCURACY:
 *
 *                      Relative error:
 * arithmetic   domain      # trials      peak         rms
 *    IEEE     +-1000        30000       2.1e-16     5.7e-17
 *  See also sin().
 *
 */

/* Cephes Math Library Release 2.0:  April, 1987
 * Copyright 1985, 1987 by Stephen L. Moshier
 * Direct inquiries to 30 Frost Street, Cambridge, MA 02140 */
#pragma once

#include "../config.h"
#include "../error.h"

#include "const.h"
#include "polevl.h"

namespace xsf {
namespace cephes {

    namespace detail {

        constexpr double sincof[] = {1.58962301572218447952E-10, -2.50507477628503540135E-8,
                                     2.75573136213856773549E-6,  -1.98412698295895384658E-4,
                                     8.33333333332211858862E-3,  -1.66666666666666307295E-1};

        constexpr double coscof[] = {1.13678171382044553091E-11, -2.08758833757683644217E-9, 2.75573155429816611547E-7,
                                     -2.48015872936186303776E-5, 1.38888888888806666760E-3,  -4.16666666666666348141E-2,
                                     4.99999999999999999798E-1};

        constexpr double sindg_lossth = 1.0e14;

    } // namespace detail

    XSF_HOST_DEVICE inline double sindg(double x) {
        double y, z, zz;
        int j, sign;

        /* make argument positive but save the sign */
        sign = 1;
        if (x < 0) {
            x = -x;
            sign = -1;
        }

        if (x > detail::sindg_lossth) {
            set_error("sindg", SF_ERROR_NO_RESULT, NULL);
            return (0.0);
        }

        y = std::floor(x / 45.0); /* integer part of x/M_PI_4 */

        /* strip high bits of integer part to prevent integer overflow */
        z = std::ldexp(y, -4);
        z = std::floor(z);        /* integer part of y/8 */
        z = y - std::ldexp(z, 4); /* y - 16 * (y/16) */

        j = z; /* convert to integer for tests on the phase angle */
        /* map zeros to origin */
        if (j & 1) {
            j += 1;
            y += 1.0;
        }
        j = j & 07; /* octant modulo 360 degrees */
        /* reflect in x axis */
        if (j > 3) {
            sign = -sign;
            j -= 4;
        }

        z = x - y * 45.0;   /* x mod 45 degrees */
        z *= detail::PI180; /* multiply by pi/180 to convert to radians */
        zz = z * z;

        if ((j == 1) || (j == 2)) {
            y = 1.0 - zz * polevl(zz, detail::coscof, 6);
        } else {
            y = z + z * (zz * polevl(zz, detail::sincof, 5));
        }

        if (sign < 0)
            y = -y;

        return (y);
    }

    XSF_HOST_DEVICE inline double cosdg(double x) {
        double y, z, zz;
        int j, sign;

        /* make argument positive */
        sign = 1;
        if (x < 0)
            x = -x;

        if (x > detail::sindg_lossth) {
            set_error("cosdg", SF_ERROR_NO_RESULT, NULL);
            return (0.0);
        }

        y = std::floor(x / 45.0);
        z = std::ldexp(y, -4);
        z = std::floor(z);        /* integer part of y/8 */
        z = y - std::ldexp(z, 4); /* y - 16 * (y/16) */

        /* integer and fractional part modulo one octant */
        j = z;
        if (j & 1) { /* map zeros to origin */
            j += 1;
            y += 1.0;
        }
        j = j & 07;
        if (j > 3) {
            j -= 4;
            sign = -sign;
        }

        if (j > 1)
            sign = -sign;

        z = x - y * 45.0;   /* x mod 45 degrees */
        z *= detail::PI180; /* multiply by pi/180 to convert to radians */

        zz = z * z;

        if ((j == 1) || (j == 2)) {
            y = z + z * (zz * polevl(zz, detail::sincof, 5));
        } else {
            y = 1.0 - zz * polevl(zz, detail::coscof, 6);
        }

        if (sign < 0)
            y = -y;

        return (y);
    }

    /* Degrees, minutes, seconds to radians: */

    /* 1 arc second, in radians = 4.848136811095359935899141023579479759563533023727e-6 */

    namespace detail {
        constexpr double sindg_P64800 = 4.848136811095359935899141023579479759563533023727e-6;
    }

    XSF_HOST_DEVICE inline double radian(double d, double m, double s) {
        return (((d * 60.0 + m) * 60.0 + s) * detail::sindg_P64800);
    }

} // namespace cephes
} // namespace xsf