IEEE 754 floating point converter
Type a decimal number to see exactly how a computer stores it as a 32 bit float or a 64 bit double: the sign, exponent and fraction bits, the value actually stored, and how far that is from what you typed.
Any decimal, such as 3.14, -2.5e-8 or 1e300, and also Infinity, -Infinity and NaN.
Click a bit to flip it. Hex: 3DCCCCCD
- Sign
- 0
- positive
- Exponent
- 01111011
- 123 − 127 = −4
- Significand
- 1.10011001100110011001101
- = 1.60000002384185791015625
+1.60000002384185791015… × 2−4
The rounding error in full
0.000000001490116119384765625
Stored value minus the number typed, exactly. It is positive because the nearest float is above it.
How floating point works
A float is scientific notation in binary. Any nonzero number can be written as 1.something × 2n, the way decimal scientific notation writes 6.02 × 1023. IEEE 754 stores three parts of that:
- Sign: one bit, 0 for positive and 1 for negative. The rest of the number is the same either way, unlike two's complement integers.
- Exponent: the power n, stored with a bias added so the field is never negative. For a float the bias is 127, so 20 is stored as 127 and 2−3 as 124.
- Fraction: the bits after the point. The 1 before the point is always there for a normal number, so it is not stored at all, which gains a bit of precision for free.
| Float (single) | Double | |
|---|---|---|
| Total bits | 32 | 64 |
| Exponent bits | 8 | 11 |
| Fraction bits | 23 (+1 hidden) | 52 (+1 hidden) |
| Bias | 127 | 1023 |
| Decimal digits | about 7 | about 15 |
| Largest | 3.4028235e+38 | 1.7976931348623157e+308 |
| Smallest normal | 1.1754944e-38 | 2.2250738585072014e-308 |
| Smallest subnormal | 1e-45 | 5e-324 |
| Epsilon (gap above 1) | 1.1920929e-7 | 2.220446049250313e-16 |
Between two powers of two there are always the same number of floats, 223 for single precision, so the gap between neighbours doubles every time the exponent goes up by one. Above 224 the gap is 2, and a float can no longer hold every integer: 16777217 is stored as 16777216, rounded down. See it.
Printable IEEE 754 chart
The float32 and float64 bit layouts, the bias, the value formula and 5.75 worked through, on one sheet for printing or a slide.
Click to download: IEEE 754 floating point format chart
Why 0.1 + 0.2 is not 0.3
One tenth has no finite binary expansion, just as one third has none in decimal: it is 0.000110011001100… with the 1100 repeating forever. A double keeps 53 significant bits and rounds the rest, so each of these numbers is stored as something slightly different. These are the exact values of the doubles, computed by the converter's engine:
| Typed | Exact value of the double stored |
|---|---|
| 0.1 | 0.1000000000000000055511151231257827021181583404541015625 |
| 0.2 | 0.200000000000000011102230246251565404236316680908203125 |
| 0.1 + 0.2 | 0.3000000000000000444089209850062616169452667236328125 |
| 0.3 | 0.299999999999999988897769753748434595763683319091796875 |
0.1 is rounded up and 0.2 is rounded up. Their sum is rounded again, to the double printed as 0.30000000000000004. But 0.3 on its own is rounded down, to a different double, 3FD3333333333333 rather than 3FD3333333333334. The two differ in the last bit, so 0.1 + 0.2 == 0.3 is false in JavaScript, Python, C and every other language using IEEE 754 doubles. The fix is to compare with a small tolerance, or to use integers (cents rather than pounds) when values must be exact.
Special values
The all-zeros and all-ones exponents are reserved. All zeros means zero or a subnormal; all ones means infinity or NaN. These are the float (32 bit) patterns:
| Value | Hex | Sign | Exponent | Fraction |
|---|---|---|---|---|
| Zero | 00000000 | 0 | 00000000 | 000000…000 |
| Negative zero | 80000000 | 1 | 00000000 | 000000…000 |
| Infinity | 7F800000 | 0 | 11111111 | 000000…000 |
| Negative infinity | FF800000 | 1 | 11111111 | 000000…000 |
| NaN | 7FC00000 | 0 | 11111111 | 100000…000 |
| Smallest subnormal | 00000001 | 0 | 00000000 | 000000…001 |
| Smallest normal | 00800000 | 0 | 00000001 | 000000…000 |
| Largest finite | 7F7FFFFF | 0 | 11111110 | 111111…111 |
NaN, not a number, is the result of operations with no sensible answer, such as 0 ÷ 0 or ∞ − ∞. Any fraction other than zero under an all-ones exponent is a NaN, and a NaN is not equal even to itself. Subnormals fill the gap between the smallest normal number and zero in even steps, so that x − y is zero only when x equals y.
Integers use a different encoding: see the binary converter and two's complement. To see the bits of a hex pattern one nibble at a time, use the hex to binary converter.
Questions
What is IEEE 754?
The standard that almost every processor and programming language uses for floating point numbers. It fixes the layout of the bits, one sign bit, then an exponent, then a fraction, and it fixes how results are rounded, so the same calculation gives the same bits on any machine. float in C and Java is its 32 bit single precision format; double, and every number in JavaScript, is the 64 bit double precision format.
Why is 0.1 + 0.2 not equal to 0.3?
Because none of the three can be stored exactly. 0.1 in binary is 0.0001100110011… repeating forever, so it is rounded to the nearest double, which is slightly above 0.1. The same happens to 0.2. Their sum rounds to 0.30000000000000004, while 0.3 on its own rounds to a different double slightly below 0.3, so the comparison fails. Compare floats with a tolerance instead of ==.
How do I convert a decimal number to IEEE 754 by hand?
Write the number in binary, then shift the point until one 1 is left in front of it: 5.75 is 101.11, which is 1.0111 × 2². The sign bit is 0 for positive. The exponent field is the power plus the bias, 2 + 127 = 129 = 10000001 for single precision. The fraction is the bits after the point, 0111, padded with zeros to 23 bits. The leading 1 is not stored.
What is the exponent bias?
A fixed number added to the real exponent so the field can be stored as an unsigned number: 127 for single precision and 1023 for double. An exponent field of 127 in a float means 2⁰. Storing it biased means larger floats have larger bit patterns, so positive floats can be compared as plain integers.
What are subnormal numbers?
Numbers too small for the normal format. When the exponent field is all zeros, the hidden leading bit becomes 0 instead of 1 and the exponent stays at its minimum, so values fade towards zero in even steps instead of jumping from the smallest normal number straight to zero. They cost precision, and on some processors speed.
Why are there two zeros?
Because the sign bit is separate from the rest, +0 and −0 have different bit patterns. They compare equal, but they behave differently in a few places: 1/+0 is +Infinity and 1/−0 is −Infinity. A negative number that underflows keeps its sign as −0.
How precise are float and double?
A float has a 24 bit significand, about 7 significant decimal digits, and a double has 53 bits, about 15. Every integer up to 2²⁴ = 16,777,216 fits exactly in a float and every integer up to 2⁵³ in a double; above that, some integers are skipped, which is why 16777217 becomes 16777216 as a float.