Finding something worth knowing…

Science

Why your computer cannot store one fifth exactly

Most computers count in base two, and in binary the fraction 1/5 never ends, just as 1/3 never ends in decimal. So every time software stores 0.2, it quietly keeps a close neighbour instead. Floating-point arithmetic is the clever, slightly imperfect system that makes this rounding workable at enormous speed.

A floating-point number is basically scientific notation for machines. It holds a fixed-length string of digits, called the significand, plus an exponent saying where the point goes. Because that point can float left or right, the same few digits can describe distances between galaxies or between protons. The catch is uneven spacing: the gap between neighbouring representable values grows as numbers get larger.

Precision is set by the length of the significand. With five decimal digits, 12.345 fits, but 12.3456 needs six and gets rounded to 12.346, and adding 12.345 to 1.0001 might give 13.345 instead of 13.3451. Every such number is a ratio of two whole numbers, and which fractions come out exact depends on the base. Base three handles 1/3 effortlessly, while decimal manages 1/5 but not 1/3.

In the common 32-bit single-precision format, the significand is 24 bits long. Storing pi means taking its first 33 bits and rounding at the 24th, which lands at about 3.1415927. Binary formats pull a neat trick: after normalisation the leading digit of any non-zero number must be 1, so it need not be stored at all, handing the format one extra bit of precision for free.

Different computers once used different schemes, some even in bases 16, 8 or 256. The IEEE 754 standard of 1985 brought order, and its formats have dominated since the 1990s. Dedicated floating-point units now handle the work in hardware, and speed is measured in FLOPS. Alternatives exist, such as cheap fixed-point arithmetic for embedded chips and banking software, or logarithmic systems that make multiplication easy but addition awkward.

Source: Floating-point arithmetic

Related

More in Science · All topics