PROCEDURE reduce(x: rational);
VAR g: INTEGER;
BEGIN
ASSERT(x.den # 0);
g := gcd(ABS(x.num),ABS(x.den));
x.num := x.num DIV g; x.den := x.den DIV g;
IF x.den < 0 THEN
x.num := -x.num; x.den := -x.den
END
END reduce;The denominator condition ensures , including when the numerator is zero. Dividing both components by the same nonzero integer preserves the fraction, and the final simultaneous sign change preserves it again. The resulting numerator and denominator are coprime; for zero the denominator becomes one.
Articles by others on the same topic
There are currently no matching articles.