@CompSciFact: Floating point numbers are a leaky abstraction

X AI KOLs Timeline News

Summary

An article explaining why floating point numbers are a leaky abstraction, covering precision limits, subtraction issues, and overflow/underflow, with references to Goldberg's classic paper.

Floating point numbers are a leaky abstraction https://t.co/xDWVo6LsyJ
Original Article
View Cached Full Text

Cached at: 07/31/26, 06:50 AM

Floating point numbers are a leaky abstraction https://t.co/xDWVo6LsyJ


Floating point numbers: a leaky abstraction

Source: https://www.johndcook.com/blog/2009/04/06/numbers-are-a-leaky-abstraction/ Joel Spolsky coined the termleaky abstractionfor programming concepts that usually shield you from messy details but sometimes break down. A perfect abstraction is a black box that you never have to open. A leaky abstraction is a black box that you have to open up occasionally.

Floating point numbers, the computer representations of real numbers, are leaky abstractions. They work remarkably well: you can usually pretend that a floating point type is a mathematical real number. But sometimes you can’t. The abstraction leaks, though not very often.

Most explanations I’ve heard for the limitations of machine numbers are pedantic. “There are only a finite number of floating point numbers so they can’t represent real numbers well.” That’s not much help. It doesn’t explain why floating point numbers actuallydorepresent real numbers sufficiently well for most applications, and it doesn’t suggest where the abstraction might leak.

A standard floating point number has roughly 16 decimal places of precision and a maximum value on the order of 10308, a 1 followed by 308 zeros. (According to IEEE standard 754, the typical floating point implementation.)

Sixteen decimal places is a lot. Hardly any measured quantity is known to anywhere near that much precision. For example, the constant in Newton’s Law of Gravity is only known to six significant figures. The charge of an electron is known to 11 significant figures, much more precision than Newton’s gravitational constant, but still less than a floating point number.So when are 16 figures not enough? One problem area is subtraction. The other elementary operations — addition, multiplication, division — are very accurate. As long as you don’t overflow or underflow, these operations often produce results that are correct to the last bit. But subtraction can be anywhere from exact to completely inaccurate. If two numbers agree to n figures, you can lose up to n figures of precision in their subtraction. This problem can show up unexpectedly in the middle of other calculations. For an example, see this post oncalculating standard deviation.

What about overflow or underflow?When do you need numbers bigger than 10308? Often you don’t. But in probability calculations, for example, you need them all the time unless you’re clever. It’s common in probability to compute a medium-sized number that is the product of an astronomically large number and an infinitesimally small number. The final result fits into a computer just fine, but the intermediate numbers might not due to overflow or underflow. For example, the maximum floating point number on most computers is somewhere between 170 factorial and 171 factorial. Such large factorials often appear in applications, often in ratios with other large factorials.

Often you can afford to be blissfully ignorant of the details of floating point arithmetic, but sometimes you cannot. A great place to learn more is David Goldberg’s paperWhat Every Computer Scientist Should Know About Floating-Point Arithmetic.

Update: See follow-up post,Anatomy of a floating point number.

More on floating point computing

Similar Articles

Intermediate Floating-Point Precision

Lobsters Hottest

This article explores how intermediate floating-point precision in C++ code depends on compiler settings, CPU flags, and architecture, particularly on x87 FPU, and how this affects performance and calculation results.

Floats Don't Agree with Themselves

Hacker News Top

Developer releases `exact-poly`, a 2D geometry library using exact integer arithmetic instead of floats to eliminate cross-platform reproducibility issues caused by IEEE 754 implementation differences.