Skip to content

Floating Point

Teens & adults Computing Number systems

Computers store decimals in binary fractions — 1/2, 1/4, 1/8… Some decimals have no exact binary form. 0.1 is like 1/3 in decimal: an infinite repeat (0.0001100110011…₂). The computer rounds it off — and the tiny error becomes visible:

0.1 + 0.2 = 0.30000000000000004 ← every computer on Earth

Float vs double

Rules for staying sane

In ALB ALB offers PURE/RATIONAL types that store exact fractions instead of binary approximations — 1/3 stays exactly 1/3. Where that isn't available, integer cents are the professional trick.
Practice

Why does 0.1 + 0.2 print 0.30000000000000004?

Store $9.99 safely as:

Compare two floats a and b with: