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
/*
Copyright (C) 2012-2014 Fredrik Johansson
This file is part of FLINT.
FLINT is free software: you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License (LGPL) as published
by the Free Software Foundation; either version 3 of the License, or
(at your option) any later version. See <https://www.gnu.org/licenses/>.
*/
#include "arb.h"
#include "mpn_extras.h"
#define TMP_ALLOC_LIMBS(size) TMP_ALLOC((size) * sizeof(ulong))
/* atan(x) = x + eps, |eps| < x^3*/
static void
arb_atan_eps(arb_t z, const arf_t x, slong prec)
{
fmpz_t mag;
fmpz_init(mag);
fmpz_mul_ui(mag, ARF_EXPREF(x), 3);
arb_set_arf(z, x);
arb_set_round(z, z, prec);
arb_add_error_2exp_fmpz(z, mag);
fmpz_clear(mag);
}
/* atan(x) = pi/2 - eps, eps < 1/x <= 2^(1-mag) */
static void
arb_atan_inf_eps(arb_t z, const arf_t x, slong prec)
{
fmpz_t mag;
fmpz_init(mag);
fmpz_neg(mag, ARF_EXPREF(x));
fmpz_add_ui(mag, mag, 1);
if (arf_sgn(x) > 0)
{
arb_const_pi(z, prec);
}
else
{
arb_const_pi(z, prec);
arb_neg(z, z);
}
arb_mul_2exp_si(z, z, -1);
arb_add_error_2exp_fmpz(z, mag);
fmpz_clear(mag);
}
void
arb_atan_arf(arb_t z, const arf_t x, slong prec)
{
if (arf_is_special(x))
{
if (arf_is_zero(x))
{
arb_zero(z);
}
else if (arf_is_pos_inf(x))
{
arb_const_pi(z, prec);
arb_mul_2exp_si(z, z, -1);
}
else if (arf_is_neg_inf(x))
{
arb_const_pi(z, prec);
arb_mul_2exp_si(z, z, -1);
arb_neg(z, z);
}
else
{
arb_indeterminate(z);
}
}
else if (COEFF_IS_MPZ(*ARF_EXPREF(x)))
{
if (fmpz_sgn(ARF_EXPREF(x)) < 0)
arb_atan_eps(z, x, prec);
else
arb_atan_inf_eps(z, x, prec);
}
else
{
slong exp, wp, wn, N, r;
nn_srcptr xp;
slong xn, tn;
nn_ptr tmp, w, t, u;
ulong p1, q1bits, p2, q2bits, error, error2;
int negative, inexact, reciprocal;
TMP_INIT;
exp = ARF_EXP(x);
negative = ARF_SGNBIT(x);
if (exp < -(prec/2) - 2 || exp > prec + 2)
{
if (exp < 0)
arb_atan_eps(z, x, prec);
else
arb_atan_inf_eps(z, x, prec);
return;
}
ARF_GET_MPN_READONLY(xp, xn, x);
/* Special case: +/- 1 (we require |x| != 1 later on) */
if (exp == 1 && xn == 1 && xp[xn-1] == LIMB_TOP)
{
arb_const_pi(z, prec);
arb_mul_2exp_si(z, z, -2);
if (negative)
arb_neg(z, z);
return;
}
if (prec >= ARB_ATAN_NEWTON_PREC)
{
arb_atan_arf_newton(z, x, prec);
return;
}
/* Absolute working precision (NOT rounded to a limb multiple) */
wp = prec - FLINT_MIN(0, exp) + 4;
/* Too high precision to use table */
if (wp > ARB_ATAN_TAB2_PREC)
{
arb_atan_arf_bb(z, x, prec);
return;
}
/* Working precision in limbs */
wn = (wp + FLINT_BITS - 1) / FLINT_BITS;
TMP_START;
tmp = TMP_ALLOC_LIMBS(4 * wn + 3);
w = tmp; /* requires wn+1 limbs */
t = w + wn + 1; /* requires wn+1 limbs */
u = t + wn + 1; /* requires 2wn+1 limbs */
/* ----------------------------------------------------------------- */
/* Convert x or 1/x to a fixed-point number |w| < 1 */
/* ----------------------------------------------------------------- */
if (exp <= 0) /* |x| < 1 */
{
reciprocal = 0;
/* todo: just zero top */
flint_mpn_zero(w, wn);
/* w = x as a fixed-point number */
error = _arf_get_integer_mpn(w, xp, xn, exp + wn * FLINT_BITS);
}
else /* |x| > 1 */
{
slong one_exp, one_limbs, one_bits;
nn_ptr one;
reciprocal = 1;
one_exp = xn * FLINT_BITS + wn * FLINT_BITS - exp;
flint_mpn_zero(w, wn);
/* 1/x becomes zero */
if (one_exp >= FLINT_BITS - 1)
{
/* w = 1/x */
one_limbs = one_exp / FLINT_BITS;
one_bits = one_exp % FLINT_BITS;
if (one_limbs + 1 >= xn)
{
one = TMP_ALLOC_LIMBS(one_limbs + 1);
flint_mpn_zero(one, one_limbs);
one[one_limbs] = UWORD(1) << one_bits;
/* todo: only zero necessary part */
flint_mpn_zero(w, wn);
mpn_tdiv_q(w, one, one_limbs + 1, xp, xn);
/* Now w must be < 1 since x > 1 and we rounded down; thus
w[wn] must be zero */
}
}
/* todo: moderate powers of two would be exact... */
error = 1;
}
/* ----------------------------------------------------------------- */
/* Table-based argument reduction */
/* ----------------------------------------------------------------- */
/* choose p such that p/q <= x < (p+1)/q */
if (wp <= ARB_ATAN_TAB1_PREC)
q1bits = ARB_ATAN_TAB1_BITS;
else
q1bits = ARB_ATAN_TAB21_BITS;
p1 = w[wn-1] >> (FLINT_BITS - q1bits);
/* atan(w) = atan(p/q) + atan(w2) */
/* where w2 = (q*w-p)/(q+p*w) */
if (p1 != 0)
{
t[wn] = (UWORD(1) << q1bits) + mpn_mul_1(t, w, wn, p1);
flint_mpn_zero(u, wn);
u[2 * wn] = mpn_lshift(u + wn, w, wn, q1bits) - p1;
mpn_tdiv_q(w, u, 2 * wn + 1, t, wn + 1);
error++; /* w2 is computed with 1 ulp error */
}
/* Do a second round of argument reduction */
if (wp <= ARB_ATAN_TAB1_PREC)
{
p2 = 0;
}
else
{
q2bits = ARB_ATAN_TAB21_BITS + ARB_ATAN_TAB22_BITS;
p2 = w[wn-1] >> (FLINT_BITS - q2bits);
if (p2 != 0)
{
t[wn] = (UWORD(1) << q2bits) + mpn_mul_1(t, w, wn, p2);
flint_mpn_zero(u, wn);
u[2 * wn] = mpn_lshift(u + wn, w, wn, q2bits) - p2;
mpn_tdiv_q(w, u, 2 * wn + 1, t, wn + 1);
error++;
}
}
/* |w| <= 2^-r */
r = _arb_mpn_leading_zeros(w, wn);
/* N >= (wp-r)/(2r) */
N = (wp - r + (2*r-1)) / (2*r);
/* Evaluate Taylor series */
_arb_atan_taylor_rs(t, &error2, w, wn, N, 1);
/* Taylor series evaluation error */
error += error2;
/* Size of output number */
tn = wn;
/* First table lookup */
if (p1 != 0)
{
if (wp <= ARB_ATAN_TAB1_PREC)
mpn_add_n(t, t, arb_atan_tab1[p1] + ARB_ATAN_TAB1_LIMBS - tn, tn);
else
mpn_add_n(t, t, arb_atan_tab21[p1] + ARB_ATAN_TAB2_LIMBS - tn, tn);
error++;
}
/* Second table lookup */
if (p2 != 0)
{
mpn_add_n(t, t, arb_atan_tab22[p2] + ARB_ATAN_TAB2_LIMBS - tn, tn);
error++;
}
/* pi/2 - atan(1/x) */
if (reciprocal)
{
t[tn] = LIMB_ONE - mpn_sub_n(t,
arb_atan_pi2_minus_one + ARB_ATAN_TAB2_LIMBS - tn, t, tn);
/* result can be >= 1 */
tn += (t[tn] != 0);
/* error of pi/2 */
error++;
}
/* The accumulated arithmetic error */
mag_set_ui_2exp_si(arb_radref(z), error, -wn * FLINT_BITS);
/* Truncation error from the Taylor series */
mag_add_ui_2exp_si(arb_radref(z), arb_radref(z), 1, -r*(2*N+1));
/* Set the midpoint */
inexact = _arf_set_mpn_fixed(arb_midref(z), t, tn, wn, negative, prec, ARB_RND);
if (inexact)
arf_mag_add_ulp(arb_radref(z), arb_radref(z), arb_midref(z), prec);
TMP_END;
}
}