Floating-point Arithmetic
This page or section is a stub. You can help the wiki by accurately contributing to it. |
Floating-point arithmetic is arithmetic with floating-point numbers, which is represented by a fixed number of significant digits (the significand) and scaled by an exponent in some base. This is most useful when you need to do arithmetic on very big or very small numbers. Representable numbers have the form significand*base^{exponent}, where significand and exponent are integers and base is an integer greater than or equal to two.
The most common representation is the one defined by the IEEE 754 standard.
A floating-point unit is specially designed to carry out operations on floating-point numbers.
Contents
IEEE 754
The IEEE 754 standard defines
- Arithmetic formats
- Interchange formats
- Rounding rules
- Operations
- Exception handling
Basic Formats
Format | Name | Base | Significand bits | Exponent bits | Exponent bias |
---|---|---|---|---|---|
binary32 | Single precision | 2 | 24 | 8 | 127 |
binary64 | Double precision | 2 | 53 | 11 | 1023 |
binary128 | Quadruple precision | 2 | 113 | 15 | 16383 |
decimal64 | 10 | 16 | 9.58 | 398 | |
decimal128 | 10 | 34 | 13.58 | 6176 |
The floating point number is internally represented as [Sign|Exponent|Significand] with the number of bits as shown in the table above. Converting the binary formats to something readable is done as follows
(-1)^{sign} * (1.significand)_{2} * 2^{exp - bias}
For example, the binary32 number 0x41880000:
0 | 10000011 | (1)00010000000000000000000 (-1)^{0} * (1.00010000000000000000000)_{2} * 2^{100000112 - 127} (1.00010000000000000000000)_{2} * 2^{131 - 127} (1.00010000000000000000000)_{2} * 2^{4} (2^{0} + 2^{-4}) * 2^{4} = 17
All IEEE 754 floating point numbers (except denormalized numbers, which have an exponent of -Emax, or binary 00000000) have an implied/invisible 1, which is not stored, as shown by the (1) in the example above.
Rounding Rules
The standard defines five rounding rules.
- 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 (zero) least significant bit
- Round to nearest, ties away from zero - rounds to the nearest value; if the number falls midway it is rounded towards the nearest value, above for positive numbers and down for negative numbers
- Round toward 0 - directed rounding towards zero (truncation)
- Round toward +∞ - directed rounding towards positive infinity (ceiling)
- Round toward −∞ - directed rounding towards negative infinity (floor)
Required Operations
Special Values
- Not a Number (NaN) - returned by undefined operations such as 0/0, or sqrt(-1)
- Quiet NaN - the default NaN, will be propagated by most operations (e.g. if any input is NaN, the result will be NaN)
- Signaling NaN - will cause an invalid exception to be signaled if it is encountered in any arithmetic operation. There are some unspecified bits in the NaN format which may be used to encode the source of the error.
- Infinity - representation of the real infinity numbers. They are often used as overflow values, though they are not error values in any way. A divide-by-zero operation will return a signed infinity
- Signed zero - in the IEEE 754 standard, zero is always signed. Some arithmetic operations will behave differently for +0 and -0, for example, the identity 1/(1/±∞) = ±∞ is maintained
- 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
Exceptions
Floating-point Arithmetic Operations
Addition
Adding floating point numbers requires a few more steps than adding integers.
1.10010000000000000000000_{2} * 2^{3} (= 12.5) 1.01110000000000000000000_{2} * 2^{4} (= 23)
First, the exponents need to be made equal. Then the significands can be added as if they were integers.
0.11001000000000000000000 0_{2} * 2^{4} 1.01110000000000000000000_{2} * 2^{4} +------------------------------------- 10.00111000000000000000000 0_{2} * 2^{4}
This should be shifted, right if there is a carry, or as many places left as there are leading zeroes (not counting the implicit one). Additionally, the exponent should be incremented (when shifting right) or decremented (when shifting left) the same number of places as the shift.
1.000111000000000000000000 00_{2} * 2^{5} (= 35.5)
The last step would be rounding the result based on the chosen rounding rules. The above example does not need to be rounded, as the 2 lowest bits are both zero.
Subtraction
Multiplication
Division
Rounding
Take for example the number 4.0 and 2.5*10^{7}, adding them will result in the non-significant bits being non-zero.
1.000000000000000000000000_{2} * 2^{2} 1.010101101011111110010101_{2} * 2^{-23} 1.000000000000000000000000_{2} * 2^{2} 0.000000000000000000000000 10101011_{2} * 2^{2} +------------------------------------------ 1.000000000000000000000000 10101011_{2} * 2^{2}
This will be rounded differently depending on the chosen rounding rule.
- Round to nearest, ties to even: 1.000000000000000000000000_{2} * 2^{2} (= 4.0)
- Round to nearest, ties away from zero: 1.000000000000000000000001_{2} * 2^{2} (= 4.0000005)
- Round toward zero: 1.000000000000000000000000_{2} * 2^{2} (= 4.0)
- Round toward +∞: 1.000000000000000000000001_{2} * 2^{2} (= 4.0000005)
- Round toward −∞: 1.000000000000000000000000_{2} * 2^{2} (= 4.0)
Floating-point Unit
See also
- [1] - IEEE-754 Floating Point converter (binary, hexadecimal, individual fields, and rounding error)