Floating Point
converter.
See the exact bits a computer stores for a decimal, and how far off the stored value is.
- Sign
- 0, positive
- Exponent
- 2⁻⁴
- Kind
- Normal
- Off by
- 1.49 × 10⁻⁹
0.1 has no exact binary form in single precision, so the nearest value is stored, 1.49 × 10⁻⁹ too high. That is 0.2 of a step to the next value (one ulp, 7.45 × 10⁻⁹).
The bits
Click a bit to flip it32 bits: sign 0, exponent 01111011 (123, power -4), fraction 1001 1001 1001 1001 1001 101. Hex 0x3DCCCCCD.
Working
- 1Sign bit: 0 for positive, 1 for negatives = 0
- 2Write the value as 1.f × 2 to a power1.10011001100110011001101₂ × 2⁻⁴
- 3Add the bias 127 to the power and write it in 8 bits−4 + 127 = 123 = 01111011
- 4Keep 23 fraction bits, rounding to the nearest and to an even last bit on a tie1001 1001 1001 1001 1001 101
- 5Join sign, exponent and fraction, then read four bits at a time as hex0 01111011 10011001… = 0x3DCCCCCD
- 6Check: decode the bits back to the exact value stored(1 +5,033,1658,388,608)× 2⁻⁴ =13,421,773134,217,728= 0.100000001490116119384765625
The exact value stored
0.100000001490116119384765625
Every finite single precision value is a whole number times a power of 2, so its decimal form always ends. The shortest decimal that gives the same bits is 0.1.
Limits of single precision
| Value | About | Hex |
|---|---|---|
| Largest finite | 3.4028 × 10³⁸Hex 0x7F7FFFFF | 0x7F7FFFFF |
| Smallest normal | 1.1755 × 10⁻³⁸Hex 0x00800000 | 0x00800000 |
| Smallest subnormal | 1.4013 × 10⁻⁴⁵Hex 0x00000001 | 0x00000001 |
| Gap above 1 | 1.1921 × 10⁻⁷Hex 0x3F800001 | 0x3F800001 |
| Exact integers up to | 16,777,216Hex 0x4B800000 | 0x4B800000 |
| One | 1Hex 0x3F800000 | 0x3F800000 |
| Overflow becomes | ∞Hex 0x7F800000 | 0x7F800000 |
32 bits: 1 sign bit, 8 exponent bits with bias 127, and 23 fraction bits.
Convert a list
Results
| Decimal | Hex | Off by |
|---|---|---|
| 0.1 | 0x3DCCCCCDOff by 1.49 × 10⁻⁹ | 1.49 × 10⁻⁹ |
| 0.2 | 0x3E4CCCCDOff by 2.98 × 10⁻⁹ | 2.98 × 10⁻⁹ |
| 0.3 | 0x3E99999AOff by 1.19 × 10⁻⁸ | 1.19 × 10⁻⁸ |
| 1e-7 | 0x33D6BF95Off by 1.17 × 10⁻¹⁵ | 1.17 × 10⁻¹⁵ |
| 16777217 | 0x4B800000Off by −1 | −1 |
How it's worked out
IEEE 754 stores a number in three fields: one sign bit, an exponent and a fraction. The value is rebuilt from them like this:
- is the sign bit, 0 for positive and 1 for negative.
- is the stored exponent field and is the bias: 15 for half, 127 for single and 1023 for double precision. Subtracting the bias lets an unsigned field hold negative powers.
- is the fraction field read as a whole number, and is how many bits it has: 10, 23 or 52. The leading 1 is not stored, so single precision keeps 24 significant bits.
So 1 in single precision is a sign of 0, an exponent field of 127, which is the bias, and a fraction of all zeros. That makes 0x3F800000. An exponent field of all zeros means zero or a subnormal number, which drops the leading 1. All ones means infinity, or a code for not a number.
To encode a decimal, the tool finds the nearest value the format can hold. Goldberg explains the rule IEEE 754 uses: round to the nearest value, and on an exact tie pick the one whose last bit is even. It then writes out the stored value in full. Every finite float is a whole number times a power of 2, so its decimal form always ends.
Worked examples
Maya asks why 0.1 + 0.2 is not 0.3 in JavaScript
JavaScript and Python numbers are doubles. In binary, 0.1 repeats forever, so the double nearest to it is stored: 0x3FB999999999999A, which is about 0.10000000000000000555 (the tool shows all 55 decimal places). That is about 5.55 × 10⁻¹⁸ too high.
0.2 is stored as about 0.2000000000000000111, also a little high. Add the two and round, and you get 0x3FD3333333333334, which prints as 0.30000000000000004. The double for 0.3 is 0x3FD3333333333333, one step lower, because 0.3 is stored as about 0.2999999999999999889, a little low.
So the two sides of 0.1 + 0.2 == 0.3 land on neighboring doubles and the test fails. Compare with a small tolerance instead, or work in whole cents.
Sam encodes −118.625 for a homework sheet
118.625 is 1110110.101 in binary, because 0.625 is . Move the point six places left to get 1.110110101 times 2 to the power 6.
The sign bit is 1. The exponent field is 6 + 127 = 133, which is 10000101 in 8 bits. The fraction is the bits after the point, padded with zeros to 23 bits: 11011010100000000000000.
Put together and read four bits at a time, that is 0xC2ED4000. The error is 0, because 118.625 is a sum of powers of 2 that fits in the fraction.
Lee's game counter stops at 16,777,216
Lee keeps a score in a 32-bit float. Past 16,777,216, which is 2 to the power 24, adding 1 does nothing. The tool shows why: 16,777,217 is stored as 16777216, off by −1.
At that size the gap between neighboring floats is 2. A sum that lands halfway rounds to the even pattern, so 16,777,216 + 1 rounds back down. A 32-bit integer or a double, exact up to 9,007,199,254,740,992, fixes it.
Reading your result
The hex pattern is the answer to type into code or compare with a memory dump. In C, a float of 0.1 holds 0x3DCCCCCD. Its exact value is , about 1.49 × 10⁻⁹ above 0.1.
The error and the ulp. The ulp, a unit in the last place, is the gap to the next value the format can hold. Correct rounding never misses by more than half an ulp, so any reading up to 0.5 ulp means the format could do no better. Errors grow when many rounded steps pile up.
Digits you can trust. Wikipedia's format pages give 6 to 9 significant decimal digits for single precision and 15 to 17 for double. Half precision holds integers exactly only up to 2,048, and its largest finite value is 65,504.
Subnormal values sit below the smallest normal number, 1.18 × 10⁻³⁸ in single precision. They have an exponent field of zero and no hidden 1, so 1e-40 is stored with fewer significant bits, as 1e-40.
Common mistakes
- Testing floats with ==. Two results that should match can land one ulp apart. Compare the difference with a tolerance that suits the size of the numbers.
- Keeping money in floats. 0.10 has no exact binary form, so a long run of additions drifts. Store whole cents, or use a decimal type.
- Reading printed digits as stored digits. A single-precision 0.1 prints as 0.1, but it holds 0.100000001490116119384765625.
- Forgetting half precision overflows early. 65,519 is stored as 65500, but 65,520 becomes Infinity, because ties round to the even pattern.
- Rounding twice. Converting a double to single and then to half can land on a different value than rounding once. The tool rounds once from the decimal you type, so 1 + 2^-11 in half precision is 0x3C00.
Questions people ask
Sources
- Goldberg, D. (1991). What Every Computer Scientist Should Know About Floating-Point Arithmetic (Oracle edition)
- Wikipedia, Half-precision floating-point format (IEEE 754 binary16)
- Wikipedia, Single-precision floating-point format (IEEE 754 binary32)
- Wikipedia, Double-precision floating-point format (IEEE 754 binary64)