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 00:26, 10 March 2023
, 1 year ago→{{header|ATS}}
Line 781:
cf_make
let
typedef state = '(ng8,
val state_ref : ref state = ref '(ng8, i2sz 0, i2sz 0, false)▼
Size_t -<cloref1> integer,
Size_t)
fn xsource (i : Size_t) :<cloref1> integer = x[i]
fn ysource (i : Size_t) :<cloref1> integer = y[i]
fn neginf_source (i : Size_t) :<cloref1> integer =
ref '(ng8, xsource, ysource, i2sz 0, i2sz 0)
in
lam () =<cloref1>
let
fnx
loop (
let
val '(@(a12, a1, a2, a, b12, b1, b2, b),
in
if iseqz b12 * iseqz b1 * iseqz b2 * iseqz b then
neginf<integerknd> ()
else if iseqz b * iseqz b2 then
absorb_x_term
else if iseqz b + iseqz b2 then
absorb_y_term
else if iseqz b1 then
absorb_x_term
else
let
Line 816 ⟶ 828:
!state_ref :=
'(@(b12, b1, b2, b, r12, r1, r2, r),
xsource, ysource, i, j
q
end
Line 828 ⟶ 840:
in
if abs (n1 -! n) > abs (n2 -! n) then
absorb_x_term
else
absorb_y_term
end
end
end
and
absorb_x_term (
let
val '(@(a12, a1, a2, a, b12, b1, b2, b),
val term = xsource i
▲ (if overflow then neginf<integerknd> () else x[i])
in
if term <> neginf<integerknd> () then
Line 855 ⟶ 866:
and b = b1
in
!state_ref :=
'(@(a12, a1, a2, a, b12, b1, b2, b),
xsource, ysource, succ i, j);
(* Be aware: this is not a tail recursion! *)
loop
▲ succ i, j, overflow)
end
with
| ~ gint_overflow () =>
(* Replace the sources with
terms. (You have to replace BOTH sources.) *)
!state_ref :=
'(@(a12, a1, a12, a1, b12, b1, b12, b1),
neginf_source, neginf_source, succ i, j);
loop ()
end
end
else
!state_ref
'(@(a12, a1, a12, a1, b12, b1, b12, b1),
xsource, ysource, succ i, j);
loop ()
end
end
and
absorb_y_term (
let
val '(@(a12, a1, a2, a, b12, b1, b2, b),
val term = ysource j
in
if term <> neginf<integerknd> () then
Line 889 ⟶ 911:
and b = b2
in
!state_ref :=
'(@(a12, a1, a2, a, b12, b1, b2, b),
xsource, ysource, i, succ j);
(* Be aware: this is not a tail recursion! *)
loop
▲ i, succ j, overflow)
end
with
| ~ gint_overflow () =>
(* Replace the sources with
terms. (You have to replace BOTH sources.) *)
!state_ref :=
'(@(a12, a12, a2, a2, b12, b12, b2, b2),
neginf_source, neginf_source, i, succ j);
loop ()
end
end
else
!state_ref
'(@(a12, a12, a2, a2, b12, b12, b2, b2),
xsource, ysource, i, succ j);
loop ()
end
end
in
loop (
end
end
|