Learning path

Full curriculum

Full curriculum

Arrows go from each prerequisite to the units that depend on it. Hover or focus a unit to highlight its path.

Unit content

Decimal and fixed-point arithmetic

Binary floating point is useful when wide numerical range matters, but some applications need quantities such as decimal prices to have exact representations.

Fixed-point representation

A fixed-point value stores an integer together with an agreed scale. If money is stored in cents,

12345 ↔ 123.45

then addition and subtraction of same-scale amounts use exact integer arithmetic.

The scale is part of the representation: storing cents cannot exactly represent fractions smaller than one cent.

Decimal arithmetic

Decimal numeric types represent values using decimal digits and a decimal scale or exponent. Fractions such as 0.1 can therefore be represented exactly when they fit the chosen precision and scale.

This does not make every calculation exact. Division can still produce more digits than the representation can hold.

Rounding is a rule

When a result has too many decimal places, software needs an explicit rounding rule and point at which that rule is applied. Different policies can produce different totals, especially when many rounded values are combined.

Choosing a representation

Use binary floating point when approximation is acceptable and its range and performance are useful. Use fixed-point or decimal representations when the domain requires exact decimal quantities and explicit rounding.

The representation should follow the domain's rules rather than treating every non-integer number as interchangeable.