forked from libtom/libtomfloat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmpf_kernel_atan.c
101 lines (91 loc) · 2.3 KB
/
mpf_kernel_atan.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
#include <tomfloat.h>
int mpf_kernel_atan(mp_float * a, mp_float * b, int hyper)
{
mp_float sum1, sum2, EPS, t2, t4, tt, tmp;
long n, eps, oldeps;
int err;
// checks & balances
// check for -1 < this < 1 must get done by caller
// atan(h)(0) = 0
if (mpf_iszero(a)) {
mpf_copy(a, b);
return MP_OKAY;
}
err = MP_OKAY;
oldeps = a->radix;
eps = oldeps + MP_DIGIT_BIT;
if ((err =
mpf_init_multi(eps, &sum1, &sum2, &EPS, &t2, &t4, &tt, &tmp,
NULL)) != MP_OKAY) {
return err;
}
if ((err = mpf_const_eps(&EPS)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(a, &sum1)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_normalize_to(&sum1, eps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_sqr(&sum1, &t2)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_sqr(&t2, &t4)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_div_d(&sum1, 3, &sum2)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_mul(&sum1, &t4, &tmp)) != MP_OKAY) {
goto _ERR;
}
// we skipped the first terms, hence...
n = 5;
//...a while-loop instead of my usual do-while-loops because it could
// already stop here if all the stars are aligned right.
while (mpf_cmp(&tmp, &EPS) != MP_LT) {
// Doing it this way saves some branching for the hyperbolic case and
// because of the rule "legibility wins if it is cheaply to have" it is
// done here.
// It can be done for the cosine, too.
if ((err = mpf_div_d(&tmp, n, &tt)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_add(&sum1, &tt, &sum1)) != MP_OKAY) {
goto _ERR;
}
n += 2;
if ((err = mpf_div_d(&tmp, n, &tt)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_add(&sum2, &tt, &sum2)) != MP_OKAY) {
goto _ERR;
}
n += 2;
if ((err = mpf_mul(&tmp, &t4, &tmp)) != MP_OKAY) {
goto _ERR;
}
}
if ((err = mpf_mul(&sum2, &t2, &sum2)) != MP_OKAY) {
goto _ERR;
}
if (hyper == 1) {
if ((err = mpf_add(&sum1, &sum2, &sum1)) != MP_OKAY) {
goto _ERR;
}
} else {
if ((err = mpf_sub(&sum1, &sum2, &sum1)) != MP_OKAY) {
goto _ERR;
}
}
if ((err = mpf_normalize_to(&sum1, oldeps)) != MP_OKAY) {
goto _ERR;
}
if ((err = mpf_copy(&sum1, b)) != MP_OKAY) {
goto _ERR;
}
_ERR:
mpf_clear_multi(&sum1, &sum2, &EPS, &t2, &t4, &tt, &tmp, NULL);
return err;
}