repo.or.cz
/
frac.git
/
blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
log
|
graphiclog1
|
graphiclog2
|
commit
|
commitdiff
|
tree
|
refs
|
edit
|
fork
blame
|
history
|
raw
|
HEAD
Merged signal and get.
[frac.git]
/
sqrt2.c
blob
87ce1284114d906635cdd3e0c0930395953d56b9
1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <gmp.h>
4
#include
"cf.h"
5
6
static void
*
sqrt2
(
cf_t cf
) {
7
mpz_t z
;
8
mpz_init
(
z
);
9
mpz_set_ui
(
z
,
1
);
10
if
(!
cf_wait
(
cf
))
goto
finish
;
11
cf_put
(
cf
,
z
);
12
mpz_set_ui
(
z
,
2
);
13
while
(
cf_wait
(
cf
)) {
14
cf_put
(
cf
,
z
);
15
}
16
finish
:
17
mpz_clear
(
z
);
18
return
NULL
;
19
}
20
21
cf_t
cf_new_sqrt2
() {
22
return
cf_new
(
sqrt2
,
NULL
);
23
}
24
25
int
main
() {
26
cf_t x
,
conv
;
27
x
=
cf_new_sqrt2
();
28
conv
=
cf_new_convergent
(
x
);
29
30
mpz_t p
,
q
;
31
mpz_init
(
p
);
32
mpz_init
(
q
);
33
for
(
int
i
=
0
;
i
<
10
;
i
++) {
34
cf_get
(
p
,
conv
);
35
cf_get
(
q
,
conv
);
36
gmp_printf
(
"p/q = %Zd/%Zd
\n
"
,
p
,
q
);
37
}
38
cf_free
(
conv
);
39
cf_free
(
x
);
40
mpz_clear
(
p
);
41
mpz_clear
(
q
);
42
return
0
;
43
}