Represent by the pair in the quadratic field . Its multiplication rule isThe constructor copies both rational components, so later reduction through another pointer cannot change the constructed value:
PROCEDURE mkIrr(r,i: rational): irrational;
VAR z: irrational;
BEGIN
NEW(z);
z.rat := mkRat(r.num,r.den);
z.irr := mkRat(i.num,i.den);
RETURN z
END mkIrr;
PROCEDURE addIrr(x,y: irrational): irrational;
BEGIN
RETURN mkIrr(addRat(x.rat,y.rat),addRat(x.irr,y.irr))
END addIrr;
PROCEDURE mulIrr(x,y: irrational): irrational;
VAR r,i: rational;
BEGIN
r := addRat(mulRat(x.rat,y.rat),
mulRat(mkRat(5,1),mulRat(x.irr,y.irr)));
i := addRat(mulRat(x.rat,y.irr),mulRat(x.irr,y.rat));
RETURN mkIrr(r,i)
END mulIrr;The representation includes rational values when the second coefficient is zero; the supplied type name does not require every stored value to be irrational.
Articles by others on the same topic
There are currently no matching articles.