Floating-point numbers: why tiny rounding happens

Concepts explained

A floating-point number stores a number using a limited set of binary digits and a scale. The scale lets the decimal point effectively move, so the same kind of value can represent small fractions and large amounts.

Think about writing one third as 0.333. The short version is useful, but it cannot include every digit. Computers have a similar problem with some fractions when using binary.

A surprising comparison

# language: en
say 0.1 + 0.2
say 0.1 + 0.2 == 0.3
say 10 + 20 == 30

The outputs are 0.30000000000000004, false, and true. Pliro uses binary64, the 64-bit floating-point number format specified by IEEE 754. The number reference describes its rules. The stored approximations of some fractions cause the small difference.

A finite number is an ordinary bounded number, rather than infinity or an invalid numeric result. NaN means “Not a Number,” a marker some systems use for invalid calculations. Pliro does not accept NaN or infinity as successful Number values.

Choose useful units

For a classroom shop game, store whole cents: 10 plus 20 gives 30 cents. This avoids fractional rounding in that small calculation. Very large whole numbers also have precision limits, so this is not a promise of unlimited exact arithmetic.

For positions and measurements, choose a sensible allowed difference when testing approximate results. Do not assume every decimal calculation is exact.

Numbers · Testing