IEEE 754
The IEEE Standard for Floating-Point Arithmetic is a technical standard for floating-point arithmetic originally established in 1985 by the Institute of Electrical and Electronics Engineers. 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:
- arithmetic formats: sets of binary and decimal floating-point data, which consist of finite numbers, infinities, and special "not a number" values
- interchange formats: encodings that may be used to exchange floating-point data in an efficient and compact form
- rounding rules: properties to be satisfied when rounding numbers during arithmetic and conversions
- operations: arithmetic and other operations on arithmetic formats
- [|exception] handling: indications of exceptional conditions
History
The need for a floating-point standard arose from chaos in the business and scientific computing industry in the 1960s and 1970s. IBM used a hexadecimal floating-point format with 7 bits always used for the exponent regardless of precision. CDC and Cray computers used ones' complement representation, which admits a value of +0 and −0. CDC 60-bit computers did not have full 60-bit adders, so integer arithmetic was limited to 48 bits of precision from the floating-point unit. Exception processing from divide-by-zero was different on different computers. Moving data between systems and even repeating the same calculations on different systems was often difficult.The first IEEE 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 and IEEE 854-1987 standards. 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 has been approved for adoption through ISO/IEC JTC 1/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 and defect fixes, but also includes some new recommended operations.
The international standard ISO/IEC 60559:2020 has been approved for adoption through ISO/IEC JTC 1/SC 25 and published.
The next projected revision of the standard is in 2029.
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 base b, which is either 2 or 10 in IEEE 754;
- a precision p;
- an exponent range from emin to emax, with emin = 1 − emax, or equivalently emin = −, for all IEEE 754 formats.
- Finite numbers, which can be described by three integers: s = a sign, c = a significand having no more than p digits when written in base b, and q = an exponent such that emin ≤ q + p − 1 ≤ emax. The numerical value of such a finite number is. Moreover, there are two zero values, called signed zeros: the sign bit specifies whether a zero is +0 or −0.
- Two infinities: +∞ and −∞.
- Two kinds of NaN : a quiet NaN and a signaling NaN.
Representation and encoding in memory
Some numbers may have several possible floating-point 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 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 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, a NaN may carry other information: a sign bit and a payload, which is intended for diagnostic information indicating the source of the NaN.
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 and two decimal floating-point basic formats. 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 some of the possible interchange formats.
In the table above, integer values are exact, whereas values in decimal notation are rounded values. 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 emin value −1022 and all but one of the 53 significand bits.
Decimal digits is the precision of the format expressed in terms of an equivalent number of decimal digits. It is computed as digits × log10 base. E.g. binary128 has approximately the same precision as a 34 digit decimal number.
log10 MAXVAL is a measure of the range of the encoding. Its integer part is the largest exponent shown on the output of a value in scientific notation with one leading digit in the significand before the decimal point.
The binary32 and binary64 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. These parameters uniquely describe the set of finite numbers 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.
The original IEEE 754-1985 standard also had the concept of extended formats, but without any mandatory relation between emin and emax. For example, the Motorola 68881 80-bit format, where emin = − emax, was a conforming extended format, but it became non-conforming in the 2008 revision.
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.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 − 13. The existing 64- and 128-bit formats follow this rule, but the 16- and 32-bit formats have more exponent bits than this formula would provide.
As with IEEE 754-1985, the biased-exponent field is filled with all 1 bits to indicate either infinity or a NaN. 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 that may be encoded is identical, and special values 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:Roundings to nearest
- Round to nearest, ties to even – rounds to the nearest value; if the number falls midway, it is rounded to the nearest value with an even least significant digit.
- Round to nearest, ties away from zero – rounds to the nearest value; if the number falls midway, it is rounded to the nearest value above or below.
"Round to nearest, ties to even" is the default for binary floating point and the recommended default for decimal. "Round to nearest, ties to away" is only required for decimal implementations.
Directed roundings
- Round toward 0 – directed rounding towards zero.
- Round toward +∞ – directed rounding towards positive infinity.
- Round toward −∞ – directed rounding towards negative infinity.
Required operations
Required operations for a supported arithmetic format include:- Conversions to and from integer
- Previous and next consecutive values
- Arithmetic operations
- Conversions
- Scaling and quantizing
- Copying and manipulating the sign
- Comparisons and total ordering
- Classification of numbers and testing for NaNs
- Testing and setting status flags
Comparison predicates
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 main differences are:- NaN is sortable.
- * NaN is treated as if it had a larger absolute value than Infinity.
- * qNaN and sNaN are treated as if qNaN had a larger absolute value than sNaN.
- * NaN is then sorted according to the payload. In IEEE 754-2008, a NaN with a lesser payload is treated as having a lesser absolute value. In IEEE 754-2019, any implementation-defined ordering is acceptable.
- Negative zero is treated as smaller than positive zero.
- If both sides of the comparison refer to the same floating-point datum, the one with the lesser exponent is treated as having a lesser absolute value.
For the binary interchange formats whose encoding follows the IEEE 754-2008 recommendation on placement of the NaN signaling bit, the comparison is identical to one that type puns the floating-point numbers to a sign–magnitude integer, an old trick for FP comparison without an FPU.
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.The five possible exceptions are
- Invalid operation: mathematically undefined, e.g., the square root of a negative number. By default, returns qNaN.
- Division by zero: an operation on finite operands gives an exact infinite result, e.g., 1/0 or log. By default, returns ±infinity.
- Overflow: a finite result is too large to be represented accurately. By default, returns ±infinity for the round-to-nearest modes.
- Underflow: a result is very small. By default, returns a number less than or equal to the minimum positive normal number in magnitude ; a subnormal number always implies an underflow exception, but by default, if it is exact, no flag is raised.
- Inexact: the exact result is not representable exactly. By default, returns the correctly rounded result.
Some decimal floating-point implementations define additional exceptions, which are not part of IEEE 754:
- Clamped: a result's exponent is too large for the destination format. By default, trailing zeros will be added to the coefficient to reduce the exponent to the largest usable value. If this is not possible then an overflow exception occurs.
- Rounded: a result's coefficient requires more digits than the destination format provides. An inexact exception is signaled if any non-zero digits are discarded.
Special values
Signed zero
In the IEEE 754 standard, zero is signed, meaning that there exist both a "positive zero" and a "negative zero". In most run-time environments, positive zero is usually printed as "0" and the negative zero as "-0". The two values behave as equal in numerical comparisons, but some operations return different results for +0 and −0. For instance, returns negative infinity, while returns positive infinity. Other common functions with a discontinuity at which might treat +0 and −0 differently include [Gamma function|] and the principal square root of for any negative number y. As with any approximation scheme, operations involving "negative zero" can occasionally cause confusion. For example, in IEEE 754, does not always imply, as but. Moreover, the reciprocal square root of is while the mathematical function over the real numbers does not have any negative value.Subnormal numbers
Subnormal values fill the underflow gap with values where the absolute distance between them is the same as for adjacent values just outside the underflow gap. This is an improvement over the older practice to just have zero in the underflow gap, and where underflowing results were replaced by zero.Modern floating-point hardware usually handles subnormal values, and does not require software emulation for subnormals.
Infinities
The infinities of the extended real number line can be represented in IEEE floating-point datatypes, just like ordinary floating-point values like 1, 1.5, etc. They are not error values in any way, though they are often used as replacement values when there is an overflow. Upon a divide-by-zero exception, a positive or negative infinity is returned as an exact result. An infinity can also be introduced as a numeral.IEEE 754 requires infinities to be handled in a reasonable way, such as
- NaN – there is no meaningful thing to do
NaNs
The representation of NaNs specified by the standard has some unspecified bits that could be used to encode the type or source of error; but there is no standard for that encoding. In theory, signaling NaNs could be used by a runtime system to flag uninitialized variables, or extend the floating-point numbers with other special values without slowing down the computations with ordinary values, although such extensions are not common. A variant of this approach is used by some JavaScript runtimes and LuaJIT to store 64-bit pointer values and IEEE 754 double-precision floating-point values in the same data type, allowing runtimes to eliminate the overhead of extra memory allocations and indirections for floating-point values.
Design rationale
It is a common misconception that the more esoteric features of the IEEE 754 standard discussed here, such as extended formats, NaN, infinities, subnormals etc., are only of interest to numerical analysts, or for advanced numerical applications. In fact the opposite is true: these features are designed to give safe robust defaults for numerically unsophisticated programmers, in addition to supporting sophisticated numerical libraries by experts. The key designer of IEEE 754, William Kahan, notes that it is incorrect to "... features of IEEE Standard 754 for Binary Floating-Point Arithmetic that... not appreciated to be features usable by none but numerical experts. The facts are quite the opposite. In 1977 those features were designed into the Intel 8087 to serve the widest possible market... Error-analysis tells us how to design floating-point arithmetic, like IEEE Standard 754, moderately tolerant of well-meaning ignorance among programmers".- The special values such as infinity and NaN ensure that the floating-point arithmetic is algebraically complete: every floating-point operation produces a well-defined result and will not—by default—throw a machine interrupt or trap. Moreover, the choices of special values returned in exceptional cases were designed to give the correct answer in many cases. For instance, under IEEE 754 arithmetic, continued fractions such as will give the correct answer on all inputs, as the potential divide by zero, e.g. for, is correctly handled by giving +infinity, and so such exceptions can be safely ignored. As noted by Kahan, the unhandled trap consecutive to a floating-point to 16-bit integer conversion overflow that caused the loss of an Ariane 5 rocket would not have happened under the default IEEE 754 floating-point policy.
- Subnormal numbers ensure that for finite floating-point numbers x and y, x − y = 0 if and only if x = y, as expected, but which did not hold under earlier floating-point representations.
- On the design rationale of the x87 80-bit format, Kahan notes: "This Extended format is designed to be used, with negligible loss of speed, for all but the simplest arithmetic with float and double operands. For example, it should be used for scratch variables in loops that implement recurrences like polynomial evaluation, scalar products, partial and continued fractions. It often averts premature Over/Underflow or severe local cancellation that can spoil simple algorithms". Computing intermediate results in an extended format with high precision and extended exponent has precedents in the historical practice of scientific calculation and in the design of scientific calculators e.g. Hewlett-Packard's financial calculators performed arithmetic and financial functions to three more significant decimals than they stored or displayed. The implementation of extended precision enabled standard elementary function libraries to be readily developed that normally gave double precision results within one unit in the last place at high speed.
- Correct rounding of values to the nearest representable value avoids systematic biases in calculations and slows the growth of errors. Rounding ties to even removes the statistical bias that can occur in adding similar figures.
- Directed rounding was intended as an aid with checking error bounds, for instance in interval arithmetic. It is also used in the implementation of some functions.
- The mathematical basis of the operations, in particular correct rounding, allows one to prove mathematical properties and design floating-point algorithms such as 2Sum, Fast2Sum and Kahan summation algorithm, e.g. to improve accuracy or implement multiple-precision arithmetic subroutines relatively easily.
Recommendations
Alternate exception handling
The standard recommends optional exception handling in various forms, including presubstitution of user-defined default values, and traps 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.The following are recommended arithmetic operations, which must round correctly:
- [exp(x)|],,
- [exp(x)−1|],,
- [Natural logarithm|], [Binary logarithm|], [Common logarithm|]
- [ln(1+x)|],,
- [Hypot|]
- [Reciprocal square root|]
- for
- [nth root|]
- [Exponentiation#Integer exponents|], [Exponentiation|]
- [sin (trigonometry)|], [cos (trigonometry)|], [tan (trigonometry)|]
- [arcsin (trigonometry)|], [arccos (trigonometry)|], [arctan (trigonometry)|], [atan2|]
- ,,
- ,,,
- [sinh (mathematical function)|], [cosh (mathematical function)|], [tanh (mathematical function)|]
- [arsinh|], [arcosh|], [artanh|]
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 deprecated 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 an optimizing 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 version of the standard allowed many variations in implementations. IEEE 754-2008 has reduced these allowances, but a few variations still remain. The reproducibility clause recommends that language standards should provide a means to write reproducible programs and describes what needs to be done to achieve reproducible results.Concrete examples of potentially non-reproducible behavior can be found in C and C++, which allow the use of higher precision for results of floating-point operations and contraction of floating-point expressions, such as regular multiply-and-add into FMA and
1.0/sqrt into a reciprocal square root as a single instruction. C/C++ Compilers such as GCC and cl.exe generally default to allowing both unless specifically asked not to, as these changes can generate faster code without obvious loss of accuracy. Compilers also offer more overtly non-compliant "fast" optimizations. C mathematical functions are usually not implemented to be "correctly rounded" and add to the problem. The floating-point environment may also be unexpectedly changed by third-party code.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 round to nearest, ties 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:
- 5 decimal digits for binary16,
- 9 decimal digits for binary32,
- 17 decimal digits for binary64,
- 36 decimal digits for binary128.
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:
- 7 decimal digits for decimal32,
- 16 decimal digits for decimal64,
- 34 decimal digits for decimal128.
Hexadecimal literals
The standard recommends providing conversions to and from external hexadecimal-significand character sequences, based on C99's hexadecimal floating point literals. Such a literal consists of an optional sign, the indicator "0x", a hexadecimal number with or without a period, an exponent indicator "p", and a decimal exponent with optional sign. The syntax is not case-sensitive. The decimal exponent scales by powers of 2. For example,0x0.1p0 is 1/16 and 0x0.1p-4 is 1/256.Standards
Secondary references
- arithmetic, FAQs, bibliography, and links
- – History and minutes
- . Includes historical perspectives.