Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2): Difference between revisions

Line 2,463:
else
{
mpz_t/* q1_diffRather than compare fractions, q2_diff;we will put the
// numerators over a common denominator of b*b1*b2, and
mpz_inits (q1_diff, q2_diff, NULL);
// and then compare the new numerators. */
 
mpz_submpz_t (q1_diffn, q1n1, q)n2, n1_diff, n2_diff;
mpz_submpz_inits (q2_diffn, q2n1, qn2, n1_diff, n2_diff, NULL);
abc (n, rp->ng[ng8a], p->ng[ng8b1], p->ng[ng8b2]);
 
ifabc (mpz_cmpabsn1, (q1_diffp->ng[ng8a1], q2_diff) p->ng[ng8b], 0p->ng[ng8b2]);
abc (n2, r2p->ng[ng8a2], p->ng[ng8b], p->ng[ng8b1]);
mpz_initsmpz_sub (q1_diffn1_diff, q2_diffn1, NULLn);
mpz_sub (n2_diff, n2, n);
if (mpz_cmpabs (n1_diff, n2_diff) > 0)
absorb_x_term (p);
else if (mpz_cmpabs (q1_diff, q2_diff) < 0)
absorb_y_term (p);
else
{
//
// Because q = q1 = q2, the following also are true:
//
// a1/b1 - a/b = r1/b1 - r/b
// a2/b2 - a/b = r2/b2 - r/b
//
// So we can work with the remainders, which are
// smaller than the original numerators.
//
// Rather than compare fractions, we will put the
// numerators over a common denominator of b*b1*b2,
// and then compare the new numerators.
//
mpz_t n, n1, n2, n1_diff, n2_diff;
mpz_inits (n, n1, n2, n1_diff, n2_diff, NULL);
abc (n, r, p->ng[ng8b1], p->ng[ng8b2]);
abc (n1, r1, p->ng[ng8b], p->ng[ng8b2]);
abc (n2, r2, p->ng[ng8b], p->ng[ng8b1]);
mpz_sub (n1_diff, n1, n);
mpz_sub (n2_diff, n2, n);
if (mpz_cmpabs (n1_diff, n2_diff) > 0)
absorb_x_term (p);
else
absorb_y_term (p);
}
}
}
Line 2,646 ⟶ 2,623:
{{out}}
<pre>$ gcc -std=gnu2x -Wall -Wextra -g bivariate-continued-fraction-task-gmp.c -lgmp -lgc && ./a.out
golden ratio => [1;1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,...] (1 + sqrt(5))/2
silver ratio => [2;2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,...] 1 + sqrt(2)
sqrt(2) => [1;2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,...]
1,448

edits