-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdegree.cpp
66 lines (61 loc) · 1.03 KB
/
degree.cpp
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
#include "stdafx.h"
#include "defs.h"
void
eval_degree(void)
{
push(cadr(p1));
eval();
push(caddr(p1));
eval();
p1 = pop();
if (p1 == symbol(NIL))
guess();
else
push(p1);
degree();
}
//-----------------------------------------------------------------------------
//
// Find the degree of a polynomial
//
// Input: tos-2 p(x)
//
// tos-1 x
//
// Output: Result on stack
//
// Note: Finds the largest numerical power of x. Does not check for
// weirdness in p(x).
//
//-----------------------------------------------------------------------------
#define POLY p1
#define X p2
#define DEGREE p3
void
degree(void)
{
save();
X = pop();
POLY = pop();
DEGREE = zero;
yydegree(POLY);
push(DEGREE);
restore();
}
void
yydegree(U *p)
{
if (equal(p, X)) {
if (iszero(DEGREE))
DEGREE = one;
} else if (car(p) == symbol(POWER)) {
if (equal(cadr(p), X) && isnum(caddr(p)) && lessp(DEGREE, caddr(p)))
DEGREE = caddr(p);
} else if (iscons(p)) {
p = cdr(p);
while (iscons(p)) {
yydegree(car(p));
p = cdr(p);
}
}
}