How would you store the mass of an electron (about 9.1 × 10-31 kg) and the mass of the Sun (about 2 × 1030 kg) in the same 32-bit variable? With the integer formats from Signed Numbers: Sign-Magnitude, 1's & 2's Complement, a 32-bit word can only hold whole numbers up to about 2.1 billion. You cannot even store 0.5.
Scientists solved this problem long ago with scientific notation: write a number as a few significant digits times a power of ten, like 6.022 × 1023. Computers do exactly the same thing, only in base 2. This is called floating-point representation, and almost every computer today follows one standard for it: IEEE 754.
A floating-point number is stored as a sign, an exponent and a significand (mantissa), and represents the value (-1)sign × significand × 2exponent. The "point" floats because the exponent decides where the binary point really is.
In this chapter we will learn the single and double precision formats, encode and decode numbers step by step, handle the special values (zero, subnormals, infinity, NaN), work out the range and precision, see how floating-point addition and multiplication work, and understand why 0.1 + 0.2 != 0.3. Every bit pattern in this chapter was checked with Python's struct module and a C program.
Why Not Just Use Fixed-Point?
One simple way to store fractions is fixed-point: decide that, say, the last 8 bits of a 32-bit word are after the binary point. Then the stored integer 0x00000180 (384) means 384 / 28 = 1.5.
Fixed-point is fast (it is just integer arithmetic) and is still used in DSP chips and money calculations. But it has a fixed budget: with a signed 32-bit word split into 24 integer bits and 8 fraction bits, the largest value is about 8.4 million (223) and the smallest step is 1/256 ≈ 0.0039. You cannot store both 1030 and 10-30.
Floating-point trades a little precision for a huge range. A 32-bit float can hold values from about 10-45 to 3.4 × 1038, always keeping about 7 significant decimal digits. The key idea is that the gap between neighbouring numbers grows with the size of the number: tiny numbers are packed closely, huge numbers far apart, so the relative error stays roughly constant.
Property | Fixed-point | Floating-point |
|---|---|---|
Position of binary point | Fixed by the design | Given by the exponent of each number |
Range | Small | Very large |
Gap between neighbours | Same everywhere (absolute precision) | Grows with magnitude (relative precision) |
Hardware | Ordinary integer ALU | Needs a floating-point unit (FPU) |
Typical use | DSP, embedded systems, currency | Science, graphics, machine learning, general programming |
Normalization: One Number, One Representation
In decimal, 1234.5 can be written as 1.2345 × 103, 12.345 × 102 or 0.12345 × 104. To avoid many encodings of the same value, we agree on one form: exactly one non-zero digit before the point. This is the normalized form.
In binary, the only non-zero digit is 1. So every normalized binary number looks like:
± 1.xxxxxxx... × 2^e
Examples:
1101.101 = 1.101101 × 2^3
0.0011 = 1.1 × 2^-3
1 = 1.0 × 2^0
Here is the clever trick: since the leading digit of a normalized binary number is always 1, there is no need to store it. IEEE 754 leaves it out and gets one extra bit of precision for free. This invisible bit is called the hidden bit (or implicit leading 1). The stored bits after the point are called the fraction (many textbooks and GATE papers call them the mantissa).
The IEEE 754 Formats
The standard (first published in 1985, revised in 2008 and 2019) defines several binary formats. The two you must know are single precision (C float) and double precision (C double):
Single precision (32 bits, binary32)
31 30 23 22 0
+---+---------------+-------------------------------------+
| S | Exponent E | Fraction F |
+---+---------------+-------------------------------------+
1 8 bits 23 bits
Double precision (64 bits, binary64)
63 62 52 51 0
+---+-------------------+-------------------------------------+
| S | Exponent E | Fraction F |
+---+-------------------+-------------------------------------+
1 11 bits 52 bits
Format | Total bits | Sign | Exponent | Fraction | Bias | Precision (with hidden bit) |
|---|---|---|---|---|---|---|
Half (binary16) | 16 | 1 | 5 | 10 | 15 | 11 bits |
Single (binary32) | 32 | 1 | 8 | 23 | 127 | 24 bits |
Double (binary64) | 64 | 1 | 11 | 52 | 1023 | 53 bits |
Quadruple (binary128) | 128 | 1 | 15 | 112 | 16383 | 113 bits |
For a normalized number, the value is:
Single: V = (-1)S × 1.F × 2E − 127, for 1 ≤ E ≤ 254.
Double: V = (-1)S × 1.F × 2E − 1023, for 1 ≤ E ≤ 2046.
Why a Biased Exponent?
The exponent can be negative (for numbers smaller than 1), so it needs a signed form. Instead of 2's complement, IEEE 754 stores E = actual exponent + bias, where the bias is 2k-1 − 1 for a k-bit exponent field (127 for 8 bits, 1023 for 11 bits). This is the excess-127 code.
Why? Because with the sign bit first and a biased exponent next, positive floats compare in the same order as their bit patterns read as unsigned integers. A larger exponent field means a larger number, and if exponents are equal, a larger fraction means a larger number. Hardware can sort and compare floats with a plain integer comparator. With a 2's complement exponent, a negative exponent would look like a huge unsigned number and this would break.
The two extreme exponent fields (all 0s and all 1s) are reserved for special values, so the usable actual exponent range for single precision is 1 − 127 = −126 to 254 − 127 = +127.
Converting Decimal to IEEE 754 (Step by Step)
Follow these five steps every time:
Note the sign: S = 0 for positive, 1 for negative. Work with the absolute value from now on.
Convert the integer part and the fractional part to binary (fractional part: repeatedly multiply by 2 and take the integer digit, as in Number Systems & Conversions).
Normalize to 1.xxx × 2e.
Biased exponent E = e + 127 (single) or e + 1023 (double), written in binary.
Fraction F = the bits after the point, padded with zeros on the right to 23 (or 52) bits. Group into hex if asked.
Example 1: −13.625 in Single Precision
Step 1: negative, so S = 1. Work with 13.625.
Step 2: 13 = 1101
0.625 × 2 = 1.25 → 1
0.25 × 2 = 0.5 → 0
0.5 × 2 = 1.0 → 1 so 0.625 = .101
13.625 = 1101.101
Step 3: 1101.101 = 1.101101 × 2^3 (point moved 3 places left)
Step 4: E = 3 + 127 = 130 = 1000 0010
Step 5: F = 101101 followed by 17 zeros
= 101 1010 0000 0000 0000 0000
Assemble: S | E | F
1 | 10000010 | 10110100000000000000000
Group in 4s: 1100 0001 0101 1010 0000 0000 0000 0000
Hex: C 1 5 A 0 0 0 0 = 0xC15A0000
In double precision the same number has E = 3 + 1023 = 1026 = 100 0000 0010 and the same fraction bits followed by more zeros, giving 0xC02B400000000000.
Example 2: 0.1 in Single Precision (A Number That Does Not Fit)
0.1 × 2 = 0.2 → 0
0.2 × 2 = 0.4 → 0
0.4 × 2 = 0.8 → 0
0.8 × 2 = 1.6 → 1
0.6 × 2 = 1.2 → 1
0.2 × 2 = 0.4 → 0 ← we have seen 0.2 before: the pattern repeats
...
0.1 = 0.0 0011 0011 0011 0011 ... (binary, repeating forever)
Normalize: 1.100110011001100110011001100... × 2^-4
E = -4 + 127 = 123 = 0111 1011
Fraction needs 23 bits: 10011001100110011001100 | 1100...
^ first discarded bit is 1
and later bits are non-zero,
so round UP
F = 10011001100110011001101
Bits: 0 01111011 10011001100110011001101 = 0x3DCCCCCD
Just like 1/3 = 0.3333... never ends in decimal, 1/10 never ends in binary. The stored float is actually 0.100000001490116119384765625, slightly more than 0.1. In double precision (0x3FB999999999999A) the error is much smaller, but it is still not zero. This is the root of many famous floating-point surprises.
A decimal fraction has an exact binary representation only if its denominator (in lowest terms) is a power of 2. 0.5, 0.25, 0.625 and 0.375 are exact; 0.1, 0.2, 0.3 and 0.7 are not.
Converting IEEE 754 to Decimal
Reverse the process: split the bits, check E for special cases, then compute (-1)S × 1.F × 2E−bias.
Example 3: Decode 0xC0B00000
Hex: C 0 B 0 0 0 0 0
Bits: 1100 0000 1011 0000 0000 0000 0000 0000
S = 1 → negative
E = 1000 0001 = 129 → actual exponent = 129 - 127 = 2
F = 0110 0000 ... → significand = 1.011
Value = -1.011 × 2^2 = -101.1 (binary) = -(4 + 1 + 0.5) = -5.5
Example 4: Decode 0x40490FDB
Bits: 0 10000000 10010010000111111011011
S = 0, E = 128 → exponent 1
1.F = 1.10010010000111111011011 (binary) ≈ 1.5707963705
Value ≈ 1.5707963705 × 2 = 3.1415927410...
This is the float closest to π. It is accurate to about 7 significant digits, which is all a 24-bit significand can give.
Special Values
The reserved exponent patterns give IEEE 754 its special values. For single precision:
Exponent field E | Fraction F | Meaning | Value |
|---|---|---|---|
0 | 0 | Zero | +0 or −0 (depending on S) |
0 | ≠ 0 | Subnormal (denormalized) | (-1)S × 0.F × 2−126 |
1 to 254 | anything | Normalized | (-1)S × 1.F × 2E−127 |
255 (all 1s) | 0 | Infinity | +∞ or −∞ |
255 (all 1s) | ≠ 0 | NaN (Not a Number) | Result of invalid operations |
Zero
Because of the hidden 1, the normal formula can never give 0. So zero is a special pattern: all exponent and fraction bits 0. There are two zeros, +0 (0x00000000) and −0 (0x80000000). They compare equal (-0.0 == 0.0 is true), but they can behave differently, for example 1/(+0) = +∞ while 1/(−0) = −∞.
Subnormal (Denormalized) Numbers
The smallest normalized single is 1.0 × 2−126 ≈ 1.18 × 10−38. Without subnormals, the next value below it would be 0, a sudden big gap. Subnormals fill that gap: when E = 0, the hidden bit becomes 0 and the exponent is fixed at −126 (not 0 − 127 = −127). This gives gradual underflow: numbers lose precision smoothly as they approach zero.
The smallest positive subnormal is 0x00000001 = 2−23 × 2−126 = 2−149 ≈ 1.4 × 10−45.
A useful consequence: with subnormals, x − y = 0 happens only when x = y.
Infinity and NaN
Overflow, or dividing a non-zero number by zero, gives ±∞ instead of crashing: 1.0/0.0 = +∞, and +∞ + 5 = +∞. Invalid operations such as 0/0, ∞ − ∞, 0 × ∞ and sqrt(−1) give NaN. NaN spreads through calculations (NaN + 1 = NaN), and it is unequal to everything, including itself: x != x is true exactly when x is NaN. The standard recommends that the first fraction bit distinguishes quiet NaNs (1) from signalling NaNs (0); a typical quiet NaN is 0x7FC00000.
Range and Precision
Quantity | Single precision | Double precision |
|---|---|---|
Largest finite value | (2 − 2−23) × 2127 ≈ 3.40 × 1038 (0x7F7FFFFF) | (2 − 2−52) × 21023 ≈ 1.80 × 10308 |
Smallest positive normal | 2−126 ≈ 1.18 × 10−38 (0x00800000) | 2−1022 ≈ 2.23 × 10−308 |
Smallest positive subnormal | 2−149 ≈ 1.40 × 10−45 (0x00000001) | 2−1074 ≈ 4.94 × 10−324 |
Machine epsilon (gap after 1.0) | 2−23 ≈ 1.19 × 10−7 | 2−52 ≈ 2.22 × 10−16 |
Decimal digits of precision | 24 × log102 ≈ 7.2 | 53 × log102 ≈ 15.9 |
The Gaps Between Floats
Between 2e and 2e+1 there are exactly 223 single-precision values, evenly spaced. So the gap is 2e−23:
Range Gap between neighbouring floats
[1, 2) 2^-23 ≈ 0.00000012
[1024, 2048) 2^-13 ≈ 0.000122
[2^23, 2^24) 1 (every integer is representable)
[2^24, 2^25) 2 (only even integers!)
That is why 16777217 (224 + 1) cannot be stored in a float: it lies exactly halfway between 16777216 and 16777218 and gets rounded. Every integer up to 224 is exact in a float, and every integer up to 253 is exact in a double.
Rounding
When a result has more bits than the fraction can hold, it must be rounded. IEEE 754 defines these rounding modes:
Round to nearest, ties to even (the default): pick the nearest representable value; if exactly halfway, pick the one whose last fraction bit is 0. Example: 16777217 → 16777216, and 16777219 → 16777220.
Round toward zero (truncation).
Round toward +∞ (ceiling) and round toward −∞ (floor), used in interval arithmetic.
Why ties to even? Always rounding halves up would push long sums slightly upward on average. Rounding ties to even goes up half the time and down half the time, so the bias cancels out.
To round correctly, hardware keeps three extra bits beyond the fraction during an operation: the guard bit, the round bit, and the sticky bit (the OR of all bits shifted out beyond them). With these, the result is the same as if the exact answer had been computed and then rounded once.
Floating-Point Addition
You cannot add significands unless the exponents match, just as you cannot add 1.5 × 103 and 2.0 × 101 directly. The steps are:
Align: shift the significand of the number with the smaller exponent right by the exponent difference.
Add (or subtract) the significands.
Normalize: shift the result to the form 1.xxx and adjust the exponent; check for overflow or underflow.
Round to the fraction width (and re-normalize if rounding carries out).
Example 5: 6.75 + 1.375
6.75 = 110.11 = 1.1011 × 2^2 (0x40D80000, E = 129)
1.375 = 1.011 = 1.011 × 2^0 (0x3FB00000, E = 127)
1. Align: exponent difference = 2, so shift 1.011 right by 2
1.011 × 2^0 = 0.01011 × 2^2
2. Add: 1.10110
+ 0.01011
---------
10.00001 × 2^2
3. Normalize: 10.00001 × 2^2 = 1.000001 × 2^3 (E = 130)
4. Round: 1.000001 fits easily in 23 bits, nothing to round.
Result: 1.000001 × 2^3 = 1000.001 (binary) = 8.125 (0x41020000) ✓
Notice why we shift the smaller number right: its low bits fall off the end, and those are the least significant bits. Shifting the larger number left would lose its most significant bits.
Floating-Point Multiplication
Multiplication is actually simpler than addition, because no alignment is needed:
Sign: S = S1 XOR S2.
Exponent: E = E1 + E2 − bias. (Each stored exponent already contains one bias, so the sum contains two; subtract one.)
Multiply the significands (1.F1 × 1.F2), normalize, and round.
Example 6: 2.5 × (−1.5)
2.5 = 1.01 × 2^1 S1 = 0, E1 = 128
-1.5 = 1.1 × 2^0 S2 = 1, E2 = 127
Sign: 0 XOR 1 = 1
Exponent: 128 + 127 - 127 = 128 (actual exponent 1)
Significand: 1.01
× 1.1
------
101
101
------
1.111 (already normalized)
Result: -1.111 × 2^1 = -11.11 (binary) = -3.75 (0xC0700000) ✓
A common mistake is to add the stored exponents (128 + 127 = 255) and forget to subtract the bias. 255 is the infinity/NaN pattern, which would be completely wrong.
Floating-Point Surprises (and Why They Happen)
Here is a C program that prints the fields of a few floats and shows the classic surprises. It was compiled with gcc -std=c17 -Wall and run on x86-64 Linux:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
/* Print the sign, exponent and fraction fields of a float */
void show(float f) {
uint32_t u;
memcpy(&u, &f, sizeof u); /* copy the raw 32 bits */
unsigned sign = u >> 31;
unsigned exp = (u >> 23) & 0xFF;
unsigned frac = u & 0x7FFFFF;
printf("%-14.9g hex=%08X S=%u E=%3u F=0x%06X\n", f, u, sign, exp, frac);
}
int main(void) {
show(-13.625f);
show(0.1f);
show(1.0f);
show(16777217.0f); /* 2^24 + 1 does not fit */
show(1e-45f); /* a subnormal */
double a = 0.1 + 0.2;
printf("0.1 + 0.2 == 0.3 ? %s\n", a == 0.3 ? "yes" : "no");
printf("0.1 + 0.2 = %.17g\n", a);
float big = 1e20f;
printf("(big - big) + 1 = %g\n", (big - big) + 1.0f);
printf("big + (-big + 1) = %g\n", big + (-big + 1.0f));
return 0;
}
Output:
-13.625 hex=C15A0000 S=1 E=130 F=0x5A0000
0.100000001 hex=3DCCCCCD S=0 E=123 F=0x4CCCCD
1 hex=3F800000 S=0 E=127 F=0x000000
16777216 hex=4B800000 S=0 E=151 F=0x000000
1.40129846e-45 hex=00000001 S=0 E= 0 F=0x000001
0.1 + 0.2 == 0.3 ? no
0.1 + 0.2 = 0.30000000000000004
(big - big) + 1 = 1
big + (-big + 1) = 0
What each line teaches:
0.1f is stored as 0.100000001: 0.1 has no exact binary form.
16777217.0f becomes 16777216: above 224 the gap between floats is 2.
1e-45f is the subnormal 0x00000001: E = 0 and only the last fraction bit is set.
0.1 + 0.2 ≠ 0.3: each of the three numbers is rounded separately, and the errors do not cancel. Never compare floats with
==; checkfabs(a - b) < toleranceinstead.Floating-point addition is not associative: in
big + (-big + 1), adding 1 to −1020 changes nothing (1 is far below the gap at 1020), so the 1 is lost. Order of operations matters, which is why compilers do not reorder float arithmetic unless you allow it (e.g.-ffast-math).
(We used memcpy to view the bits. Casting a float* to uint32_t* breaks C's strict aliasing rule.)
Integer vs Floating-Point: Quick Comparison
Point | 32-bit 2's complement int | 32-bit IEEE 754 float |
|---|---|---|
Number of bit patterns | 232 | 232 (same!) |
Range | −231 to 231 − 1 | about ±3.4 × 1038, plus ±∞ |
Exact? | Every value in range is exact | Most real numbers are rounded |
Overflow | Wraps around (silent) | Becomes ±∞ |
Divide by zero | Error / undefined behaviour in C | ±∞ or NaN |
Zeros | One | Two (+0, −0) |
The float does not have "more numbers". It spreads the same 232 patterns over a much wider range, so they are spaced further apart.
Common Mistakes
Forgetting the hidden bit. The significand is 1.F, not 0.F, for normalized numbers. When decoding, always add the leading 1.
Including the hidden 1 in the fraction field. When encoding, store only the bits after the point.
Using the wrong bias. 127 for single, 1023 for double, 2k−1 − 1 in general for a k-bit exponent. A question may define its own format; read the bias from the question.
Using exponent −127 for subnormals. Subnormals use 2−126 (single) with significand 0.F.
Padding the fraction on the wrong side. The fraction is padded with zeros on the right, the exponent with zeros on the left.
Adding biased exponents in multiplication without subtracting the bias.
Saying a float has 23 bits of precision. It has 24 (23 stored + 1 hidden).
Comparing floats with ==. Use a tolerance, and remember that NaN is not equal to itself.
GATE and Interview Questions
Q1. What is the IEEE 754 single precision representation (in hex) of −14.25?
14.25 = 1110.01 = 1.11001 × 23. S = 1, E = 130 = 10000010, F = 11001000...0. Bits: 1 10000010 11001000000000000000000 = 1100 0001 0110 0100 0000 ... = 0xC1640000.
Q2. What decimal value does the single precision pattern 0xC1E00000 represent?
Bits: 1 10000011 11000000000000000000000. S = 1, E = 131, actual exponent 4, significand 1.11 = 1.75. Value = −1.75 × 16 = −28.
Q3. A 16-bit format has 1 sign bit, a 5-bit exponent with bias 15, and a 10-bit fraction with a hidden 1 (this is IEEE half precision). Encode 0.15625.
0.15625 = 0.00101 = 1.01 × 2−3. E = −3 + 15 = 12 = 01100. F = 0100000000. Pattern: 0 01100 0100000000 = 0011 0001 0000 0000 = 0x3100.
Q4. Why is the exponent stored with a bias instead of in 2's complement?
So that the exponent field increases with the value. Then (non-negative) floats can be compared and sorted using simple unsigned integer comparison of their bit patterns, and zero (all bits 0) is the smallest pattern.
Q5. What are the largest and smallest positive normalized single precision values?
Largest: E = 254, F = all 1s: (2 − 2−23) × 2127 ≈ 3.4 × 1038. Smallest normalized: E = 1, F = 0: 2−126 ≈ 1.18 × 10−38. (The smallest positive subnormal is 2−149.)
Q6. Can every 32-bit int be stored exactly in a float? In a double?
Not in a float: a float has only 24 significant bits, so integers above 224 = 16,777,216 start to be rounded. Yes in a double: it has 53 significant bits, and every 32-bit integer fits.
Q7. Why does 0.1 + 0.2 not equal 0.3 in most languages?
0.1, 0.2 and 0.3 have infinite binary expansions and are each rounded to the nearest double. The rounded 0.1 plus the rounded 0.2 gives 0.30000000000000004, which is a different double from the rounded 0.3.
Q8. What is the result of 0.0/0.0, 1.0/0.0 and −1.0/0.0 in IEEE arithmetic?
NaN, +∞ and −∞ respectively. No exception stops the program by default; a status flag is raised instead.
Q9. What is the purpose of subnormal numbers?
They provide gradual underflow, filling the gap between 0 and the smallest normal number with evenly spaced values. This guarantees that x − y = 0 only when x = y, and avoids the sudden loss of all precision when results get very small.
Summary
Floating-point stores a number as sign, biased exponent and fraction, like scientific notation in base 2, trading some precision for a huge range.
Single precision: 1 + 8 + 23 bits, bias 127. Double precision: 1 + 11 + 52 bits, bias 1023. Normalized value = (-1)S × 1.F × 2E−bias.
The leading 1 is hidden, giving 24 (single) and 53 (double) bits of precision.
Exponent all 0s means zero or subnormal (0.F × 2−126); all 1s means infinity (F = 0) or NaN (F ≠ 0).
Most decimal fractions (like 0.1) are not exact in binary, so floats are rounded, by default to nearest, ties to even.
Addition: align, add, normalize, round. Multiplication: XOR signs, add exponents and subtract the bias, multiply significands, normalize, round.
Float arithmetic is not associative and should not be compared with ==.
Next, in BCD, Gray Code, ASCII & Unicode, we look at other ways of encoding numbers and text, where the goal is not range but convenience, reliability or human readability.