-
Notifications
You must be signed in to change notification settings - Fork 0
/
atan_accurate.c
341 lines (272 loc) · 6.97 KB
/
atan_accurate.c
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Correctly rounded arctangent
*
* Author : Nicolas Gast (Ecole Normale Superieure), Florent de Dinechin
*
* This file is part of the crlibm library developed by the Arenaire
* project at Ecole Normale Superieure de Lyon
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include "crlibm_private.h"
#include "atan_accurate.h"
#include "atan_fast.h"
/*
* WHAT WE CAN DO :
*
* 1) Range reduction
*
* x > 0 because atan(-x) = - atan(x)
*
* we have built 50 intervals I(i), associated to a b(i) so that :
*
* For every x :
*
* we find the interval I(i) , as atan(x) = atan(b(i)) + atan( (x - b(i)) / (1 + x * b(i)) )
*
* so that X = (x - b(i)) / (1 + x * b(i)) be in interval [ -2^(-6) , 2^(-6) ]
* There is no cancellation because :
* for every x in [ -2^(-6) , 2^(-6) ],
*
* atan(x) <= 0.01562372862 in binary 0.000001111111111
* AND for the smallest b(i) atan(b(i)) = 0.04687118592 in binary 0.00001011111111
*
*
* 2) Polynomial evaluation of atan(X), atan(b(i)) is tabulated.
*
* (-???)
* Approximation error: |err| < 2^
*
*
* 3) Reconstruction:
*
* atan(x) = atan(b(i)) + atan(X)
*
*
* 4) Rounding:
*
* when |x| is too big, the result is always sign(x) * Pi/2,
* because Pi/2 is appromated by the biggest value smallest than Pi/2,
* in order not to have an atan > Pi/2.
*/
static void scs_atan(scs_ptr res_scs, scs_ptr x){
scs_t X_scs, denom1_scs, denom2_scs, poly_scs, X2;
scs_t atanbhihi,atanbhilo, atanblo, atanbhi, atanb;
scs_t bsc_ptr;
db_number db;
double test;
int k, i=31;
scs_get_d(&db.d, x);
#if EVAL_PERF
crlibm_second_step_taken++;
#endif
/* test if x as to be reduced */
if (db.d > MIN_REDUCTION_NEEDED) {
/* Compute i so that x E [a[i],a[i+1]] */
if (db.d < arctan_table[i][A].d) i-= 16;
else i+=16;
if (db.d < arctan_table[i][A].d) i-= 8;
else i+= 8;
if (db.d < arctan_table[i][A].d) i-= 4;
else i+= 4;
if (db.d < arctan_table[i][A].d) i-= 2;
else i+= 2;
if (db.d < arctan_table[i][A].d) i-= 1;
else if (i<61) i+= 1;
if (db.d < arctan_table[i][A].d) i-= 1;
/* evaluate X = (x - b(i)) / (1 + x*b(i)) */
scs_set_d(bsc_ptr, arctan_table[i][B].d);
scs_mul(denom1_scs,bsc_ptr,x);
scs_add(denom2_scs,denom1_scs,SCS_ONE);
scs_sub(X_scs,x,bsc_ptr);
scs_div(X_scs,X_scs,denom2_scs);
scs_get_d(&test,X_scs);
/* Polynomial evaluation of atan(X) , X = (x-b(i)) / (1+ x*b(i)) */
scs_square(X2, X_scs);
scs_set(res_scs, constant_poly_ptr[0]);
for(k=1; k < 10; k++) {
/* we use Horner expression */
scs_mul(res_scs, res_scs, X2);
scs_add(res_scs, constant_poly_ptr[k], res_scs);
}
scs_mul(poly_scs, res_scs, X_scs);
/* reconstruction : */
/* 1st we load atan ( b[i] ) in a scs*/
scs_set_d( atanbhihi , arctan_table[i][ATAN_BHI].d);
scs_set_d( atanbhilo , arctan_table[i][ATAN_BLO].d);
scs_set_d( atanblo , atan_blolo[i].d);
scs_add(atanbhi,atanbhihi,atanbhilo);
scs_add(atanb,atanbhi,atanblo);
scs_add(res_scs,atanb, poly_scs);
return;
}
else
{ /* no reduction needed */
/* Polynomial evaluation of atan(x) */
scs_square(X2, x);
scs_set(res_scs, constant_poly_ptr[0]);
for(k=1; k < 10; k++) {
/* we use Horner expression */
scs_mul(res_scs, res_scs, X2);
scs_add(res_scs, constant_poly_ptr[k], res_scs);
}
scs_mul(res_scs, res_scs, x);
return;
}
}
static void scs_atanpi(scs_ptr res, scs_ptr x){
scs_t at;
scs_atan(at, x);
scs_mul(res, at, InvPiSCS_ptr);
}
double scs_atan_rn(double x){
/* This function does NOT compute atan(x) correctly if it isn't
* called in atan_rn()
*/
scs_t sc1;
scs_t res_scs;
db_number res;
int sign =1;
res.d = x;
if (x < 0){
sign = -1;
x *= -1;
}
scs_set_d(sc1, x);
scs_atan(res_scs, sc1);
scs_get_d(&res.d, res_scs);
res.d *= sign;
return res.d;
}
double scs_atan_rd(double x){
scs_t sc1;
scs_t res_scs;
db_number res;
int sign = 1;
res.d = x;
/* Filter cases */
if (x < 0){
sign = -1;
x *= -1;
}
scs_set_d(sc1, x);
scs_atan(res_scs, sc1);
if (sign == -1){
scs_get_d_pinf(&res.d, res_scs);
res.d *= -1;
return res.d;
}
else{
scs_get_d_minf(&res.d, res_scs);
return res.d;
}
}
double scs_atan_ru(double x){
scs_t sc1;
scs_t res_scs;
db_number res;
int sign = 1;
res.d = x;
/* Filter cases */
if (x < 0){
sign = -1;
x *= -1;
}
scs_set_d(sc1, x);
scs_atan(res_scs, sc1);
if (sign == -1){
scs_get_d_minf(&res.d, res_scs);
res.d *= -1;
return res.d;
}
else{
scs_get_d_pinf(&res.d, res_scs);
return res.d;
}
}
/************************************************************/
/******** AtanPi *******************************************/
double scs_atanpi_rn(double x){
/* This function does NOT compute atanpi(x) correctly if it isn't
* called in atanpi_rn()
*/
scs_t sc1;
scs_t res_scs;
db_number res;
int sign =1;
res.d = x;
if (x < 0){
sign = -1;
x *= -1;
}
scs_set_d(sc1, x);
scs_atanpi(res_scs, sc1);
scs_get_d(&res.d, res_scs);
res.d *= sign;
return res.d;
}
double scs_atanpi_rd(double x){
scs_t sc1;
scs_t res_scs;
db_number res;
int sign = 1;
res.d = x;
/* Filter cases */
if (x < 0){
sign = -1;
x *= -1;
}
scs_set_d(sc1, x);
scs_atanpi(res_scs, sc1);
if (sign == -1){
scs_get_d_pinf(&res.d, res_scs);
res.d *= -1;
return res.d;
}
else{
scs_get_d_minf(&res.d, res_scs);
return res.d;
}
}
/*************************************************************
*************************************************************
* ROUNDED TOWARD +INFINITY
*************************************************************
*************************************************************/
double scs_atanpi_ru(double x){
scs_t sc1;
scs_t res_scs;
db_number res;
int sign = 1;
res.d = x;
/* Filter cases */
if (x < 0){
sign = -1;
x *= -1;
}
scs_set_d(sc1, x);
scs_atanpi(res_scs, sc1);
if (sign == -1){
scs_get_d_minf(&res.d, res_scs);
res.d *= -1;
return res.d;
}
else{
scs_get_d_pinf(&res.d, res_scs);
return res.d;
}
}