Floating-point arithmetic
Floating-point arithmetic is by far the most common way computers calculate with real numbers, such as 3.14159 or 0.000002.[1] Each number is stored as a significand (a fixed number of digits) times a base raised to a power, much as scientific notation writes . The power can change, so the position of the point "floats" and one format can hold both very large and very small numbers.[1]
Only a limited set of numbers fits in a fixed number of bits, so most results must be rounded to a nearby number in that set. The rounded result is usually close to the true value but not exactly equal to it.[1] Since at least 2000, almost all computers have used the binary arithmetic of the IEEE 754 standard, which fixes exactly how the basic operations must round.[1][2]
Representing numbers
Significand, base, and exponent
A floating-point format is set by four integers:[3]
- the base , almost always 2;
- the precision , the number of digits in the significand;
- the smallest exponent ;
- the largest exponent .
A number in the format has the form
Here each digit is an integer with , the digit string is the significand, and the exponent is an integer with .[1][4] IEEE 754 sets for all its formats.[4]
A number may fit exactly in one base but not in another. The number 0.1 is a good example, because 1/10 has an endless repeating expansion in binary:[1]
| Base | Precision | 0.1 stored as | Exact? |
|---|---|---|---|
| 10 | 3 | Yes | |
| 2 | 24 | No, only approximate |
Normalized numbers and the hidden bit
A number is normalized when its leading digit is not zero, which makes its representation unique.[1] In base 2 the leading digit of a normalized number is always 1, so formats leave it out of storage and gain one bit of precision. This implied digit is called the hidden bit, which is why single precision stores 23 significand bits but has .[1]
Requiring normalization makes zero impossible to write, so zero gets a reserved encoding (see IEEE 754 formats).[1]
The hidden-bit idea appears to have been first published by I. Bennett Goldberg in 1967, although Donald Knuth credits it to Konrad Zuse.[1]
Spacing between numbers
Floating-point numbers are not evenly spaced. In binary, the gap between neighbors doubles at every power of 2, so the relative spacing stays roughly constant, varying by up to a factor of .[3]
The effect is large in real formats. In IEEE single precision the next number after 1 is about 1.0000001, a gap of about . The next number after is about away.[5]
Floating point versus fixed point
In fixed-point arithmetic, another form of computer arithmetic, the exponent never changes. The two approaches compare as follows:[3][1]
| Fixed point | Floating point | |
|---|---|---|
| Spacing of numbers | Equal everywhere | Gap doubles at each power of 2 |
| Range and accuracy | Scale factors must be chosen by hand to cover the range of values | Wide range at a roughly constant relative accuracy |
| Common uses | Special-purpose devices such as FPGAs, and embedded systems | By far the most widely used representation of real numbers |
Rounding
Rounding modes
When the exact result of an operation is not in the format, it must be rounded to the next larger or next smaller floating-point number.[6] IEEE 754-1985 defined four rounding modes:[5]
- Round to nearest, with ties going to the neighbor whose last bit is 0 ("ties to even"). This is the default.[1]
- Round toward (up).
- Round toward (down).
- Round toward zero, also called truncation or chopping.
The 2008 revision added a fifth mode, round to nearest with ties away from zero. Implementations of the decimal formats must provide it, but binary implementations need not.[4][7]
The two modes toward infinity support interval arithmetic, which traps the exact result between a value rounded down and a value rounded up.[1][6]
Ties to even avoids slow drift. Goldberg's decimal example uses and starts from 1.00, then repeatedly adds 0.555 and subtracts it again. Round-half-up, which sends every halfway case up to the next value, behaves differently from ties to even:[1]
| Rule for halfway cases | Values after repeated round trips | Keeps changing? |
|---|---|---|
| Round half up | 1.00, 1.01, 1.02, and so on up to 9.45 | Yes, until it reaches 9.45 |
| Ties to even | 1.00, 1.00, 1.00, ... | No |
Reiser and Knuth showed in 1975 that with ties to even such a sequence never changes after its first step.[1]
Measuring rounding error
Two measures of rounding error are common:
- The unit in the last place (ulp) of a number is the value of its last significand bit, which is the gap to its neighbor.[5]
- The relative error is the difference between the computed and exact values divided by the exact value.[1]
Round to nearest gives an error of at most half an ulp.[5] This bounds the relative error by the unit roundoff
for binary formats, or in base .[3][1] Suppose there is no overflow (a result too large for the format) and no underflow (a result too small to be stored as a normalized number). Then each correctly rounded basic operation satisfies
Here marks the computed result, op is , , or , and the same bound holds for square root.[3][6] Results smaller than the smallest normalized number (see Subnormal numbers) do not satisfy this bound.[5] Most error analysis in numerical analysis is built from it.[1]
Machine epsilon, , is the gap between 1 and the next larger number, so .[3]
Machine epsilon is in single precision and in double precision.[5]
Usage of the term varies. Goldberg, for example, uses "machine epsilon" for the bound called here.[1]
How the basic operations work
Addition and subtraction
An operand is a number used as input to an operation. Adding two floating-point operands takes three steps:[1]
- Shift the significand of the smaller number right until both exponents match.
- Add (or subtract) the aligned significands exactly.
- Round the result back to digits, adjusting the exponent if the leading digit moved.
Keeping every shifted-out digit is costly, but dropping them all can be disastrous. With and no extra digits, becomes instead of 0.17, wrong in every digit.[1] One extra digit, called a guard digit, makes this result exact and keeps the relative error of any sum or difference below .[1]
IBM thought guard digits important enough that in 1968 it added one to the double-precision format of the System/360 and updated the machines already in the field.[1]
Correct rounding
IEEE 754 requires addition, subtraction, multiplication, division, and square root to be correctly rounded. Each result must equal the exact answer rounded to the format, as if it had been computed to infinite precision first.[6] Two guard digits plus one extra bit are enough to get correctly rounded sums and differences at little extra cost, and a multiplier must form all digits of the product before rounding.[1]
Correct rounding makes results predictable and provable. For the operations the standard specifies, numerical results and exceptions (special conditions an operation reports, such as an invalid operation or overflow) are fixed by the input values, the sequence of operations, and the output formats.[8] This precise definition also makes it possible to prove error bounds for floating-point programs.[1]
The exponential, logarithm, and sine functions are not required to be correctly rounded. The reason is the table maker's dilemma: no one can say in advance how many digits must be computed to decide which way a value rounds.[1] The current standard recommends correctly rounded versions of these functions but does not require them.[6]
Fused multiply-add
A fused multiply-add (FMA) computes with one rounding instead of two. Its result therefore satisfies the same bound as a single operation.[6]
Hardware that has FMA usually runs it at the same speed as one addition or multiplication.[6]
A compiler may or may not use FMA, so the same program can give different results on different systems, or even on the same system.[1]
IEEE 754 formats
Bit layout
A binary IEEE 754 number is stored as three fields: a sign bit, an exponent field, and a fraction holding the significand bits after the hidden bit.[5] The exponent field stores the true exponent plus a fixed offset called the exponent bias, so the field is never negative.[1] The two most common formats use these field sizes:[5][1]
| Format | Sign bits | Exponent bits | Fraction bits | Bias |
|---|---|---|---|---|
| Single precision | 1 | 8 | 23 | 127 |
| Double precision | 1 | 11 | 52 | 1023 |
A normal single-precision number with sign bit , exponent field , and fraction has the value
Here is the binary significand formed by placing the hidden 1 before the fraction bits, and runs from 1 to 254.[5] For example, 1.0 is stored as hexadecimal 3F800000, which is sign 0, exponent field 127, and fraction 0.[5]
Reserved exponent values
The smallest and largest exponent fields are kept for special values, including NaN ("not a number"), which marks an invalid or undefined result. For single precision the encodings are:[5][6]
| Exponent field | Fraction | Value |
|---|---|---|
| 1 to 254 | any | (normal number) |
| 0 | nonzero | (subnormal number) |
| 0 | zero | |
| 255 | zero | |
| 255 | nonzero | NaN |
Double precision follows the same pattern, with bias 1023, exponent field 2047 for infinities and NaNs, and for subnormal numbers.[5]
Common binary formats
IEEE 754-2019 defines five basic formats: three binary formats of 32, 64, and 128 bits, and two decimal formats of 64 and 128 bits.[4] Single and double precision were in the 1985 standard. Half precision and quadruple precision were added in 2008, with half precision defined only as a storage format, though it is widely used for computation.[6]
The table also includes bfloat16, a 16-bit format outside IEEE 754 with 8 exponent bits.[9]
| Format | Bits (sign, exponent, fraction) | Precision | Largest finite value (about) | Unit roundoff (about) |
|---|---|---|---|---|
| binary16 (half) | 1, 5, 10 | 11 | ||
| bfloat16 (not IEEE) | 1, 8, 7 | 8 | ||
| binary32 (single) | 1, 8, 23 | 24 | ||
| binary64 (double) | 1, 11, 52 | 53 | ||
| binary128 (quadruple) | 1, 15, 112 | 113 |
The binary16, bfloat16, and binary32 rows come from Higham.[9] The binary64 and binary128 rows come from Sun's guide, with .[5]
Other formats
- Extended precision formats give intermediate results extra bits. IEEE 754-1985 required a double extended format to have at least 64 significand bits and at least 79 bits in total, and x86 processors store theirs in 80 bits with an explicit leading bit.[5] According to William Kahan, the Intel 8087 used 64 significand bits because that was the widest carry it could pass along without lengthening its clock cycle.[1]
- Decimal floating point uses , which suits calculators that show every result in decimal.[1] It began as the separate IEEE 854 standard in 1987 and joined IEEE 754 in 2008.[6] Decimal arithmetic in software, such as the decimal module of Python, is used for accounting.[2]
- Bfloat16 keeps the 8 exponent bits of single precision but only an 8-bit significand. It was designed for neural networks, which are more sensitive to the size of the exponent than to the precision of the significand.[10]
- TensorFloat-32 (TF32) is a mode of the tensor cores, specialized units that speed up matrix multiplications, in NVIDIA Ampere GPUs. It uses 1 sign bit, 8 exponent bits, and 10 fraction bits.[11] With the hidden bit its precision is , the same as half precision, in a 19-bit format.[12]
Special values and exceptions
IEEE arithmetic is a closed system: thanks to infinities and NaN, every operation returns a value in the system.[6]
Infinity
Infinities and overflow behave as follows:[1]
- Overflow under round to nearest: a result too large for the format becomes instead of the largest finite number.
- Positive overflow under round toward zero or toward : the result is the largest finite number instead.
- A nonzero number divided by zero: the result is , with the sign set by the usual rules.
Arithmetic with infinity follows limits, so and a finite number divided by is 0.[6] For example, evaluates to 1 when .[6]
Signed zero
IEEE formats have both and , called signed zero. The two compare as equal, but they behave differently in some operations: while , because the sign of the infinite result follows the signs of both operands in the usual way.[1]
Keeping the sign lets an underflowed negative number stay negative, so a function such as log can tell it apart from a true zero.[1]
NaN
A NaN is the result of invalid operations such as these:[6]
Any arithmetic operation with a NaN operand returns a NaN.[6] This lets a calculation continue, for example when a root finder tries a point outside the function's domain.[1]
A NaN never compares equal to anything, including itself, so is true only when is a NaN.[1]
Signaling NaNs, a second kind that raises the invalid-operation exception when used, entered the standard as a compromise during drafting.[13][1]
Subnormal numbers
Without special handling, there is a gap between 0 and the smallest normalized number, and results that fall into it are flushed to zero.[1] Subnormal numbers fill the gap. They have exponent and leading digit 0, and they share the spacing of the smallest normalized numbers.[3][1]
Returning subnormal numbers instead of zero is called gradual underflow.[5]
Gradual underflow keeps useful guarantees:
- If , then the computed is not zero.[1][5]
- An underflowed result never loses more accuracy than ordinary rounding error causes.[5]
Subnormal numbers also have costs:
- They have fewer significant bits than normal numbers.[5]
- Some hardware traps on them and handles them in software, so programs that underflow often can run noticeably slower.[1]
Before IEEE 754, flushing to zero was the default on most mainframes, the large central computers of that era.[5]
Exceptions and flags
IEEE 754 defines five exceptions. By default each one delivers a result and lets the program continue:[1][6]
| Exception | Typical cause | Default result |
|---|---|---|
| Invalid operation | , , | NaN |
| Division by zero | nonzero number divided by 0 | |
| Overflow | result too large for the format | or the largest finite number, depending on rounding mode |
| Underflow | result too small to be normal | a subnormal number or 0 |
| Inexact | result not exactly representable, such as | the rounded result |
Each exception also sets a status flag, a bit that records that the exception has occurred. Flags are "sticky": once set, they stay set until the program clears them.[1]
The 1985 standard also strongly recommended that systems let users install trap handlers that run when an exception occurs.[1]
Where floating point differs from real arithmetic
Decimal fractions are inexact
Most decimal fractions have no exact binary form.[2] In double precision, 0.1 is stored as , which equals 0.1000000000000000055511151231257827021181583404541015625.[2] As a result, adding 0.1 ten times in Python does not give exactly 1.0:[2]
>>> 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 1.0
False
To print a binary number in decimal and read it back unchanged, 9 significant digits are enough for single precision and 17 for double.[1][4]
Order of operations matters
Floating-point addition is commutative, meaning always holds, but it is not associative.[1] With , , and , the sum is 1 but is 0.[1] Algorithms that depend on a fixed order break if a compiler regroups terms.[1]
Cancellation
Subtracting two nearly equal numbers cancels their matching leading digits. Catastrophic cancellation happens when the operands already carry rounding errors: the subtraction itself is exact, but it exposes the earlier errors.[1] With , , , , and , the computed is 0.1, while the exact value is 0.0292.[1]
Benign cancellation happens when the operands are exact, and then the subtraction is accurate.[1] The Sterbenz lemma makes this precise. If , , and , then exactly.[14][1]
Testing for equality
Two expressions that are equal in algebra can produce different floating-point results, so exact equality tests often fail. Treating numbers as equal when they differ by less than a tolerance is not a full cure. It leaves open how to choose the tolerance, and the relation is not transitive: close to and close to does not make close to .[1]
Python provides math.isclose for such tolerance-based tests.[2]
Compilers and hidden precision
An optimizer, the part of a compiler that rewrites code to improve it, can break correct floating-point code if it applies the rules of real numbers. For example, it can simplify to zero the correction term of compensated summation, a method that tracks and corrects rounding errors in long sums.[1]
Languages such as C allow expressions to be evaluated in a format wider than their type. On x86 systems that compute in extended precision, q = 3.0/7.0 followed by the test q == 3.0/7.0 can come out false.[1]
A separate problem is double rounding. Rounding first to extended precision and then to double can give a different answer from a single rounding to double.[1]
Techniques for accurate results
Rewriting formulas
A formula with catastrophic cancellation can often be rearranged. For the quadratic formula with real roots (, ), the root
can be written either way whenever the denominator is not zero, which always holds when . The first form suffers cancellation when and , so the second form should be used. The other root has no such cancellation, and for the roles swap.[1]
Similarly, is more accurate when computed as , which replaces a catastrophic cancellation with a benign one.[1]
Extra and mixed precision
A short burst of higher precision often protects a whole calculation. Computing in double precision when , , and are single precision keeps every single-precision bit of the roots.[1]
Iterative refinement improves a computed solution of a linear system by repeating three steps:[1]
- Compute the residual , the difference between the computed and the target .
- Solve .
- Update .
The residual is a difference of nearby inexact numbers, so the method helps little unless is computed in higher precision.[1] Mixing precisions in this way is called mixed-precision arithmetic.
Compensated summation
The Kahan summation algorithm tracks the low-order bits lost in each addition and feeds them back into the next one:[1]
def kahan_sum(xs):
s = xs[0]
c = 0.0
for x in xs[1:]:
y = x - c # apply the correction from the last step
t = s + y # low-order bits of y are lost here
c = (t - s) - y # recover those lost bits
s = t
return s
The computed sum equals the exact sum of terms that are each changed by a relative amount of at most about . Plain left-to-right addition of terms can change terms by up to about .[1]
Error-free transformations
The rounding error of a single operation can often be computed exactly with a few more floating-point operations. In binary with correct rounding to nearest, if and no overflow occurs, then
This formula is due to Dekker (1971).[1] Related algorithms such as 2Sum are called error-free transformations. IEEE 754-2019 added augmented operations that return both the rounded result of , , or and its exact error.[6]
Bounding or removing the error
- Interval arithmetic uses the directed rounding modes to compute an interval certain to contain the exact result. Because such intervals often grow large, it works best together with multiple precision, redoing the calculation at higher precision until the interval is small enough.[1]
- Arbitrary-precision arithmetic stores a very long significand in an array of words, or represents a number as a list of ordinary floating-point numbers whose sum, not yet added together, is the value.[1]
History
Early machines
Konrad Zuse's Z3, completed in 1941, used 2,300 relays, performed binary floating-point arithmetic, and had a 22-bit word length.[15]
For decades afterward, each manufacturer had its own floating-point arithmetic. Kahan analyzed these commercial arithmetics and estimated how much their unexpected behavior raised the cost of reliable, portable numerical software.[13] Examples of the differences:
- IBM's System/370 used base 16, which can lose up to 3 bits of effective precision compared with base 2.[1]
- Some machines, such as Cray systems, had no guard digit.[1]
- Until the 1980s, almost all computers flushed underflows to zero.[13]
The IEEE 754 standard
The standard grew out of Intel's work on a floating-point coprocessor. Intel manager John Palmer persuaded the company it needed an arithmetic standard and brought in Kahan as a consultant.[13] The DEC VAX formats competed with Kahan's draft, and the main dispute was over gradual underflow.[13]
- 1976: Intel begins designing a floating-point coprocessor for its 8086/8088 processors, which became the 8087.[13]
- 1977: After an IEEE p754 meeting, Kahan, his student Jerome Coonen, and visiting professor Harold Stone write the "K-C-S" draft, which p754 later adopts.[13]
- 1981: At a p754 meeting in Boston, G. W. Stewart, whom DEC had asked to assess gradual underflow, reports that it is the right choice.[13]
- 1984: Intel, AMD, Apple, IBM, Motorola, and others have already implemented the draft.[13]
- 1985: IEEE 754-1985 is published.[6]
- 1987: The separate IEEE 854 standard for decimal arithmetic is published.[6]
- 1989: Kahan receives the Turing Award, in particular for his work on IEEE 754 and 854.[6]
- 2008: A revision merges decimal arithmetic into IEEE 754 and adds half and quadruple precision.[6]
- 2019: A revision adds augmented arithmetic operations.[6]
References
- What Every Computer Scientist Should Know About Floating-Point Arithmetic, David Goldberg, ACM Computing Surveys (edited reprint by Sun Microsystems), 1991.
- Floating-Point Arithmetic: Issues and Limitations, Python Software Foundation, 2025.
- What Is Floating-Point Arithmetic?, Nick Higham, 2020.
- IEEE Std 754-2019, IEEE Standard for Floating-Point Arithmetic, IEEE, 2019.
- IEEE Arithmetic (Numerical Computation Guide, Chapter 2), Sun Microsystems, 2000.
- What Is IEEE Standard Arithmetic?, Nick Higham, 2020.
- Intel IEEE 754-2008 Binary Floating-Point Conformance Library, Intel, 2025.
- IEEE 754-2019: IEEE Standard for Floating-Point Arithmetic, IEEE Standards Association, 2019.
- Half Precision Arithmetic: fp16 Versus bfloat16, Nick Higham, 2018.
- What Is Bfloat16 Arithmetic?, Nick Higham, 2020.
- Accelerating AI Training with NVIDIA TF32 Tensor Cores, NVIDIA, 2020.
- Numerical Behavior of NVIDIA Tensor Cores, Massimiliano Fasi, Nicholas J. Higham, Mantas Mikaitis, and Srikara Pranesh, PeerJ Computer Science, 2021.
- An Interview with the Old Man of Floating-Point, Charles Severance, 1998.
- On Automatically Proving the Correctness of math.h Implementations, Proceedings of the ACM on Programming Languages (POPL), 2018.
- 1941: Timeline of Computer History, Computer History Museum.
