-
Notifications
You must be signed in to change notification settings - Fork 59
/
quotient.coffee
109 lines (79 loc) · 1.32 KB
/
quotient.coffee
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# Divide polynomials
Eval_quotient = ->
push(cadr(p1)); # 1st arg, p(x)
Eval()
push(caddr(p1)); # 2nd arg, q(x)
Eval()
push(cadddr(p1)); # 3rd arg, x
Eval()
p1 = pop(); # default x
if (p1 == symbol(NIL))
p1 = symbol(SYMBOL_X)
push(p1)
divpoly()
#-----------------------------------------------------------------------------
#
# Divide polynomials
#
# Input: tos-3 Dividend
#
# tos-2 Divisor
#
# tos-1 x
#
# Output: tos-1 Quotient
#
#-----------------------------------------------------------------------------
#define DIVIDEND p1
#define DIVISOR p2
#define X p3
#define Q p4
#define QUOTIENT p5
divpoly = ->
h = 0
i = 0
m = 0
n = 0
x = 0
#U **dividend, **divisor
save()
p3 = pop()
p2 = pop()
p1 = pop()
h = tos
dividend = tos
push(p1)
push(p3)
m = coeff() - 1; # m is dividend's power
divisor = tos
push(p2)
push(p3)
n = coeff() - 1; # n is divisor's power
x = m - n
push_integer(0)
p5 = pop()
while (x >= 0)
push(stack[dividend+m])
push(stack[divisor+n])
divide()
p4 = pop()
for i in [0..n]
push(stack[dividend+x + i])
push(stack[divisor+i])
push(p4)
multiply()
subtract()
stack[dividend+x + i] = pop()
push(p5)
push(p4)
push(p3)
push_integer(x)
power()
multiply()
add()
p5 = pop()
m--
x--
moveTos h
push(p5)
restore()