-
Notifications
You must be signed in to change notification settings - Fork 0
/
w.c
352 lines (207 loc) · 10.6 KB
/
w.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
------- Git -----------------
cd existing_folder
git init
git remote add origin [email protected]:adammajewski/wake_gmp.git
git add .
git commit -m ""
git push -u origin master
-------------------------------
?? http://stackoverflow.com/questions/2380415/how-to-cut-a-mpz-t-into-two-parts-using-gmp-lib-on-c
to compile from console:
gcc w.c -lgmp -lmpfr -Wall
to run from console :
./a.out
tested on Ubuntu 14.04 LTS
uiIADenominator = 89
Using MPFR-3.1.2-p3 with GMP-5.1.3 with precision = 200 bits
internal angle = 34/89
first external angle :
period = denominator of internal angle = 89
external angle as a decimal fraction = 179622968672387565806504265/618970019642690137449562111 = 179622968672387565806504265 /( 2^89 - 1)
External Angle as a floating point decimal number = 2.9019655713870868535821260055542440298749779423213948304299730531995503353103626302473331181359966368582651105245850405837027542373052381532777325121338632071561064451614697645709384232759475708007812e-1
external angle as a binary rational (string) : 1001010010010100101001001010010010100101001001010010100100101001001010010100100101001001/11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
external angle as a binary floating number in exponential form =0.10010100100101001010010010100100101001010010010100101001001010010010100101001001010010010100101001001010010100100101001001010010100100101001010010010100100101001010010010100100101001010010010100101001*2^-1
external angle as a binary floating number in periodic form =0.(01001010010010100101001001010010010100101001001010010100100101001001010010100100101001001)
----------------------------------------------------------
uiIADenominator = 31
Using MPFR-4.0.1 with GMP-6.1.1 with precision = 200 bits
internal angle = 1/31
first external angle :
period = denominator of internal angle = 31
external angle as a decimal fraction = 1/2147483647 = 1 /( 2^31 - 1)
External Angle as a floating point decimal number = 4.6566128752457969241057508271679984532147638747537340385623900399147124035218327351773467491518412546164991452584040446332679803851633049696400990590800086037362649120721624917962344625266268849372863e-10
external angle as a binary rational (string) : 1/1111111111111111111111111111111
external angle as a binary floating number in exponential form =0.10000000000000000000000000000001000000000000000000000000000000100000000000000000000000000000010000000000000000000000000000001000000000000000000000000000000100000000000000000000000000000010000000000000*2^-30
external angle as a binary floating number in periodic form =0.(0000000000000000000000000000001)
------------------------------------------------------------
*/
#include <stdlib.h> // malloc
#include <stdio.h>
#include <gmp.h> // for rational numbers
#include <mpfr.h> // for floating point mumbers
// rotation map
//the number n is always increased by n0 modulo d
// input : op = n/d ( rational number ) and n0 ( integer)
// n = (n + n0 ) % d
// d = d
// output = rop = n/d
void mpq_rotation(mpq_t rop, const mpq_t op, const mpz_t n0)
{
mpz_t n; // numerator
mpz_t d; // denominator
mpz_inits( n, d, NULL);
//
mpq_get_num (n, op); //
mpq_get_den (d, op);
// n = (n + n0 ) % d
mpz_add(n, n, n0);
mpz_mod( n, n, d);
// output
mpq_set_num(rop, n);
mpq_set_den(rop, d);
mpz_clears( n, d, NULL);
}
void mpq_wake(mpq_t rop, mpq_t op)
{
// arbitrary precision variables from GMP library
mpz_t n0 ; // numerator of q
mpz_t nc;
mpz_t n;
mpz_t d ; // denominator of q
mpz_t m; // 2^i
mpz_t num ; // numerator of rop
mpz_t den ; // denominator of rop
long long int i;
unsigned long int base = 2;
unsigned long int id;
int cmp;
mpz_inits(n, n0,nc,d,num,den,m, NULL);
mpq_get_num(n0,op);
mpq_get_den(d,op);
id = mpz_get_ui(d);
// if (n <= 0 || n >= d ) error !!!! bad input
mpz_sub(nc, d, n0); // nc = d - n0
mpz_set(n, n0);
mpz_set_ui(num, 0);
// rop
// num = numerator(rop)
// denominator = den(rop) = (2^i) -1
mpz_ui_pow_ui(den, base, id) ; // den = base^id
mpz_sub_ui(den, den, 1); // den = den-1
// numerator
for (i=0; i<id ; i++){
mpz_set_ui(m, 0);
cmp = mpz_cmp(n,nc);// Compare op1 and op2. Return a positive value if op1 > op2, zero if op1 = op2, or a negative value if op1 < op2.
if ( cmp>0 ) {
mpz_ui_pow_ui(m, 2, id-i-1); // m = 2^(id-i )
mpz_add(num, num, m); // num = num + m
if (mpz_cmp(num, den) >0) mpz_mod( num, num, den); // num = num % d ; if num==d gives 0
//gmp_printf("s = 1");
}
// else gmp_printf("s = 0");
//gmp_printf (" i = %ld internal angle = %Zd / %Zd ea = %Zd / %Zd ; m = %Zd \n", i, n, d, num, den, m);
// n = (n + n0 ) % d = rotation
mpz_add(n, n, n0);
if (mpz_cmp(n, d)>0) mpz_mod( n, n, d);
//
//
}
// rop = external angle
mpq_set_num(rop,num);
mpq_set_den(rop,den);
mpq_canonicalize (rop); // It is the responsibility of the user to canonicalize the assigned variable before any arithmetic operations are performed on that variable.
// clear memory
mpz_clears(n, n0, nc, d, num,den, m, NULL);
}
/*
http://stackoverflow.com/questions/9895216/remove-character-from-string-in-c
"The idea is to keep a separate read and write pointers (pr for reading and pw for writing),
always advance the reading pointer, and advance the writing pointer only when it's not pointing to a given character."
modified
remove first length2rmv chars and after that take only length2stay chars from input string
output = input string
*/
void extract_str(char* str, unsigned int length2rmv, unsigned long int length2stay) {
// separate read and write pointers
char *pr = str; // read pointer
char *pw = str; // write pointer
int i =0; // index
while (*pr) {
if (i>length2rmv-1 && i <length2rmv+length2stay)
pw += 1; // advance the writing pointer only when
pr += 1; // always advance the reading pointer
*pw = *pr;
i +=1;
}
*pw = '\0';
}
int main ()
{
// notation :
//number type : s = string ; q = rational ; z = integer, f = floating point
// base : b = binary ; d = decimal
char *sqdInternalAngle = "1/31";
mpq_t qdInternalAngle; // internal angle = rational number q = n/d
mpz_t den;
unsigned long int uiIADenominator;
mpq_t qdExternalAngle; // rational number q = n/d
char *sqbExternalAngle;
mpfr_t fdExternalAngle ; //
char *sfbExternalAngle; //
mp_exp_t exponent ; // holds the exponent for the result string ( EA)
mpz_t zdEANumerator;
mpz_t zdEADenominator;
mpfr_t EANumerator;
mpfr_t EADenominator;
mpfr_prec_t p = 200; // in bits , should be > denominator of internal angle
mpfr_set_default_prec (p); // but previously initialized variables are unaffected.
//mpfr_set_default_prec (precision);
// init variables
//mpf_init(fdExternalAngle);
mpz_inits(den, zdEANumerator,zdEADenominator, NULL);
mpq_inits (qdExternalAngle, qdInternalAngle, NULL); //
mpfr_inits(fdExternalAngle, EANumerator, EADenominator, NULL);
// set variables
mpq_set_str(qdInternalAngle, sqdInternalAngle, 10); // string is an internal angle
mpq_canonicalize (qdInternalAngle); // It is the responsibility of the user to canonicalize the assigned variable before any arithmetic operations are performed on that variable.
mpq_get_den(den,qdInternalAngle);
uiIADenominator = mpz_get_ui(den);
printf("uiIADenominator = %lu \n", uiIADenominator);
if ( p < uiIADenominator) printf("increase precision !!!!\n");
mpfr_printf("Using MPFR-%s with GMP-%s with precision = %u bits \n", mpfr_version, gmp_version, (unsigned int) p);
//
mpq_wake(qdExternalAngle, qdInternalAngle); // internal -> external
mpq_get_num(zdEANumerator ,qdExternalAngle);
mpq_get_den(zdEADenominator,qdExternalAngle);
// conversions
mpfr_set_z (EANumerator, zdEANumerator, GMP_RNDN);
mpfr_set_z (EADenominator, zdEADenominator, GMP_RNDN);
sqbExternalAngle = mpq_get_str (NULL, 2, qdExternalAngle); // rational number = fraction : from decimal to binary
mpfr_div (fdExternalAngle, EANumerator, EADenominator, GMP_RNDN);
sfbExternalAngle = (char*)malloc((sizeof(char) * uiIADenominator*2*4) + 3);
// mpfr_get_str (char *str, mpfr_exp_t *expptr, int b, size_t n, mpfr_t op, mpfr_rnd_t rnd)
if (sfbExternalAngle==NULL ) {printf("sfbExternalAngle error \n"); return 1;}
mpfr_get_str(sfbExternalAngle, &exponent, 2,200, fdExternalAngle, GMP_RNDN);
// principal Misiurewicz point of the wake is a landing point of the angle : nm/dm
// nm = dw+2; where zdEADenominator;
// dm = (2^r)*(2^r - 1) ; precision = log2(dm) = 2*r where r = denominator if internal angle
// print
gmp_printf ("internal angle = %Qd\n", qdInternalAngle); //
printf("first external angle : \n");
gmp_printf ("period = denominator of internal angle = %Zd\n", den); //
gmp_printf ("external angle as a decimal fraction = %Qd = %Zd /( 2^%Zd - 1) \n", qdExternalAngle, zdEANumerator, den); //
printf ("External Angle as a floating point decimal number = ");
mpfr_out_str (stdout, 10, p, fdExternalAngle, MPFR_RNDD); putchar ('\n');
gmp_printf ("external angle as a binary rational (string) : %s \n", sqbExternalAngle); //
printf ("external angle as a binary floating number in exponential form =0.%s*%d^%ld\n", sfbExternalAngle, 2, exponent);
extract_str(sfbExternalAngle, uiIADenominator+exponent, uiIADenominator);
printf ("external angle as a binary floating number in periodic form =0.(%s)\n", sfbExternalAngle);
// clear memory
//mpf_clear(fdExternalAngle);
mpq_clears(qdExternalAngle, qdInternalAngle, NULL);
mpz_clears(den, zdEANumerator, zdEADenominator, NULL);
mpfr_clears(fdExternalAngle, EANumerator, EADenominator, NULL);
free(sfbExternalAngle);
return 0;
}