Cleaned up lock mess monster.
[frac.git] / sqrt2.c
blob87ce1284114d906635cdd3e0c0930395953d56b9
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <gmp.h>
4 #include "cf.h"
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);
16 finish:
17 mpz_clear(z);
18 return NULL;
21 cf_t cf_new_sqrt2() {
22 return cf_new(sqrt2, NULL);
25 int main() {
26 cf_t x, conv;
27 x = cf_new_sqrt2();
28 conv = cf_new_convergent(x);
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);
38 cf_free(conv);
39 cf_free(x);
40 mpz_clear(p);
41 mpz_clear(q);
42 return 0;