StringMash.com

IEEE 754 converter

Sign, exponent and fraction, and how close the stored value really is.

Conversion
3 characters
Updates as you type
Hex0x3DCCCCCD
Sign0
Exponent01111011 (123, 2^-4)
Fraction10011001100110011001101
Stored value0.100000001

Show the steps
  1. Sign bit 0: positive.
  2. Exponent bits 01111011 = 123. Subtract the bias, 127: 2^-4.
  3. Fraction bits after an implied leading 1: 1.10011001100110011001101 in binary.
  4. Value = 1.fraction × 2^-4 = 0.10000000149011612.
  5. 0.1 can't be stored exactly in single precision. The nearest value it can store is 0.100000001, off by 1.490e-9.

Using it

Type a number such as 0.1, -2.5 or 6.02e23. The output shows the bits split into sign, exponent and fraction; the rows give the hex, each field, and the value actually stored. If the number can't be stored exactly, the steps say how far off the stored one is. Infinity and NaN work too.

Swap the boxes to decode. Type the bits, or the hex such as 0x3DCCCCCD, and set Precision to match its length: 32 bits for single, 64 for double.

How a float is laid out

A single-precision float is 32 bits: 1 sign bit, 8 exponent bits and 23 fraction bits. Double precision is 64: 1, 11 and 52. The value is (−1)^sign × 1.fraction × 2^(exponent − bias), where the bias is 127 for single and 1023 for double. The leading 1 isn't stored, because every normal number has one, which buys an extra bit of precision for free.

An exponent of all zeros means zero or a subnormal number, a tiny value that gives up the hidden 1 to get closer to zero. An exponent of all ones means infinity if the fraction is zero, and NaN if it isn't.

Why 0.1 + 0.2 isn't 0.3

0.1 in binary is 0.0001100110011… repeating forever, the way 1/3 repeats in decimal. A float has to stop somewhere, so 0.1 is stored as the nearest value it can hold: 0.100000001490116… in single precision. Add two numbers that are each slightly off and the error can show, which is why 0.1 + 0.2 gives 0.30000000000000004 in JavaScript, Python and most other languages that use doubles.

Some values to check against

NumberSingle (hex)Double (hex)
13F8000003FF0000000000000
-2C0000000C000000000000000
0.13DCCCCCD3FB999999999999A
0.53F0000003FE0000000000000
Infinity7F8000007FF0000000000000

Questions

What's the difference between single and double precision?

Single uses 32 bits and holds about 7 significant decimal digits; double uses 64 and holds about 15 to 17.

Is −0 different from 0?

As bits, yes: −0 has the sign bit set. They compare as equal, but dividing by them gives −Infinity and Infinity.

Which numbers can be stored exactly?

Those that are a whole number times a power of two within range, such as 0.5, 0.75 and 3.25. Most decimal fractions aren't.

Sources

Added . What's new