Definition
Binary floating-point is a way to store an approximation of a real number in a fixed number of bits: a sign, an exponent and a significand, a number with a fixed count of binary digits. The value is plus or minus the significand times 2 to the power of the exponent. The IEEE 754 standard defines the common formats, binary32 (C’s usual float) and binary64 (double), reserves bit patterns for zeros, infinities and NaN (not a number), and requires each arithmetic result to be rounded by a stated rule [271].
The idea
A 32-bit signed integer covers about in steps of 1. Science and engineering need both and . Floating-point uses scientific notation in base 2: a fixed number of significant binary digits, and an exponent that moves the binary point. Every value then carries about the same relative precision, and the gaps between neighboring values grow with magnitude: they double at each power of two.
The set of representable values is finite, so almost every real number falls between two of them and must be rounded. Even 0.1 cannot be stored exactly. A fraction has a finite binary expansion only if its denominator, in lowest terms, is a power of two, and has the factor 5 in its denominator. The binary64 value nearest to 0.1 is exactly 0.1000000000000000055511151231257827021181583404541015625, and evaluates to 0.30000000000000004 in binary64. Rounding is part of the definition of the arithmetic, not a malfunction.
A ruler whose marks double their spacing at each power of two is the picture: fine near zero, coarse far away, with as many marks between 1 and 2 as between 1024 and 2048. The picture stops at the special values. Signed zeros, infinities and NaN are not marks on the ruler.
How it works
Fields. A binary32 value has 32 bits in three fields, starting from the most significant bit:
the sign , 1 bit: 0 for positive, 1 for negative;
the exponent field , 8 bits, read as an unsigned integer from 0 to 255;
the fraction field , 23 bits, read as an unsigned integer from 0 to .
The binary64 format has the same structure with 1, 11 and 52 bits.
Classes. The exponent field decides how to read the other two fields.
: a normal number, with value . The exponent is stored with a bias: the true exponent is , so fields from 1 to 254 give exponents from to without a separate sign. The leading 1 of the significand is not stored, because every normal significand starts with 1; this hidden bit gives 24 significant bits from 23 stored ones.
and : zero, or according to the sign.
and : a subnormal number, with value . The leading bit is 0 and the scale stays at the smallest normal exponent, so subnormals fill the gap between the smallest normal number and zero with evenly spaced values.
and : or .
and : NaN, the result of an operation that has no meaningful value, such as or . The standard recommends, and most processors follow, that a NaN whose highest fraction bit is 1 is quiet: it passes silently through later operations. A signaling NaN, with that bit 0, raises the invalid-operation exception when an operation uses it.
The binary64 format uses bias 1023 and exponent fields from 1 to 2046 for normal numbers, and has 53 significant bits.
Rounding. IEEE 754 requires addition, subtraction, multiplication, division, square root and fused multiply-add ( with a single rounding) to be correctly rounded: the result is the exact mathematical result, rounded once to a representable value. The default rule is round to nearest, ties to even: take the nearest representable value, and on an exact tie take the one whose last significand bit is 0. The other rules round toward zero, toward and toward .
Spacing. For a normal binary32 number with , neighboring representable values are apart. This gap is the ULP (unit in the last place) of : the value of the last significand bit. Round to nearest is never wrong by more than half an ULP.
Exceptions. IEEE 754 names five exceptional events: invalid operation (for example , which gives NaN), division by zero ( gives ), overflow (a result too large for the format, which becomes under round to nearest but the largest finite value under round toward zero), underflow (a result too small to be normal and not exact) and inexact (the result was rounded). By default each event sets a status flag and the operation returns its default result; the program does not stop.
Comparison. NaN is unordered: every comparison , , , , that involves a NaN is false, and is true, even when a NaN is compared with itself. The two zeros compare equal, yet and .
Worked example
Encoding 1.5. In binary, 1.5 is , that is , times . So . The true exponent 0 is stored as , binary 01111111. The bits after the leading 1 are a 1 followed by 22 zeros, so . Check: , and . The 32 bits are 0 01111111 10000000000000000000000; grouped in fours, 0011 1111 1100 0000 followed by sixteen zeros, they read 0x3FC00000. Byte order §4.21 changes only how those four bytes are laid out in memory: 00 00 C0 3F on a little-endian machine.
Extreme values of binary32. The largest finite value has and every fraction bit 1: . The smallest positive normal value has and : . The smallest positive subnormal value has and : . Subnormals are apart, and such steps reach , exactly the smallest normal value, so the spacing continues without a jump.
Rounding an integer. In binary32 every integer up to is exact. From to the ULP is , so only even integers exist there. The integer 16,777,217 lies exactly halfway between 16,777,216 and 16,777,218. Both are even integers, so evenness of the integer cannot break the tie. The rule looks at the last significand bit instead: 16,777,216 is , with last bit 0, and 16,777,218 is , with last bit 1. The result is 16,777,216. By the same rule, 16,777,219 rounds to 16,777,220, whose last bit is 0. A binary32 counter that adds 1 at a time therefore stops at 16,777,216.
Order of operations. In binary64 the ULP from to is 2. Take , and . For : the exact sum is a tie between and ; ties to even gives , and adding gives 0. For : is exact, because its magnitude is below , and adding gives 1. Real addition is associative; rounded addition is not.
Stated precisely
For binary32, with , and the field values defined above, the value of a bit pattern is For binary64, replace 23 by 52, 127 by 1023, 254 by 2046, by and 255 by 2047.
Let be the number of significant bits (24 for binary32, 53 for binary64) and the unit roundoff. Every integer with is representable. If and are representable, is one of , , , , the rule is round to nearest, and the exact result lies in the normal range, then the computed result is where denotes the rounded floating-point operation and is the relative rounding error. For binary32, ; for binary64, . Machine epsilon, the gap between 1 and the next larger representable value, is : and .
The bound rests on these assumptions: the hardware or library rounds correctly; the result neither overflows nor underflows; the rounding rule is round to nearest; and the compiler does not change the operation, for example by computing in a wider format, by reordering operations, or by fusing a multiplication and an addition into one rounded step.
Limits and common mistakes
Decimal fractions are approximations. Testing a computed value with fails for this reason: is false in binary64. Money and other decimal quantities belong in integers that count the smallest unit, such as cents, or in a decimal type.
A range check written with ordinary comparisons lets NaN through. The test if (x < lo || x > hi) reject(); rejects nothing when is NaN, because both comparisons are false. Write the test so that NaN fails it, as in if (!(x >= lo && x <= hi)) reject();, or test isfinite(x) first.
Integers lose exactness above in binary32 and above in binary64. An identifier such as 9,007,199,254,740,993 read into a binary64 number becomes 9,007,199,254,740,992. JSON §12.26 (JavaScript Object Notation) parsers in JavaScript, whose ordinary number type is binary64, do exactly this.
In C, converting a NaN, an infinity, or a value outside the target type’s range to an integer type is undefined behavior §4.12.
Hardware and compiler settings can change the arithmetic. Some processors offer a flush-to-zero mode that replaces subnormal results with zero for speed, and on some processors subnormal arithmetic is much slower than normal arithmetic, which makes timing depend on the data. Compiler options §12.3 such as -ffast-math let the compiler assume that no NaNs or infinities occur and reorder operations.
Printing is a separate conversion. A value printed with few digits may hide the difference between two stored values, and printing more digits shows the stored binary value, never the decimal text that was originally parsed.
Questions answered
Does every 32-bit pattern mean a finite real number?
No. In the IEEE 754 binary32 format, the 32 bits are a sign bit, an 8-bit exponent field and a 23-bit fraction field, and the exponent field value 255 (all ones) is reserved for values that are not finite. With a zero fraction it means or ; with any nonzero fraction it means NaN (not a number). Exponent field 255 occurs in patterns, of which 2 are infinities and are NaNs. The other patterns are finite: normal numbers, subnormal numbers (exponent field 0 and a nonzero fraction), and two zeros, and , which are different patterns for the same real number. Code that reads four bytes as a binary32 value must therefore check that the value is finite, for example with isfinite, before treating it as a quantity.
Why can normal binary32 have 24 significant bits with only 23 fraction bits stored?
Because the leading bit of every normal significand is 1, the format does not store it. A normal binary32 number is , where is the sign bit, the 8-bit exponent field, and means a binary 1 followed by the 23 stored fraction bits after the binary point. The exponent is always chosen so that the first significant bit sits just before the binary point, which makes that bit 1 for every normal number; storing it would waste a bit. For example, 1.5 is : the stored fraction is a 1 followed by 22 zeros, and the hidden 1 is added back when the value is read. Subnormal numbers, which have exponent field 0, have a hidden bit of 0 instead, so they have fewer significant bits, down to one for the smallest.
Why does ties-to-even mean a retained bit rule rather than an even decimal integer?
Because even refers to the last significand bit that the rounded result keeps, not to whether the number is an even integer. When an exact result lies exactly halfway between two representable values, round to nearest, ties to even picks the one whose last significand bit is 0. The two candidates are often both even integers, so integer evenness could not decide. In binary32, from to only even integers are representable. The integer 16,777,217 is halfway between 16,777,216 and 16,777,218, which are both even. The first is , with last significand bit 0; the second is , with last bit 1. The rule picks 16,777,216. Deciding by the last bit sends ties up and down about equally often, so rounding errors do not drift in one direction over many operations.
Can more printed digits or a repeated sample recover precision already lost in parsing?
No. Parsing decimal text into a binary floating-point number rounds it to the nearest representable value, and every later step sees only that stored value. Different texts can round to the same value: in binary64, 9007199254740993 and 9007199254740992 both become . Printing more digits shows the stored binary value more exactly, never the original text. Repeating the parse gives the same value every time, because rounding is deterministic, and the average of identical values is that same value. Averaging many different noisy measurements can reduce random measurement error, but that is a statement about noise in the measurements, not a way to undo rounding. To keep the exact input, keep the text, or use a representation that holds it exactly, such as an integer or a decimal type.
Is a NaN check interchangeable with checking a finite permitted range?
No. A NaN (not a number) check rejects only NaN, so , and finite values outside the permitted range still pass it. A range check that rejects when x < lo || x > hi is true catches out-of-range finite values and both infinities, but lets NaN through, because every ordered comparison with a NaN is false. Each test alone is incomplete. A single test that rejects all three cases rejects when !(x >= lo && x <= hi) is true: that expression is true for NaN, for both infinities and for finite values outside the range. Where it matters, first classify the value with isfinite, then check the range, and decide separately what the program does with each class.
Related entries: Signed versus unsigned comparison (§2.12), Endianness (§4.21), JSON (§12.26), Counter quantization (§11.35). Further reading: [187, 190, 191, 271].