liveBook · Manning

Normalization

As explained in Modern C

Normalization is the process of reducing a rational representation so its numerator and denominator have no unnecessary common factors.

A rational number can have several representations of the same value. Normalization reduces its numerator and denominator by their greatest common divisor, leaving no unnecessary common factors. section 10.2.2

12   rat rat_get_normal(rat x) {
13     size_t c = gcd(x.num, x.denom);
14     x.num /= c;
15     x.denom /= c;
16     return x;
17   }


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":0,\"ch\":0},{\"line\":0,\"ch\":2}],[{\"line\":1,\"ch\":0},{\"line\":1,\"ch\":2}],[{\"line\":1,\"ch\":7},{\"line\":1,\"ch\":13}],[{\"line\":2,\"ch\":0},{\"line\":2,\"ch\":2}],[{\"line\":3,\"ch\":0},{\"line\":3,\"ch\":2}],[{\"line\":4,\"ch\":0},{\"line\":4,\"ch\":2}],[{\"line\":4,\"ch\":7},{\"line\":4,\"ch\":13}],[{\"line\":5,\"ch\":0},{\"line\":5,\"ch\":2}]]"}
!@%STYLE%@!

The function divides both fields by their greatest common divisor and returns the reduced value. section 10.2.2

Normalize results after rational multiplication or addition. Besides making representations easier to read, this can help avoid overflow during arithmetic. section 10.2.2

How Modern C covers it

How deep it goes
Defines reduction and shows a gcd-based implementation, without elaborating on gcd itself.
Examples
Shows reducing both fields by their gcd and normalizing product and sum results.
What it assumes
Uses a gcd function to reduce the numerator and denominator.
What you can do after
Apply normalization with rat_get_normal to reduce a rational value’s numerator and denominator.
How it connects
Connects rational numbers, greatest common divisors, and rational arithmetic.

Also explained in

Compare how 21 books explain normalization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.