summaryrefslogtreecommitdiff
path: root/ipl/procs/converge.icn
blob: d64a7a76fc6af4542fdf2d54406044f178571b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
############################################################################
#
#	File:     converge.icn
#
#	Subject:  Procedure to produce continued-fraction convergents
#
#	Author:   Ralph E. Griswold
#
#	Date:     June 7, 2000
#
############################################################################
#
#  This file is in the public domain.
#
############################################################################
#
#  This procedure produces continued-fraction convergents from a list
#  of partial quotients.
#
############################################################################
#
#  Links:  rational
#
############################################################################

link rational

procedure converge(seq)		#: continued-fraction convergents
   local prev_p, prev_q, p, q, t

   seq := copy(seq)

   prev_p := [0, 1]
   prev_q := [1, 0]

   while t := get(seq) do {
      p := t * prev_p[2] + prev_p[1]
      q := t * prev_q[2] + prev_q[1]
      suspend rational(p, q, 1)
      prev_p[1] := prev_p[2]
      prev_p[2] := p
      prev_q[1] := prev_q[2]
      prev_q[2] := q
      }

end