forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpf_trig_arg_reduct.c
152 lines (136 loc) · 3.63 KB
/
mpf_trig_arg_reduct.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
#include <tomfloat.h>
/* Argument reduction for sine, cosine and tangent to x <= pi/4 */
int mpf_trig_arg_reduct(mp_float * a, mp_float * b, int *k)
{
int err, sign;
mp_float pi, pihalf, piquart, r, x, three, one, K;
long size, oldeps, eps, oldprec, newprec;
if (mpf_iszero(a)) {
*k = 0;
return mpf_copy(a, b);
}
oldeps = a->radix;
eps = oldeps + 10;
err = MP_OKAY;
if ((err =
mpf_init_multi(eps, &pi, &pihalf, &piquart, &r, &x, &three, &one, &K,
NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_abs(a, &x)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&x, eps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_d(&three, 3)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_pi(&pi)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_pi(&pihalf)) != MP_OKAY) {
goto _ERR;
}
pihalf.exp -= 1;
if ((err = mpf_const_pi(&piquart)) != MP_OKAY) {
goto _ERR;
}
piquart.exp -= 1;
// nothing to do if it is already small enough
if (mpf_cmp(&x, &piquart) != MP_GT) {
if ((err = mpf_copy(a, b)) != MP_OKAY) {
goto _ERR;
}
*k = 0;
goto _ERR;
}
// it starts to get tricky for x < 3pi/4 especially around pi/2
// but not for the reduction part
if ((err = mpf_mul(&three, &piquart, &three)) != MP_OKAY) {
goto _ERR;
}
if (mpf_cmp(&x, &three) == MP_LT) {
if ((err = mpf_sub(&x, &pihalf, &x)) != MP_OKAY) {
goto _ERR;
}
x.mantissa.sign = a->mantissa.sign;
if ((err = mpf_copy(&x, b)) != MP_OKAY) {
goto _ERR;
}
*k = 1;
goto _ERR;
}
sign = a->mantissa.sign;
// size of integer part in bits
size = a->radix + a->exp;
if (size < 0) {
size = 0;
}
// Reduction must be done in precision
// work_precision_(base 2) + log_2(x)
// if we have an integer part
// But the main problem with such large numbers lies in the loss of accuracy
// in the original number. That needs to get caught in the calling function.
oldprec = eps;
if (size > 0) {
newprec = oldprec + size + 3;
} else {
newprec = oldprec + 3;
}
// we need as many digits of pi as there are digits in the integer part of the
// number, so for e.g.: 1e308 we need 308 decimal digits of pi.
// Computing that much may take a while.
// Compute remainder of x/Pi/2
if ((err = mpf_normalize_to(&pi, newprec)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_const_pi(&pi)) != MP_OKAY) {
goto _ERR;
}
// k = round(x * 2/Pi)
if ((err = mpf_normalize_to(&x, newprec)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&K, newprec)) != MP_OKAY) {
goto _ERR;
}
// multiply by two
x.exp += 1;
if ((err = mpf_div(&x, &pi, &K)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_round(&K, &K)) != MP_OKAY) {
goto _ERR;
}
// undo multiplying by two
x.exp -= 1;
// r = x - k * Pi/2
if ((err = mpf_normalize_to(&r, newprec)) != MP_OKAY) {
goto _ERR;
}
pi.exp -= 1;
if ((err = mpf_mul(&K, &pi, &pi)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_sub(&x, &pi, &r)) != MP_OKAY) {
goto _ERR;
}
// TODO: check for size of "size" and don't delete pi if "size" is moderate
// mpf_const_pi(NULL);
if ((err =
mp_div_2d(&K.mantissa, abs(K.exp), &K.mantissa, NULL)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&r, oldprec)) != MP_OKAY) {
goto _ERR;
}
// we need the last two bits only
*k = K.mantissa.dp[0];
r.mantissa.sign = a->mantissa.sign;
mpf_copy(&r, b);
err = MP_OKAY;
_ERR:
mpf_clear_multi(&pi, &pihalf, &piquart, &r, &x, &three, &one, &K, NULL);
return err;
}