Understanding the IEEE 754 floating-point converter — concept and principle
IEEE 754 is the international standard for how computers store real numbers in binary. This converter shows how a decimal number is encoded in 32-bit single precision and 64-bit double precision, splits the 32-bit pattern into its sign, exponent and mantissa fields, and reveals the value that is actually stored.
How it is calculated
A normal floating-point number is stored as
value = (−1)^s × 1.f × 2^(e − bias)
- s (sign): 1 bit, 0 for positive and 1 for negative.
- e (exponent): 8 bits with bias 127 in single precision; 11 bits with bias 1023 in double precision.
- f (fraction or mantissa): 23 bits in single and 52 bits in double precision. The leading 1 is implied and not stored, giving 24 and 53 significant bits.
The input is rounded to the nearest representable value (ties to even).
The principle
Floating point is scientific notation in base 2: a fixed number of significant bits plus an exponent that moves the binary point. This gives a huge range with a constant relative precision of about 7 decimal digits in single and about 16 in double precision. The catch is that only fractions whose denominator is a power of 2 can be stored exactly. 1/10 has a factor 5 in its denominator, so in binary it is the repeating pattern 0.000110011001100…, which must be cut off and rounded, just as 1/3 must be rounded in decimal.
Worked example
- Input: 0.1. Write it as 1.6 × 2⁻⁴, so s = 0.
- Exponent field: −4 + 127 = 123 = 01111011.
- Fraction: 0.6 in binary is 0.1001 1001 1001…; keeping 23 bits and rounding up the last one gives 10011001100110011001101.
- Together the bits read 0 01111011 10011001100110011001101, which is 3DCCCCCD in hex.
- Stored value: 13,421,773 ÷ 2²⁷ = 0.100000001490116…, shown as 0.1000000015.
- Double precision: exponent −4 + 1023 = 1019 = 3FB, and the 52-bit fraction rounds to 999999999999A, giving 3FB999999999999A.
How to read the result
The stored value shows the representation error. For 0.1 in single precision it is about 1.5 × 10⁻⁹; in double precision the stored value is 0.1000000000000000055…, accurate to about 17 significant digits. An exponent field of all zeros means zero or a subnormal number; all ones means infinity or NaN. The largest finite single-precision value is about 3.40 × 10³⁸ and the largest double is about 1.80 × 10³⁰⁸.
Common mistakes
- Comparing floating-point results with == instead of allowing a small tolerance.
- Using binary floating point for money; use integer cents or decimal types instead.
- Forgetting the bias and reading the exponent field as the actual power.
Frequently asked questions
Why does 0.1 + 0.2 give 0.30000000000000004?
Both inputs are rounded when stored, and their sum rounds to the double just above 0.3 rather than the double nearest to 0.3.
Which integers can a double store exactly?
Every integer with magnitude up to 2⁵³ = 9,007,199,254,740,992.
Sources: IEEE Std 754-2019, IEEE Standard for Floating-Point Arithmetic · D. Goldberg, "What Every Computer Scientist Should Know About Floating-Point Arithmetic", ACM Computing Surveys 23(1), 1991