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

Line 781:
cf_make
let
typedef state = '(ng8, Size_t, Size_t, bool)
Size_t -<cloref1> succ iinteger, j, overflow)
val state_ref : ref state = ref '(ng8, i2sz 0, i2sz 0, false)
Size_t -<cloref1> integer,
iSize_t, succ j, overflow)
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 =
(if overflow then neginf<integerknd> () else x[i])
 
val state_ref : ref state = ref '(ng8, i2sz 0, i2sz 0, false)
ref '(ng8, xsource, ysource, i2sz 0, i2sz 0)
in
lam () =<cloref1>
let
fnx
loop (state : state) : integer =
let
val '(@(a12, a1, a2, a, b12, b1, b2, b),
ixsource, jysource, overflowi, j) = state!state_ref
in
if iseqz b12 * iseqz b1 * iseqz b2 * iseqz b then
neginf<integerknd> ()
else if iseqz b * iseqz b2 then
absorb_x_term state()
else if iseqz b + iseqz b2 then
absorb_y_term state()
else if iseqz b1 then
absorb_x_term state()
else
let
Line 816 ⟶ 828:
!state_ref :=
'(@(b12, b1, b2, b, r12, r1, r2, r),
xsource, ysource, i, j, overflow);
q
end
Line 828 ⟶ 840:
in
if abs (n1 -! n) > abs (n2 -! n) then
absorb_x_term state()
else
absorb_y_term state()
end
end
end
and
absorb_x_term (state : state) : integer =
let
val '(@(a12, a1, a2, a, b12, b1, b2, b),
ixsource, jysource, overflowi, j) = state!state_ref
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 '(@(a12, a1, a2, a, b12, b1, b2, b),
succ i, j, overflow)
end
with
| ~ gint_overflow () =>
loop '(@(a12, a1, a12, a1, b12, b1, b12, b1),begin
(* Replace the sources with succones i,that j,return true)no
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
loop '(@(a12, a1, a12, a1, b12, b1, b12, b1),begin
!state_ref succ i, j, overflow):=
'(@(a12, a1, a12, a1, b12, b1, b12, b1),
xsource, ysource, succ i, j);
loop ()
end
end
and
absorb_y_term (state : state) : integer =
let
val '(@(a12, a1, a2, a, b12, b1, b2, b),
ixsource, jysource, overflowi, j) = state!state_ref
val term = ysource j
(if overflow then neginf<integerknd> () else y[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 '(@(a12, a1, a2, a, b12, b1, b2, b),
i, succ j, overflow)
end
with
| ~ gint_overflow () =>
loop '(@(a12, a12, a2, a2, b12, b12, b2, b2),begin
(* Replace the sources with i,ones succthat j,return true)no
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
loop '(@(a12, a12, a2, a2, b12, b12, b2, b2),begin
!state_ref i, succ j, overflow):=
'(@(a12, a12, a2, a2, b12, b12, b2, b2),
xsource, ysource, i, succ j);
loop ()
end
end
in
loop (!state_ref)
end
end
1,448

edits