Computer Algebra & Symbolic Computation

exact rational arithmetic

Add 1/3 and 1/6 on a calculator and you get 0.5, a number that happens to come out clean — but add 1/3 and 1/7 and you get 0.47619047..., a decimal that is already a little bit wrong. Exact rational arithmetic refuses to round: it keeps every number as a fraction, a numerator over a denominator, and computes 1/3 + 1/7 as exactly 10/21. The answer is a pair of integers, perfectly equal to the true value, no decimal in sight.

A rational number is stored as two arbitrary-precision integers, p/q, kept in lowest terms with q positive. The arithmetic is the rules you learned for fractions, done exactly: a/b + c/d = (a d + b c) / (b d), and a/b times c/d = (a c) / (b d), after which the result is reduced by dividing top and bottom by their greatest common divisor (computed with the Euclidean algorithm). Because the integers can be arbitrarily large, no information is ever lost: a chain of additions, multiplications, and divisions of fractions yields the one exactly correct fraction at the end. This is the number system at the heart of exact linear algebra, where fraction-free or rational Gaussian elimination solves A x = b with a perfectly exact answer.

Exact rationals matter when correctness must be airtight — verifying an identity, solving a linear system exactly, or any setting where catastrophic cancellation would wreck floating-point. The honest cost is twofold and central to computer algebra: the numerators and denominators grow as you compute, sometimes explosively (a textbook case of expression swell), and the constant GCD reductions are not free. So exact rational arithmetic is precise but does not scale; for large systems numerical methods, despite their round-off, are usually the only practical choice.

Compute 1/3 + 1/7 exactly. Common denominator 21: 1/3 = 7/21, 1/7 = 3/21, sum = 10/21. The GCD of 10 and 21 is 1, so 10/21 is already in lowest terms. The floating-point answer 0.47619047619047616 is close but not equal — its difference from 10/21 is what cancellation later amplifies.

Reduce by the GCD to lowest terms; the result is exactly the true fraction.

Keeping fractions in lowest terms is not optional housekeeping — skip the GCD reductions and the numerators and denominators bloat far faster, turning a manageable computation into an unmanageable one. Even reduced, exact rationals can still swell badly during a long calculation.

Also called
exact fraction arithmeticrational number arithmetic精確分數運算有理數算術