liveBook · Manning

Normalization

As explained in Modern C, Third Edition

Normalization is the process of choosing a canonical representation of a rational number by reducing common factors from its numerator and denominator.

Equivalent fractions can have different representations: 6/15 and 2/5 denote the same rational number. Normalization divides the numerator and denominator by their greatest common divisor, giving a representation with fewer factors that can also help avoid overflow during arithmetic. section 10.2.2

 rat rat_get_normal(rat x) [[__unsequenced__]] {
   size_t c = gcd(x.num, x.denom);
   x.num /= c;
   x.denom /= c;
   return x;
 }

rat_get_normal computes the greatest common divisor and divides both fields by it. section 10.2.2

Rational multiplication and addition can first compute a result representation, then normalize it before returning it. section 10.2.2

How Modern C, Third Edition covers it

How deep it goes
Implements reduction by computing a greatest common divisor and dividing the numerator and denominator.
Examples
Uses 6/15 and 2/5 as equivalent representations and normalizes multiplication and addition results.
What you can do after
Apply it via rat_get_normal to reduce a rational number's numerator and denominator by their greatest common divisor.
How it connects
Connects rational numbers, greatest common divisors, and rational multiplication and addition.

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, Third Edition. The section links point to the parts of the book it draws on.