Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2): Difference between revisions
Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) (view source)
Revision as of 12:15, 9 March 2023
, 1 year ago→{{header|Python}}
Line 4,609:
=={{header|Python}}==
<syntaxhighlight lang="python">
Line 4,766 ⟶ 4,759:
while not done:
(a12, a1, a2, a, b12, b1, b2, b) = env[ng]
if b == 0 and b1 == 0 and b2 == 0 and b12 == 0:
retval =
elif b == 0 and b2
absorb_x_term ()▼
elif b == 0 or b2 == 0:
absorb_y_term ()
Line 4,781 ⟶ 4,773:
q1 = a1 // b1
q2 = a2 // b2
if
absorb_x_term ()▼
elif q1_diff < q2_diff:▼
absorb_y_term ()▼
elif b12 == 0 or q != a12 // b12:▼
#▼
# 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▼
# Rather than compare fractions, we will put the▼
# numerators over a common denominator of b*b1*b2,▼
# Output a term. Notice the resemblance to r2cf.
env[0] = (b12, b1, b2, b, # Divisors.
Line 4,819 ⟶ 4,784:
retval = (None if treat_as_infinite (q) else q)
done = True
▲ # Rather than compare fractions, we will put the
▲ # numerators over a common denominator of b*b1*b2,
▲ absorb_x_term ()
return retval
Line 4,850 ⟶ 4,826:
print (" 4 => ", cf2string (four))
print (" (1 + 1/sqrt(2))/2 => ",
cf2string (cf_div (cf_add (one, cf_div (one, sqrt2)), two))
" method 1")
print (" (1 + 1/sqrt(2))/2 => ",
cf2string (NG8 (1, 0, 0, 1, 0, 0, 0, 8) (silver_ratio,
silver_ratio)),
" method 2")
print (" (1 + 1/sqrt(2))/2 => ",
cf2string (cf_div (cf_div (cf_div (silver_ratio, sqrt2),
" method 3")
print (" sqrt(2) + sqrt(2) => ", cf2string (cf_add (sqrt2, sqrt2)))
print (" sqrt(2) - sqrt(2) => ", cf2string (cf_sub (sqrt2, sqrt2)))
Line 4,870 ⟶ 4,856:
3 => [3]
4 => [4]
(1 + 1/sqrt(2))/2 => [0;1,5,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...] method 1
(1 + 1/sqrt(2))/2 => [0;1,5,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...] method 2
(1 + 1/sqrt(2))/2 => [0;1,5,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...] method 3
sqrt(2) + sqrt(2) => [2;1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,4,1,...]
sqrt(2) - sqrt(2) => [0]
|