Floating-point arithmetic

For this reason, floating-point arithmetic is often used to allow very small and very large real numbers that require fast processing times.

The speed of floating-point operations, commonly measured in terms of FLOPS, is an important characteristic of a computer system, especially for applications that involve intensive mathematical calculations.

In scientific notation, the given number is scaled by a power of 10, so that it lies within a specific range—typically between 1 and 10, with the radix point appearing immediately after the first digit.

To determine the actual value, a decimal point is placed after the first digit of the significand and the result is multiplied by 105 to give 1.528535047×105, or 152,853.5047.

For Torres, "n will always be the same number of digits (e.g. six), the first digit of n will be of order of tenths, the second of hundredths, etc, and one will write each quantity in the form: n; m." The format he proposed shows the need for a fixed-sized significand as is presently used for floating-point data, fixing the location of the decimal point in the significand so that each representation was unique, and how to format such numbers by specifying a syntax to be used that could be entered through a typewriter, as was the case of his Electromechanical Arithmometer in 1920.

[15] In contrast, von Neumann recommended against floating-point numbers for the 1951 IAS machine, arguing that fixed-point arithmetic is preferable.

The arithmetic is actually implemented in software, but with a one megahertz clock rate, the speed of floating-point and fixed-point operations in this machine were initially faster than those of many competing computers.

In 1989, mathematician and computer scientist William Kahan was honored with the Turing Award for being the primary architect behind this proposal; he was aided by his student Jerome Coonen and a visiting professor, Harold Stone.

Three formats are especially widely used in computer hardware and languages:[citation needed] Increasing the precision of the floating-point representation generally reduces the amount of accumulated round-off error caused by intermediate calculations.

Floating-point numbers are typically packed into a computer datum as the sign bit, the exponent field, and the significand or mantissa, from left to right.

In the IEEE binary interchange formats the leading 1 bit of a normalized significand is not actually stored in the computer datum.

This means that numbers that appear to be short and exact when written in decimal format may need to be approximated when converted to binary floating-point.

The IEEE 754 standard requires the same rounding to be applied to all fundamental algebraic operations, including square root and conversions, when there is a numeric (non-NaN) result.

[34] Converting a double-precision binary floating-point number to a decimal string is a common operation, but an algorithm producing results that are both accurate and minimal did not appear in print until 1990, with Steele and White's Dragon4.

[41] The problem of parsing a decimal string into a binary FP representation is complex, with an accurate parser not appearing until Clinger's 1990 work (implemented in dtoa.c).

In the example below, the second number (with the smaller exponent) is shifted right by three digits, and one then proceeds with the usual addition method: In detail: This is the true result, the exact sum of the operands.

The original IEEE 754 standard, however, failed to recommend operations to handle such sets of arithmetic exception flag bits.

Over time some programming language standards (e.g., C99/C11 and Fortran) have been updated to specify methods to access and change status flag bits.

Overflow and invalid exceptions can typically not be ignored, but do not necessarily represent errors: for example, a root-finding routine, as part of its normal operation, may evaluate a passed-in function at values outside of its domain, returning NaN and an invalid exception flag to be ignored until finding a useful start point.

That is, (a + b) × c may not be the same as a × c + b × c: In addition to loss of significance, inability to represent numbers such as π and 0.1 exactly, and other slight inaccuracies, the following phenomena may occur:

This is important since it bounds the relative error in representing any non-zero real number x within the normalized range of a floating-point system:

Although individual arithmetic operations of IEEE 754 are guaranteed accurate to within half a ULP, more complicated formulae can suffer from larger errors for a variety of reasons.

As decimal fractions can often not be exactly represented in binary floating-point, such arithmetic is at its best when it is simply being used to measure real-world quantities over a wide range of scales (such as the orbital period of a moon around Saturn or the mass of a proton), and at its worst when it is expected to model the interactions of quantities expressed as decimal strings that are expected to be exact.

Even simple expressions like 0.6/0.2-3==0 will, on most computers, fail to be true[63] (in IEEE 754 double precision, for example, 0.6/0.2 - 3 is approximately equal to −4.44089209850063×10−16).

[54] Values derived from the primary data representation and their comparisons should be performed in a wider, extended, precision to minimize the risk of such inconsistencies due to round-off errors.

[64] Small errors in floating-point arithmetic can grow when mathematical algorithms perform operations an enormous number of times.

[65] Summation of a vector of floating-point values is a basic algorithm in scientific computing, and so an awareness of when loss of significance can occur is essential.

As an example, Archimedes approximated π by calculating the perimeters of polygons inscribing and circumscribing a circle, starting with hexagons, and successively doubling the number of sides.

Two forms of the recurrence formula for the circumscribed polygon are:[citation needed] Here is a computation using IEEE "double" (a significand with 53 bits of precision) arithmetic: While the two forms of the recurrence formula are clearly mathematically equivalent,[nb 14] the first subtracts 1 from a number extremely close to 1, leading to an increasingly problematic loss of significant digits.

[66] The "fast math" option on many compilers (ICC, GCC, Clang, MSVC...) turns on reassociation along with unsafe assumptions such as a lack of NaN and infinite numbers in IEEE 754.

An early electromechanical programmable computer, the Z3 , included floating-point arithmetic (replica on display at Deutsches Museum in Munich ).
Single-precision floating-point numbers on a number line : the green lines mark representable values.
Augmented version above showing both signs of representable values
Leonardo Torres Quevedo , in 1914, published an analysis of floating point based on the analytical engine .
Konrad Zuse , architect of the Z3 computer, which uses a 22-bit binary floating-point representation
William Kahan , principal architect of the IEEE 754 floating-point standard
Fig. 1: resistances in parallel, with total resistance