IEEE 754

The IEEE Standard for Floating-Point Arithmetic (IEEE 754) is a technical standard for floating-point arithmetic established in 1985 by the Institute of Electrical and Electronics Engineers (IEEE). The standard addressed many problems found in the diverse floating-point implementations that made them difficult to use reliably and portably. Many hardware floating-point units use the IEEE 754 standard.
The standard defines:
IEEE 754-2008, published in August 2008, includes nearly all of the original IEEE 754-1985 standard, plus the IEEE 854-1987 Standard for Radix-Independent Floating-Point Arithmetic. The current version, IEEE 754-2019, was published in July 2019. It is a minor revision of the previous version, incorporating mainly clarifications, defect fixes and new recommended operations.
Standard development.
The first standard for floating-point arithmetic, IEEE 754-1985, was published in 1985. It covered only binary floating-point arithmetic.
A new version, IEEE 754-2008, was published in August 2008, following a seven-year revision process, chaired by Dan Zuras and edited by Mike Cowlishaw. It replaced both IEEE 754-1985 (binary floating-point arithmetic) and IEEE 854-1987 Standard for Radix-Independent Floating-Point Arithmetic. The binary formats in the original standard are included in this new standard along with three new basic formats, one binary and two decimal. To conform to the current standard, an implementation must implement at least one of the basic formats as both an arithmetic format and an interchange format.
The international standard ISO/IEC/IEEE 60559:2011 (with content identical to IEEE 754-2008) has been approved for adoption through JTC1/SC 25 under the ISO/IEEE PSDO Agreement and published.
The current version, IEEE 754-2019 published in July 2019, is derived from and replaces IEEE 754-2008, following a revision process started in September 2015, chaired by David G. Hough and edited by Mike Cowlishaw. It incorporates mainly clarifications (e.g. "totalOrder") and defect fixes (e.g. "minNum"), but also includes some new recommended operations (e.g. "augmentedAddition").
The international standard ISO/IEC 60559:2020 (with content identical to IEEE 754-2019) has been approved for adoption through JTC1/SC 25 and published.
Formats.
An IEEE 754 "format" is a "set of representations of numerical values and symbols". A format may also include how the set is encoded.
A floating-point format is specified by:
A format comprises:
For example, if "b" = 10, "p" = 7, and "emax" = 96, then "emin" = −95, the significand satisfies 0 ≤ "c" ≤ , and the exponent satisfies −101 ≤ "q" ≤ 90. Consequently, the smallest non-zero positive number that can be represented is 1×10−101, and the largest is 9999999×1090 (9.999999×1096), so the full range of numbers is −9.999999×1096 through 9.999999×1096. The numbers −"b"1−"emax" and "b"1−"emax" (here, −1×10−95 and 1×10−95) are the smallest (in magnitude) "normal numbers"; non-zero numbers between these smallest numbers are called subnormal numbers.
Representation and encoding in memory.
Some numbers may have several possible exponential format representations. For instance, if "b" = 10, and "p" = 7, then −12.345 can be represented by −12345×10−3, −123450×10−4, and −1234500×10−5. However, for most operations, such as arithmetic operations, the result (value) does not depend on the representation of the inputs.
For the decimal formats, any representation is valid, and the set of these representations is called a "cohort". When a result can have several representations, the standard specifies which member of the cohort is chosen.
For the binary formats, the representation is made unique by choosing the smallest representable exponent allowing the value to be represented exactly. Further, the exponent is not represented directly, but a bias is added so that the smallest representable exponent is represented as 1, with 0 used for subnormal numbers. For numbers with an exponent in the normal range (the exponent field being neither all ones nor all zeros), the leading bit of the significand will always be 1. Consequently, a leading 1 can be implied rather than explicitly present in the memory encoding, and under the standard the explicitly represented part of the significand will lie between 0 and 1. This rule is called "leading bit convention", "implicit bit convention", or "hidden bit convention". This rule allows the binary format to have an extra bit of precision. The leading bit convention cannot be used for the subnormal numbers as they have an exponent outside the normal exponent range and scale by the smallest represented exponent as used for the smallest normal numbers.
Due to the possibility of multiple encodings (at least in formats called "interchange formats"), a NaN may carry other information: a sign bit (which has no meaning, but may be used by some operations) and a "payload", which is intended for diagnostic information indicating the source of the NaN (but the payload may have other uses, such as "NaN-boxing").
Basic and interchange formats.
The standard defines five basic formats that are named for their numeric base and the number of bits used in their interchange encoding. There are three binary floating-point basic formats (encoded with 32, 64 or 128 bits) and two decimal floating-point basic formats (encoded with 64 or 128 bits). The binary32 and binary64 formats are the "single" and "double" formats of IEEE 754-1985 respectively. A conforming implementation must fully implement at least one of the basic formats.
The standard also defines "interchange formats", which generalize these basic formats. For the binary formats, the leading bit convention is required. The following table summarizes the smallest interchange formats (including the basic ones).
Note that in the table above, the minimum exponents listed are for normal numbers; the special subnormal number representation allows even smaller numbers to be represented (with some loss of precision). For example, the smallest positive number that can be represented in binary64 is 2−1074; contributions to the −1074 figure include the E min value −1022 and all but one of the 53 significand bits (2−1022 − (53 − 1) = 2−1074).
Decimal digits is "digits" × log10 "base". This gives an approximate precision in number of decimal digits.
Decimal E max is "Emax" × log10 "base". This gives an approximate value of the maximum decimal exponent.
The binary32 (single) and binary64 (double) formats are two of the most common formats used today. The figure below shows the absolute precision for both formats over a range of values. This figure can be used to select an appropriate format given the expected value of a number and the required precision.
An example of a layout for 32-bit floating point is
and the 64 bit layout is similar.
Extended and extendable precision formats.
The standard specifies optional extended and extendable precision formats, which provide greater precision than the basic formats. An extended precision format extends a basic format by using more precision and more exponent range. An extendable precision format allows the user to specify the precision and exponent range. An implementation may use whatever internal representation it chooses for such formats; all that needs to be defined are its parameters ("b", "p", and "emax"). These parameters uniquely describe the set of finite numbers (combinations of sign, significand, and exponent for the given radix) that it can represent.
The standard recommends that language standards provide a method of specifying "p" and "emax" for each supported base "b". The standard recommends that language standards and implementations support an extended format which has a greater precision than the largest basic format supported for each radix "b". For an extended format with a precision between two basic formats the exponent range must be as great as that of the next wider basic format. So for instance a 64-bit extended precision binary number must have an 'emax' of at least 16383. The x87 80-bit extended format meets this requirement.
Interchange formats.
Interchange formats are intended for the exchange of floating-point data using a bit string of fixed length for a given format. 
Binary.
For the exchange of binary floating-point numbers, interchange formats of length 16 bits, 32 bits, 64 bits, and any multiple of 32 bits ≥ 128 are defined. The 16-bit format is intended for the exchange or storage of small numbers (e.g., for graphics).
The encoding scheme for these binary interchange formats is the same as that of IEEE 754-1985: a sign bit, followed by "w" exponent bits that describe the exponent offset by a "bias", and "p" − 1 bits that describe the significand. The width of the exponent field for a "k"-bit format is computed as "w" = round(4 log2("k")) − 13. The existing 64- and 128-bit formats follow this rule, but the 16- and 32-bit formats have more exponent bits (5 and 8 respectively) than this formula would provide (3 and 7 respectively).
As with IEEE 754-1985, the biased-exponent field is filled with all 1 bits to indicate either infinity (trailing significand field = 0) or a NaN (trailing significand field ≠ 0). For NaNs, quiet NaNs and signaling NaNs are distinguished by using the most significant bit of the trailing significand field exclusively, and the payload is carried in the remaining bits.
Decimal.
For the exchange of decimal floating-point numbers, interchange formats of any multiple of 32 bits are defined. As with binary interchange, the encoding scheme for the decimal interchange formats encodes the sign, exponent, and significand. Two different bit-level encodings are defined, and interchange is complicated by the fact that some external indicator of the encoding in use may be required.
The two options allow the significand to be encoded as a compressed sequence of decimal digits using densely packed decimal or, alternatively, as a binary integer. The former is more convenient for direct hardware implementation of the standard, while the latter is more suited to software emulation on a binary computer. In either case, the set of numbers (combinations of sign, significand, and exponent) that may be encoded is identical, and special values (±zero with the minimum exponent, ±infinity, quiet NaNs, and signaling NaNs) have identical encodings.
Rounding rules.
The standard defines five rounding rules. The first two rules round to a nearest value; the others are called "directed roundings":
Directed roundings.
Unless specified otherwise, the floating-point result of an operation is determined by applying the rounding function on the infinitely precise (mathematical) result. Such an operation is said to be "correctly rounded". This requirement is called "correct rounding".
Required operations.
Required operations for a supported arithmetic format (including the basic formats) include:
Comparison predicates.
The standard provides comparison predicates to compare one floating-point datum to another in the supported arithmetic format. Any comparison with a NaN is treated as unordered. −0 and +0 compare as equal.
Total-ordering predicate.
The standard provides a predicate "totalOrder", which defines a total ordering on canonical members of the supported arithmetic format. The predicate agrees with the comparison predicates when one floating-point number is less than the other. The "totalOrder" predicate does not impose a total ordering on all encodings in a format. In particular, it does not distinguish among different encodings of the same floating-point representation, as when one or both encodings are non-canonical. IEEE 754-2019 incorporates clarifications of "totalOrder".
Exception handling.
The standard defines five exceptions, each of which returns a default value and has a corresponding status flag that is raised when the exception occurs. No other exception handling is required, but additional non-default alternatives are recommended (see ).
The five possible exceptions are:
These are the same five exceptions as were defined in IEEE 754-1985, but the "division by zero" exception has been extended to operations other than the division.
For decimal floating point, there are additional exceptions:
Additionally, operations like quantize when either operand is infinite, or when the result does not fit the destination format, will also signal invalid operation exception.
Recommendations.
Alternate exception handling.
The standard recommends optional exception handling in various forms, including presubstitution of user-defined default values, and traps (exceptions that change the flow of control in some way) and other exception handling models that interrupt the flow, such as try/catch. The traps and other exception mechanisms remain optional, as they were in IEEE 754-1985.
Recommended operations.
Clause 9 in the standard recommends additional mathematical operations that language standards should define. None are required in order to conform to the standard.
Recommended arithmetic operations, which must round correctly:
The , and functions were not part of the IEEE 754-2008 standard because they were deemed less necessary. , were mentioned, but this was regarded as an error. All three were added in the 2019 revision.
The recommended operations also include setting and accessing dynamic mode rounding direction, and implementation-defined vector reduction operations such as sum, scaled product, and dot product, whose accuracy is unspecified by the standard.
, "augmented arithmetic operations" for the binary formats are also recommended. These operations, specified for addition, subtraction and multiplication, produce a pair of values consisting of a result correctly rounded to nearest in the format and the error term, which is representable exactly in the format. At the time of publication of the standard, no hardware implementations are known, but very similar operations were already implemented in software using well-known algorithms. The history and motivation for their standardization are explained in a background document.
As of 2019, the formerly required "minNum, maxNum, minNumMag, and maxNumMag" in IEEE 754-2008 are now deleted due to their non-associativity. Instead, two sets of new "minimum and maximum operations" are recommended. The first set contains "minimum, minimumNumber, maximum and maximumNumber". The second set contains "minimumMagnitude, minimumMagnitudeNumber, maximumMagnitude and maximumMagnitudeNumber". The history and motivation for this change are explained in a background document.
Expression evaluation.
The standard recommends how language standards should specify the semantics of sequences of operations, and points out the subtleties of literal meanings and optimizations that change the value of a result. By contrast, the previous 1985 version of the standard left aspects of the language interface unspecified, which led to inconsistent behavior between compilers, or different optimization levels in a single compiler.
Programming languages should allow a user to specify a minimum precision for intermediate calculations of expressions for each radix. This is referred to as "preferredWidth" in the standard, and it should be possible to set this on a per block basis. Intermediate calculations within expressions should be calculated, and any temporaries saved, using the maximum of the width of the operands and the preferred width, if set. Thus, for instance, a compiler targeting x87 floating-point hardware should have a means of specifying that intermediate calculations must use the double-extended format. The stored value of a variable must always be used when evaluating subsequent expressions, rather than any precursor from before rounding and assigning to the variable.
Reproducibility.
The IEEE 754-1985 allowed many variations in implementations (such as the encoding of some values and the detection of certain exceptions). IEEE 754-2008 has strengthened up many of these, but a few variations still remain (especially for binary formats). The reproducibility clause recommends that language standards should provide a means to write reproducible programs (i.e., programs that will produce the same result in all implementations of a language) and describes what needs to be done to achieve reproducible results.
Character representation.
The standard requires operations to convert between basic formats and "external character sequence" formats. Conversions to and from a decimal character format are required for all formats. Conversion to an external character sequence must be such that conversion back using rounding to even will recover the original number. There is no requirement to preserve the payload of a quiet NaN or signaling NaN, and conversion from the external character sequence may turn a signaling NaN into a quiet NaN.
The original binary value will be preserved by converting to decimal and back again using:
For other binary formats, the required number of decimal digits is
where "p" is the number of significant bits in the binary format, e.g. 237 bits for binary256.
When using a decimal floating-point format, the decimal representation will be preserved using:
Algorithms, with code, for correctly rounded conversion from binary to decimal and decimal to binary are discussed by Gay, and for testing by Paxson and Kahan.