ScienceComputer Science

Floating Point
converter.

See the exact bits a computer stores for a decimal, and how far off the stored value is.

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:

x\displaystyle x=(−1)s×(1+F2m)×2E−b\displaystyle {}= (-1)^{s} \times \left(1 + \frac{F}{2^{m}}\right) \times 2^{E - b}
  • ss is the sign bit, 0 for positive and 1 for negative.
  • EE is the stored exponent field and bb is the bias: 15 for half, 127 for single and 1023 for double precision. Subtracting the bias lets an unsigned field hold negative powers.
  • FF is the fraction field read as a whole number, and mm 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.

Open this example in the tool

Sam encodes −118.625 for a homework sheet

118.625 is 1110110.101 in binary, because 0.625 is 12+18\frac{1}{2} + \frac{1}{8}. 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.

Open this example in the tool

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.

Open this example in the tool

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 13421773134217728\frac{13421773}{134217728}, 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

None of the three can be stored exactly in binary. 0.1 and 0.2 are each stored a little high, and their rounded sum lands on the double just above the one used for 0.3. The language is not broken; it is rounding to the nearest value a 64-bit float can hold.

Sources