forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpf_nthroot.c
271 lines (261 loc) · 6.63 KB
/
mpf_nthroot.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
#include <tomfloat.h>
int mpf_nthroot(mp_float * a, long n, mp_float * b)
{
int err, sign;
mp_float one, t2, t3, invb, x0;
mp_float ret;
mp_float frac;
long oldeps, nm1, loops, expnt;
double d,rest;
// TODO: IEEE-754 has a quite complicated system here, adapt
if (mpf_iszero(a)) {
if (n > 0) {
if ((err = mpf_const_0(b)) != MP_OKAY) {
return err;
}
return MP_OKAY;
} else {
if ((err = mpf_const_nan(b)) != MP_OKAY) {
return err;
}
return MP_OKAY;
}
}
if (mpf_isnan(a)) {
if ((err = mpf_const_nan(b)) != MP_OKAY) {
return err;
}
return MP_OKAY;
}
if (mpf_isinf(a)) {
if ((err = mpf_const_inf(b, MP_ZPOS)) != MP_OKAY) {
return err;
}
return MP_OKAY;
}
if (n < 0) {
// 1/(x^(1/-n))
n = -n;
if ((err = mpf_init_copy(&ret, a)) != MP_OKAY) {
return err;
}
if ((err = mpf_inv(&ret, &ret)) != MP_OKAY) {
mpf_clear(&ret);
return err;
}
if ((err = mpf_nthroot(&ret, n, b)) != MP_OKAY) {
mpf_clear(&ret);
return err;
}
mpf_clear(&ret);
return MP_OKAY;
}
if (n == 1) {
// a^(1/1)
if ((err = mpf_copy(a, b)) != MP_OKAY) {
return err;
}
return MP_OKAY;
}
/* input must be positive if b is even */
if ((n & 1) == 0 && a->mantissa.sign == MP_NEG) {
return MP_VAL;
}
if (n == 2) {
return mpf_sqrt(a, b);
}
if ((err = mpf_init(&one, a->radix)) != MP_OKAY) {
return err;
}
if ((err = mpf_const_d(&one, 1)) != MP_OKAY) {
mpf_clear(&one);
return err;
}
if (mpf_cmp(a, &one) == MP_EQ) {
if (n == 0) {
if ((err = mpf_const_nan(b)) != MP_OKAY) {
mpf_clear(&one);
return err;
}
mpf_clear(&one);
return MP_VAL;
} else {
if ((err = mpf_const_0(b)) != MP_OKAY) {
mpf_clear(&one);
return err;
}
mpf_clear(&one);
return MP_OKAY;
}
}
mpf_clear(&one);
oldeps = a->radix;
if ((err = mpf_init_copy(a, &ret)) != MP_OKAY) {
return err;
}
if ((err = mpf_normalize_to(&ret, oldeps + MP_DIGIT_BIT)) != MP_OKAY) {
mpf_clear(&ret);
return err;
}
sign = a->mantissa.sign;
ret.mantissa.sign = MP_ZPOS;
/*
* Compute initial value (assuming n is positive)
*
* x^n = exp(log(x) * n)
* x^n = 2^(log_2(x) * n)
*
* (f * 2^e)^n = 2^(log_2(f * 2^e) * n)
* = 2^( (log_2(f) + e) * n)
* = 2^( log_2(f) * n + e * n )
*
* and with n the fraction 1/n
*
* (f * 2^e)^(1/n) = 2^( log_2(f) / n + e / n )
*
* handling both factors individually:
*
* (f * 2^e)^(1/n) = f^(1/n) * (2^e)^(1/n)
*
* f^(1/n) = 2^(log_2(f)/n)
*
* (2^e)^(1/n) = 2^( log_2(2^e)/n )
* = 2^(e / n)
* We need an integer for the exponent so we use the integer
* part for the big exponent and add the small fractional part f
* the big fractional part F by doing F*2^f
*/
if (mpf_isdouble(a)) {
if ((err = mpf_init(&t2, ret.radix)) != MP_OKAY) {
mpf_clear(&ret);
return err;
}
if ((err = mpf_init(&frac, ret.radix)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,NULL);
return err;
}
if ((err = mpf_set_double(&ret, &d)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
// TODO: this can underflow for very large n and very small d.
// Unlikely, but not impossible even for n = 2^32 and d = 1e-300
// More likely if the long type is larger than 32 bit
d = pow(d, 1.0 / n);
if ((err = mpf_get_double(d, &t2)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
} else {
if ((err = mpf_init(&t2, ret.radix)) != MP_OKAY) {
mpf_clear(&ret);
return err;
}
if ((err = mpf_init(&frac, ret.radix)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,NULL);
return err;
}
if ((err = mpf_frexp(&ret, &frac, &expnt)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
if ((err = mpf_set_double(&frac, &d)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
// (f) ^(1/n)
d = pow(d, 1.0 / n);
// integer part floor(e/n)
expnt = expnt / n;
// add the fractional part {e/n}
rest = ((double)(expnt)) / ((double)(n)) - expnt;
rest = pow(2,rest);
d *= rest;
if ((err = mpf_get_double(d, &frac)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
// f^(1/n) * 2^( log_2(2^e)/n )
// f^(1/n) * 2^( e/n )
if ((err = mpf_ldexp(&frac, expnt, &t2)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
if (d >= 1){
if( (err = mpf_normalize(&t2) ) != MP_OKAY){
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
}
}
// we should have a good enough initial value in t2 now.
if ((err = mpf_init(&invb, ret.radix)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,NULL);
return err;
}
if ((err = mpf_const_d(&invb, n)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,&invb,NULL);
return err;
}
if ((err = mpf_inv(&invb, &invb)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,&invb,NULL);
return err;
}
nm1 = n - 1;
// safe guard for the worst case
loops = ret.radix;
if ((err = mpf_init_multi(ret.radix, &t3, &x0, NULL)) != MP_OKAY) {
mpf_clear_multi(&ret,&t2,&frac,&invb,NULL);
return err;
}
do {
// x0 = t2
if ((err = mpf_copy(&t2, &x0)) != MP_OKAY) {
goto _ERR;
}
// t3 = t2^nm1
if ((err = mpf_pow_d(&t2, nm1, &t3)) != MP_OKAY) {
goto _ERR;
}
// t3 = a / t3
if ((err = mpf_div(&ret, &t3, &t3)) != MP_OKAY) {
goto _ERR;
}
// t3 = t3 - t2
if ((err = mpf_sub(&t3, &t2, &t3)) != MP_OKAY) {
goto _ERR;
}
// t3 = t3 * invb
if ((err = mpf_mul(&t3, &invb, &t3)) != MP_OKAY) {
goto _ERR;
}
// t2 = t2 + t3
if ((err = mpf_add(&t2, &t3, &t2)) != MP_OKAY) {
goto _ERR;
}
if (mpf_iszero(&t2)) {
break;
}
if (loops-- == 0) {
fprintf(stderr, "mp_float nthroot did not converge in some %ld rounds\n",
(ret.radix));
err = MP_VAL;
goto _ERR;
}
} while (mpf_cmp(&t2, &x0) != MP_EQ);
#ifdef DEBUG
fprintf(stderr, "nth-root loops = %ld\n", (ret.radix + 1) - loops);
#endif
/* set the sign of the result */
t2.mantissa.sign = sign;
if ((err = mpf_normalize_to(&t2, a->radix)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(&t2, b)) != MP_OKAY) {
goto _ERR;
}
err = MP_OKAY;
_ERR:
mpf_clear_multi(&t2, &t3, &x0, &invb, &ret, &frac,NULL);
return err;
}