forked from douglascrockford/DEC64
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdec64_math.c
331 lines (306 loc) · 8.02 KB
/
dec64_math.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
/*
dec64_math.c
Elementary functions for DEC64.
dec64.com
2016-01-07
Public Domain
No warranty.
This file is a placeholder. It should be replaced with functions that are
faster and more accurate.
*/
#include "dec64.h"
#include "dec64_math.h"
#define D_2 0x200LL
#define D_E 0x6092A113D8D574F0LL
#define D_HALF 0x5FFLL
#define D_HALF_PI 0x37CE4F32BB21A6F0LL
#define D_NPI 0x9063619A89BCB4F0LL
#define D_PI 0x6F9C9E6576434CF0LL
#define D_2PI 0x165286144ADA42F1LL
dec64 dec64_acos(dec64 x) {
dec64 result = dec64_subtract(
D_HALF_PI,
dec64_asin(x)
);
return result;
}
dec64 dec64_asin(dec64 x) {
if (dec64_equal(x, DEC64_ONE) == DEC64_TRUE) {
return D_HALF_PI;
}
if (dec64_equal(x, DEC64_NEGATIVE_ONE) == DEC64_TRUE) {
return dec64_neg(D_HALF_PI);
}
if (
dec64_is_any_nan(x) == DEC64_TRUE ||
dec64_less(DEC64_ONE, dec64_abs(x)) == DEC64_TRUE
) {
return DEC64_NAN;
}
dec64 bottom = D_2;
dec64 factor = x;
dec64 x2 = dec64_multiply(x, x);
dec64 result = factor;
while (1) {
factor = dec64_divide(
dec64_multiply(
dec64_multiply(dec64_dec(bottom), x2),
factor
),
bottom
);
dec64 progress = dec64_add(
result,
dec64_divide(factor, dec64_inc(bottom))
);
if (result == progress) {
break;
}
result = progress;
bottom = dec64_add(bottom, D_2);
}
return result;
}
dec64 dec64_atan(dec64 x) {
return dec64_asin(
dec64_divide(
x,
dec64_sqrt(dec64_inc(dec64_multiply(x, x)))
)
);
}
dec64 dec64_atan2(dec64 y, dec64 x) {
if (dec64_is_zero(x) == DEC64_TRUE) {
if (dec64_is_zero(y) == DEC64_TRUE) {
return DEC64_NAN;
} else if (y < 0) {
return dec64_neg(D_HALF_PI);
} else {
return D_HALF_PI;
}
} else {
dec64 atan = dec64_atan(dec64_divide(y, x));
if (x < 0) {
if (y < 0) {
return dec64_subtract(atan, D_HALF_PI);
} else {
return dec64_add(atan, D_HALF_PI);
}
} else {
return atan;
}
}
}
dec64 dec64_cos(dec64 x) {
return dec64_sin(dec64_add(x, D_HALF_PI));
}
dec64 dec64_exp(dec64 x) {
dec64 result = dec64_inc(x);
dec64 divisor = D_2;
dec64 term = x;
while (1) {
term = dec64_divide(
dec64_multiply(term, x),
divisor
);
dec64 progress = dec64_add(result, term);
if (result == progress) {
break;
}
result = progress;
divisor = dec64_inc(divisor);
}
return result;
}
dec64 dec64_exponentiate(dec64 coefficient, dec64 exponent) {
if (dec64_is_zero(exponent) == DEC64_TRUE) {
return DEC64_ONE;
}
// Adjust for a negative exponent.
if (exponent < 0) {
coefficient = dec64_divide(DEC64_ONE, coefficient);
exponent = dec64_neg(exponent);
}
if (dec64_is_any_nan(coefficient) == DEC64_TRUE) {
return DEC64_NAN;
}
if (dec64_is_zero(coefficient) == DEC64_TRUE) {
return 0;
}
// If the exponent is an integer, then use the squaring method.
if (exponent > 0 && dec64_exponent(exponent) == 0) {
dec64 aux = DEC64_ONE;
int64 n = dec64_coefficient(exponent);
if (n <= 1) {
return coefficient;
}
while (n > 1) {
if ((n & 1) != 0) {
aux = dec64_multiply(aux, coefficient);
}
coefficient = dec64_multiply(coefficient, coefficient);
n /= 2;
}
return (n == 1)
? dec64_multiply(aux, coefficient)
: aux;
}
// Otherwise do it the hard way.
return dec64_exp(dec64_multiply(
dec64_log(coefficient),
exponent
));
}
dec64 dec64_log(dec64 x)
{
if (x <= 0 || dec64_is_any_nan(x) == DEC64_TRUE) {
return DEC64_NAN;
}
if (dec64_equal(x, DEC64_ONE) == DEC64_TRUE) {
return DEC64_ZERO;
}
if (dec64_equal(x, D_HALF) == DEC64_TRUE) {
return dec64_new(-6931471805599453, -16);
}
if (x == D_E) {
return DEC64_ONE;
}
dec64 y = dec64_divide(dec64_dec(x), x);
dec64 factor = y;
dec64 result = factor;
dec64 divisor = D_2;
while (1) {
factor = dec64_multiply(factor, y);
dec64 progress = dec64_add(
result,
dec64_divide(factor, divisor)
);
if (result == progress) {
break;
}
result = progress;
divisor = dec64_inc(divisor);
}
return result;
}
dec64 dec64_root(dec64 degree, dec64 radicand) {
int repeat;
dec64 result;
degree = dec64_normal(degree);
if (
dec64_is_any_nan(radicand) == DEC64_TRUE ||
dec64_is_zero(degree) == DEC64_TRUE ||
degree < 0 ||
dec64_exponent(degree) != 0 ||
(
radicand < 0 &&
(dec64_coefficient(degree) & 1) == 0
)
) {
return DEC64_NAN;
}
if (dec64_is_zero(radicand) == DEC64_TRUE) {
return DEC64_ZERO;
}
if (degree == DEC64_ONE) {
return radicand;
}
if (degree == D_2) {
return dec64_sqrt(radicand);
}
dec64 degree_minus_one = dec64_dec(degree);
repeat = 64;
result = DEC64_ONE;
while (repeat > 0) {
dec64 progress = dec64_divide(
dec64_add(
dec64_multiply(result, degree_minus_one),
dec64_divide(
radicand,
dec64_exponentiate(result, degree_minus_one)
)
),
degree
);
if (progress == result) {
break;
}
result = progress;
repeat -= 1;
}
return result;
}
dec64 dec64_sin(dec64 x) {
while (dec64_less(D_PI, x) == DEC64_TRUE) {
x = dec64_subtract(x, D_PI);
x = dec64_subtract(x, D_PI);
}
while (dec64_less(x, D_NPI) == DEC64_TRUE) {
x = dec64_add(x, D_PI);
x = dec64_add(x, D_PI);
}
int neg = 0;
if (x < 0) {
x = dec64_neg(x);
neg = -1;
}
if (dec64_less(D_HALF_PI, x) == DEC64_TRUE) {
x = dec64_subtract(D_PI, x);
}
dec64 result;
if (x == D_HALF_PI) {
result = DEC64_ONE;
} else {
dec64 x2 = dec64_multiply(x, x);
dec64 order = DEC64_ONE;
dec64 term = x;
result = term;
while (1) {
term = dec64_multiply(term, x2);
order = dec64_inc(order);
term = dec64_divide(term, order);
order = dec64_inc(order);
term = dec64_divide(term, order);
dec64 progress = dec64_subtract(result, term);
term = dec64_multiply(term, x2);
order = dec64_inc(order);
term = dec64_divide(term, order);
order = dec64_inc(order);
term = dec64_divide(term, order);
progress = dec64_add(progress, term);
if (progress == result) {
break;
}
result = progress;
}
}
if (neg) {
result = dec64_neg(result);
}
return result;
}
dec64 dec64_sqrt(dec64 radicand) {
if (dec64_is_any_nan(radicand) != DEC64_TRUE && radicand >= 0) {
if (dec64_coefficient(radicand) == 0) {
return DEC64_ZERO;
}
int repeat = 8;
dec64 result = radicand;
do {
result = dec64_half(dec64_add(
result,
dec64_divide(radicand, result)
));
repeat -= 1;
} while (repeat > 0);
return result;
} else {
return DEC64_NAN;
}
}
dec64 dec64_tan(dec64 x) {
return dec64_divide(
dec64_sin(x),
dec64_cos(x)
);
}