Every computer systems exam and low-level programming course eventually asks you to convert a decimal number into IEEE 754 floating-point format by hand. While the binary layout looks intimidating at first glance, the conversion follows seven mechanical steps: extracting the sign, converting the integer and fractional parts to binary, normalizing the radix point, adding the exponent bias, extracting the mantissa, and applying rounding.
This guide walks through the exact 32-bit single precision (binary32) encoding standard used by modern hardware, tracing two complete worked examples—-13.625 (a terminating binary fraction) and 0.1 (an infinite repeating binary fraction that requires rounding). We then examine the underlying bit patterns that cause the famous 0.1 + 0.2 != 0.3 floating-point anomaly in JavaScript, Python, C, and Java.
Anatomy of an IEEE 754 single precision float
Standard IEEE 754 single precision uses 32 bits (4 bytes) partitioned into three distinct fields:
[ Bit 31 ] [ Bits 30-23 ] [ Bits 22-0 ]
Sign Biased Exp Mantissa (Fraction)
(1 bit) (8 bits) (23 bits)
| Field | Width | Bit indices | Purpose |
|---|---|---|---|
| Sign (S) | 1 bit | Bit 31 | 0 for positive numbers, 1 for negative numbers. |
| Biased Exponent (e) | 8 bits | Bits 30–23 | Unsigned integer representing powers of 2, stored with a fixed bias of 127. |
| Mantissa / Fraction (M) | 23 bits | Bits 22–0 | The fractional part following an implicit leading 1. for normal numbers. |
For normal numbers, the real numeric value represented by the 32-bit word is given by:
Value = (-1)^S × (1 + M / 2^23) × 2^(e - 127)
Because normalized binary numbers always begin with a non-zero digit (1), IEEE 754 does not waste memory storing this leading digit. This hidden bit gives 32-bit floats 24 bits of effective precision using only 23 stored mantissa bits.
The 7-step conversion algorithm
To convert any decimal number into 32-bit IEEE 754 single precision:
- Determine the sign bit (S):
If the number is positive or zero,S = 0. If negative,S = 1. - Convert the integer part to binary:
Divide the integer repeatedly by 2, recording remainders from bottom to top. - Convert the fractional part to binary:
Multiply the fractional portion repeatedly by 2. Record the resulting integer digit (0or1) before each multiplication, and continue with the remaining fractional part until it reaches zero or forms a repeating cycle. - Normalize into binary scientific notation:
Combine the integer and fractional bits, then shift the binary point so that exactly one non-zero digit (1) sits to the left of the point:1.mantissa_bits × 2^E. The number of positions shifted defines the unbiased exponentE. (Shifting left yields a positiveE; shifting right yields a negativeE). - Calculate the biased exponent (e):
Add the single-precision bias of127:e = E + 127. Converteinto an 8-bit unsigned binary number (00000000to11111111). - Extract the 23-bit mantissa and round:
Drop the implicit leading1.and take the next 23 fractional bits. If the fraction continues past 23 bits, apply the IEEE 754 default rounding rule (Round to Nearest, Ties to Even). - Assemble the 32-bit word and convert to Hex:
ConcatenateS(1 bit),e(8 bits), andM(23 bits). Group into 8 four-bit nibbles to write the hexadecimal representation.
Worked example 1: -13.625 (exact terminating value)
We convert -13.625 to 32-bit IEEE 754 single precision.
Step 1: Sign bit
-13.625 is negative, so S = 1.
Step 2: Integer part to binary
Convert 13:
- 13 ÷ 2 = 6, remainder 1
- 6 ÷ 2 = 3, remainder 0
- 3 ÷ 2 = 1, remainder 1
- 1 ÷ 2 = 0, remainder 1
Reading remainders from bottom to top: 13_10 = 1101_2.
Step 3: Fractional part to binary
Convert 0.625:
- 0.625 × 2 = 1.25 (bit 1, remainder 0.25)
- 0.250 × 2 = 0.50 (bit 0, remainder 0.50)
- 0.500 × 2 = 1.00 (bit 1, remainder 0.00)
The fractional remainder terminates at 0. Reading top to bottom: 0.625_10 = 0.101_2.
Step 4: Normalize to scientific notation
Combine integer and fraction:13.625_10 = 1101.101_2
Shift the binary point 3 places to the left to leave a single leading 1:1101.101_2 = 1.101101_2 × 2^3
The unbiased exponent is E = 3.
Step 5: Biased exponent
Add the bias of 127:e = E + 127 = 3 + 127 = 130
Convert 130 to 8-bit unsigned binary:130 = 128 + 2 = 10000010_2
Step 6: Mantissa field
Drop the leading 1. to obtain 101101. Pad with seventeen trailing zeros to fill 23 bits:M = 10110100000000000000000
Because the binary fraction terminated naturally within 6 bits, no rounding is necessary. The absolute rounding error is exactly 0.
Step 7: Assemble 32-bit word and hexadecimal
Join the three fields:
- Sign (1 bit):
1 - Exponent (8 bits):
10000010 - Mantissa (23 bits):
10110100000000000000000
Full 32-bit binary string:1100 0001 0101 1010 0000 0000 0000 0000
Convert each 4-bit nibble to hexadecimal:
1100= C0001= 10101= 51010= A0000= 00000= 00000= 00000= 0
Result: 0xC15A0000 (exact stored decimal: -13.625).
Worked example 2: 0.1 (repeating fraction and rounding)
A common misconception among beginner programmers is that 0.1 is a simple, clean number. In base 10, 0.1 terminates because 10 is divisible by 5. In base 2, however, any fraction whose denominator is not a pure power of 2 becomes an infinite repeating sequence.
Step 1: Sign bit
0.1 is positive, so S = 0.
Step 2: Integer part to binary
The integer part is 0, so 0_10 = 0_2.
Step 3: Fractional part to binary
Multiply 0.1 repeatedly by 2:
- 0.1 × 2 = 0.2 (bit 0)
- 0.2 × 2 = 0.4 (bit 0)
- 0.4 × 2 = 0.8 (bit 0)
- 0.8 × 2 = 1.6 (bit 1, remainder 0.6)
- 0.6 × 2 = 1.2 (bit 1, remainder 0.2)
- 0.2 × 2 = 0.4 (bit 0) — cycle begins!
- 0.4 × 2 = 0.8 (bit 0)
- 0.8 × 2 = 1.6 (bit 1)
- 0.6 × 2 = 1.2 (bit 1)
The four-bit sequence 0011 repeats indefinitely:0.1_10 = 0.000110011001100110011..._2
Step 4: Normalize to scientific notation
Shift the binary point 4 places to the right to reach the first 1:0.0001100110011..._2 = 1.10011001100110011001100 110011..._2 × 2^(-4)
The unbiased exponent is E = -4.
Step 5: Biased exponent
Add the bias of 127:e = E + 127 = -4 + 127 = 123
Convert 123 to 8-bit binary:123 = 64 + 32 + 16 + 8 + 2 + 1 = 01111011_2
Step 6: Mantissa extraction and rounding
Let us examine the bits following the implicit leading 1.:
Fraction bits: 1001 1001 1001 1001 1001 100 | 1 10011...
^------- 23 bits -------^ | ^- discarded bits
- The 23 bits stored in the mantissa field are:
10011001100110011001100. - The first discarded bit (the guard bit at position 24) is
1. - The subsequent discarded bits (round bit
1, followed by sticky bits0011...) are non-zero.
Under the IEEE 754 default rounding mode, Round to Nearest, Ties to Even:
Since the discarded fraction (.110011..._2) is strictly greater than half of the least significant bit (> 0.5 ULP), we must round up by adding 1 to the 23-bit mantissa:
1001 1001 1001 1001 1001 100
+ 1
-------------------------------
1001 1001 1001 1001 1001 101
The rounded 23-bit mantissa is 10011001100110011001101.
Step 7: Assemble 32-bit word and hexadecimal
- Sign (1 bit):
0 - Exponent (8 bits):
01111011 - Mantissa (23 bits):
10011001100110011001101
Full 32-bit binary string:0011 1101 1100 1100 1100 1100 1100 1101
Convert to hexadecimal nibbles:
0011= 31101= D1100= C1100= C1100= C1100= C1100= C1101= D
Result: 0x3DCCCCCD.
What decimal value is physically stored inside the chip?
Stored value = 1.10011001100110011001101_2 × 2^(-4)
= 0.100000001490116119384765625
Because of rounding up, the stored single-precision float is slightly larger than 0.1, with an absolute error of +1.490116 × 10^-9.
Why 0.1 + 0.2 != 0.3 in modern programming languages
One of the first surprises developers face in JavaScript, Python, C++, or Java is this expression evaluating to false:
0.1 + 0.2 === 0.3 // false!
// 0.1 + 0.2 outputs: 0.30000000000000004
Now that you understand floating-point representation, the exact mathematical explanation is clear. Most programming environments use 64-bit double precision (binary64) for standard numbers, which has 53 bits of significand precision (52 stored mantissa bits + 1 hidden bit) and an exponent bias of 1023.
Here is the exact binary breakdown computed according to the IEEE 754 standard:
| Decimal input | 64-bit Hexadecimal | Exact stored decimal representation |
|---|---|---|
| 0.1 | 0x3FB999999999999A |
0.1000000000000000055511151231257827021181583404541015625 |
| 0.2 | 0x3FC999999999999A |
0.200000000000000011102230246251565404236316680908203125 |
| 0.1 + 0.2 (sum) | 0x3FD3333333333334 |
0.3000000000000000444089209850062616169452667236328125 |
| 0.3 (literal) | 0x3FD3333333333333 |
0.299999999999999988897769753748434595763683319091796875 |
Why the difference occurs:
- Neither
0.1nor0.2can be represented with finite binary bits. Both round up slightly when fitted into 53 significand bits. - When the CPU aligns exponents and adds their mantissas, the small rounding errors compound. The sum rounds up to
0x3FD3333333333334, which evaluates to0.3000000000000000444.... - However, the standalone literal
0.3rounds down to0x3FD3333333333333, which evaluates to0.2999999999999999888.... - The two bit patterns differ in their least significant bit by 1 ULP (Unit in the Last Place), or
2^(-54) ≈ 5.55 × 10^-17. Because the CPU compares raw binary patterns,0.1 + 0.2 === 0.3evaluates tofalse.
How to compare floats safely in code
Never use strict equality (== or ===) on calculated floating-point numbers. Instead, use an epsilon comparison:
// Recommended floating-point equality test
function areClose(a, b, epsilon = Number.EPSILON) {
return Math.abs(a - b) < epsilon;
}
console.log(areClose(0.1 + 0.2, 0.3)); // true
For financial applications, never use raw floating-point numbers. Instead, store money in integer cents or use an arbitrary-precision decimal library like Python's decimal.Decimal or Java's BigDecimal.
Special values in IEEE 754
IEEE 754 reserves specific bit patterns in the exponent field (00000000 and 11111111 in single precision) for special values:
| Classification | Exponent bits (e) | Mantissa bits (M) | Sign bit (S) | Value represented |
|---|---|---|---|---|
| Positive Zero | 00000000 |
All 0s |
0 |
+0.0 |
| Negative Zero | 00000000 |
All 0s |
1 |
-0.0 |
| Positive Infinity | 11111111 |
All 0s |
0 |
+Infinity (e.g., 1.0 / 0.0) |
| Negative Infinity | 11111111 |
All 0s |
1 |
-Infinity (e.g., -1.0 / 0.0) |
| Not-a-Number (NaN) | 11111111 |
At least one non-zero bit | 0 or 1 |
NaN (e.g., 0.0 / 0.0 or sqrt(-1)) |
| Subnormal numbers | 00000000 |
Non-zero | 0 or 1 |
(-1)^S × (0.M) × 2^(-126) |
Subnormal (or denormal) numbers allow gradual underflow near zero. By switching the implicit leading bit from 1 to 0 and fixing the exponent at -126, the processor avoids an abrupt drop to zero when numbers become smaller than the minimum normal float (≈ 1.175494 × 10^-38).
You can test any custom value, inspect its bit-level fields, and observe real-time rounding errors using our IEEE 754 Converter.
Frequently asked questions
Why does IEEE 754 use an exponent bias of 127 instead of two's complement?
A biased exponent converts signed exponent values (-126 to +127) into strictly non-negative integers (1 to 254). This enables fast integer comparison hardware to compare the magnitude of two floating-point numbers directly without needing separate floating-point subtraction circuitry.
What is the hidden or implicit leading bit?
In binary scientific notation, every non-zero normalized number begins with a leading digit of 1 (for example, 1.101 × 2^3). Because this leading 1 is always present, IEEE 754 omits it from storage, saving 1 bit of memory and effectively providing 24 bits of precision from 23 physical mantissa bits.
Why does 0.1 become an infinite repeating fraction in binary?
A decimal fraction terminates in a given number base if and only if all prime factors of the denominator divide the base. In base 10 (prime factors 2 and 5), 1/10 terminates as 0.1. In base 2 (prime factor 2 only), dividing by 10 introduces a factor of 5 that cannot be expressed as a finite power of 2, resulting in the infinite repeating sequence 0.0001100110011..._2.
How does Round to Nearest, Ties to Even work?
When the discarded fraction is less than half a unit in the last place (ULP), the number rounds down. When greater than half an ULP, it rounds up. When the discarded bits equal exactly half an ULP (a tie), the algorithm rounds to whichever candidate has an even least significant bit (0). This prevents statistical drift and accumulation bias during repeated arithmetic operations.
What is the difference between single precision and double precision?
Single precision (binary32) uses 32 bits (1 sign, 8 exponent, 23 mantissa) with a bias of 127, providing about 7 decimal digits of precision. Double precision (binary64) uses 64 bits (1 sign, 11 exponent, 52 mantissa) with a bias of 1023, providing about 15 to 17 decimal digits of precision.
Why are there both +0 and -0 in IEEE 754?
Because IEEE 754 uses signed-magnitude representation rather than two's complement, the sign bit is separate from the magnitude. When an underflow occurs from a negative direction (such as -1.0 / Infinity), the sign bit remains 1, producing -0.0. While +0.0 == -0.0 evaluates to true in most languages, operations like 1.0 / +0.0 produce +Infinity whereas 1.0 / -0.0 produce -Infinity.
What is the smallest positive normalized number in single precision?
The smallest normalized single-precision float has an exponent bit pattern of 00000001 (biased exponent 1, unbiased exponent 1 - 127 = -126) and an all-zero mantissa (1.0 × 2^(-126)), which equals approximately 1.17549435 × 10^-38.
What is the largest finite number in single precision?
The largest finite single-precision float has an exponent bit pattern of 11111110 (biased exponent 254, unbiased exponent 254 - 127 = 127) and an all-ones mantissa ((2 - 2^(-23)) × 2^127), which equals approximately 3.40282347 × 10^38 (Hex 0x7F7FFFFF).
Does 0.1 + 0.2 equal 0.3 in single precision?
Interestingly, in 32-bit single precision, (float)0.1 + (float)0.2 == (float)0.3 evaluates to true on many systems because the 23-bit rounding errors happened to cancel out favorably. However, neither (float)0.1 nor (float)0.2 nor their sum equals the exact decimal value 0.3; all of them represent 0.300000011920928955.... In 64-bit double precision, the cancellation does not occur, producing false.
How many decimal digits of precision does IEEE 754 single precision have?
Single precision provides 24 bits of binary significand precision (including the hidden leading bit). Since 24 × log10(2) ≈ 7.22, single-precision floats guarantee between 6 and 9 significant decimal digits of precision depending on the specific number.
Use the Binary Calculator to practice the accompanying binary arithmetic. For a related walkthrough, read Cache Mapping Explained: Tag, Index, Offset.