-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtan.c
227 lines (196 loc) · 5.31 KB
/
tan.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
/**
* Function to compute the tan and the Cotan functions on [-pi/2,pi/2]
*
* Author : Daramy Catherine (Catherine.Daramy@ens-lyon.fr)
*
* Date of creation : 11/03/2002
* Last Modified : 15/03/2003
*/
#include <stdio.h>
#include <stdlib.h>
#include "tan.h"
#include "coefpi2.h"
void tan(scs_ptr);
void cotan(scs_ptr);
int rem_pio2_scs(scs_ptr, scs_ptr);
/**
* WHAT WE CAN DO :
*
* We are computing tan and cotan as the same time, assuming that tan (x + k*Pi/2) = Cotan (x)
*
*
* 1) Range reduction if needed ... find x in [-Pi/2, +Pi/2] tan is Pi-periodic
*
* 2) Third range reduction ... find x in [ 0, Pi/2] tan(x) = -tan(-x)
*
* 3) Polynomial evaluation of P(X) degree 34 (? terms to computes)
*
* (-149)
* Approximation error: |err| < 2^
*
*
* 4) "Reconstruction":
*
* tan(x) = P(X)
*
*/
/*
* CALCUL JUSTE ....
* Compute the tan (or cotan) of x in double floating point arithmetic with
* correct rounding.
*
* - Input x is assumed to be bounded by ~pi/2 (~ ????) in magnitude.
* - we consider each scs digit to store 30 bits
* - It computes the polynom in one time ...
*/
void scs_tan(scs_ptr x_scs){
scs_t res_scs;
scs_t x2;
int i;
scs_square(x2, x_scs);
/* x < 2^-18 => tan(x)~x+x^3/3+x^5/15+x^7/315 with accuracy 2^-143 */
if(x_scs->exception.i[HI_ENDIAN] < 0x5f76b88){ /* Test if x<2^(-18) */
scs_mul(res_scs, cste_poly_ptr[0], x2);
scs_add(res_scs, cste_poly_ptr[1], res_scs);
scs_mul(res_scs, res_scs, x2);
scs_add(res_scs, cste_poly_ptr[2], res_scs);
scs_mul(res_scs, res_scs, x2);
scs_mul(res_scs, x_scs, res_scs);
scs_add(x_scs, x_scs, res_scs);
return;
}
/* Polynomial evaluation of tan(x) */
else {
scs_mul(res_scs, constant_poly_ptr[0], x2);
for(i=1; i<33; i++){ /* accuracy 2^(-151) */
scs_add(res_scs, constant_poly_ptr[i], res_scs);
scs_mul(res_scs, res_scs, x2);
}
scs_mul(res_scs, res_scs, x_scs);
scs_add(x_scs, x_scs, res_scs);
return;
}
}
/*************************************************************
*************************************************************
* ROUNDED TO NEAREST
*************************************************************
*************************************************************/
double scs_tan_rn(double x){
scs_t sc1;
scs_t sc2;
double resd;
int N;
#if DEBUG
double deb1, deb2;
#endif
scs_set_d(sc1, x);
N = rem_pio2_scs(sc2, sc1); /* x (=sc2) is in [-Pi/4,Pi/4] */
N = N & 0x0000001; /* extract the 2 last bits of N */
switch (N){
case 0:
scs_tan(sc2);
scs_get_d(&resd, sc2);
return resd;
break;
case 1:
scs_tan(sc2);
scs_inv(sc2, sc2);
scs_get_d(&resd, sc2);
return -(resd);
break;
default:
fprintf(stderr,"ERREUR: %d is not a valid value in sn_tan \n", N);
return 0.0;
}
}
/*************************************************************
*************************************************************
* ROUNDED TOWARD -INFINITY
*************************************************************
*************************************************************/
double scs_tan_rd(double x){
scs_t sc1, sc2;
double resd;
int N;
scs_set_d(sc1, x);
N = rem_pio2_scs(sc2, sc1); /* x is in [-Pi/4,Pi/4] */
N = N & 0x0000001; /* extract the 2 last bits of N */
switch (N){
case 0:
scs_tan(sc2);
scs_get_d_minf(&resd, sc2);
return resd;
break;
case 1:
scs_tan(sc2);
scs_inv(sc2, sc2);
scs_get_d_pinf(&resd, sc2);
return -(resd);
break;
default:
fprintf(stderr,"ERREUR: %d is not a valid value in tan_rd \n", N);
exit(1);
}
return resd;
}
/*************************************************************
*************************************************************
* ROUNDED TOWARD +INFINITY
*************************************************************
*************************************************************/
double scs_tan_ru(double x){
scs_t sc1, sc2;
double resd;
int N;
scs_set_d(sc1, x);
N = rem_pio2_scs(sc2, sc1); /* x is in [-Pi/4,Pi/4] */
N = N & 0x0000001; /* extract the 2 last bits of N */
switch (N){
case 0:
scs_tan(sc2);
scs_get_d_pinf(&resd, sc2);
return resd;
break;
case 1:
scs_tan(sc2);
scs_inv(sc2, sc2);
scs_get_d_minf(&resd, sc2);
return -(resd);
break;
default:
fprintf(stderr,"ERREUR: %d is not a valid value in su_tan \n", N);
exit(1);
}
return resd;
}
/*************************************************************
*************************************************************
* ROUNDED TOWARD ZERO
*************************************************************
*************************************************************/
double scs_tan_rz(double x){
scs_t sc1, sc2;
double resd;
int N;
scs_set_d(sc1, x);
N = rem_pio2_scs(sc2, sc1); /* x is in [-Pi/4,Pi/4] */
N = N & 0x0000001; /* extract the 2 last bits of N */
switch (N){
case 0:
scs_tan(sc2);
scs_get_d_zero(&resd, sc2);
return resd;
break;
case 1:
scs_tan(sc2);
scs_inv(sc2, sc2);
scs_get_d_zero(&resd, sc2);
return -(resd);
break;
default:
fprintf(stderr,"ERREUR: %d is not a valid value in su_tan \n", N);
exit(1);
}
return resd;
}