2004-03-03 21:24:06 +01:00
|
|
|
// Divide rational numbers
|
|
|
|
//
|
|
|
|
// Input: tos-2 dividend
|
|
|
|
//
|
|
|
|
// tos-1 divisor
|
|
|
|
//
|
|
|
|
// Output: quotient on stack
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
void
|
|
|
|
qdiv(void)
|
|
|
|
{
|
|
|
|
unsigned int *aa, *bb, *c;
|
|
|
|
|
|
|
|
save();
|
|
|
|
|
|
|
|
p2 = pop();
|
|
|
|
p1 = pop();
|
|
|
|
|
|
|
|
// zero?
|
|
|
|
|
|
|
|
if (MZERO(p2->u.q.a))
|
|
|
|
stop("divide by zero");
|
|
|
|
|
|
|
|
if (MZERO(p1->u.q.a)) {
|
2004-06-25 22:45:15 +02:00
|
|
|
push(zero);
|
2004-03-03 21:24:06 +01:00
|
|
|
restore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aa = mmul(p1->u.q.a, p2->u.q.b);
|
|
|
|
bb = mmul(p1->u.q.b, p2->u.q.a);
|
|
|
|
|
|
|
|
c = mgcd(aa, bb);
|
|
|
|
|
|
|
|
MSIGN(c) = MSIGN(bb);
|
|
|
|
|
|
|
|
p1 = alloc();
|
|
|
|
|
|
|
|
p1->k = NUM;
|
|
|
|
|
|
|
|
p1->u.q.a = mdiv(aa, c);
|
|
|
|
p1->u.q.b = mdiv(bb, c);
|
|
|
|
|
|
|
|
mfree(aa);
|
|
|
|
mfree(bb);
|
|
|
|
|
|
|
|
push(p1);
|
|
|
|
|
|
|
|
restore();
|
|
|
|
}
|