Understanding how floats (floating-point numbers) work in a computer can be a bit tricky at first, but I'll break it down step by step in a way that's easy to grasp. We'll cover what floating-point numbers are, how they're represented in a computer, and some of the quirks and limitations you should be aware of.
1. What Are Floating-Point Numbers?
Floating-point numbers are a way to represent real numbers (numbers with decimal points, like 3.14, -0.001, or 1.5e10) in a computer. Unlike integers, which are whole numbers and can be represented exactly, real numbers often have fractional parts, and we need a way to store both very large and very small numbers efficiently.
The term "floating-point" comes from the idea that the decimal (or binary) point "floats" to different positions in the number, depending on an exponent. This is similar to scientific notation in mathematics. For example:
The number 1234.56 can be written in scientific notation as ( 1.23456 \times 10^3 ).
Similarly, in a computer, floating-point numbers are stored in a binary version of this format.
2. How Are Floats Represented in a Computer?
Computers use a standard called IEEE 754 to represent floating-point numbers. This standard defines how floats are stored in memory using a fixed number of bits. The most common formats are:
Single precision (32 bits): Used for float in many programming languages.
Double precision (64 bits): Used for double in many programming languages.
A floating-point number in IEEE 754 format is broken into three parts:
Sign bit (S): Indicates whether the number is positive or negative.
Exponent (E): Determines the scale (magnitude) of the number.
Mantissa (M) (also called the significand): Represents the significant digits of the number.
Here’s how these parts are laid out in memory:
Single Precision (32-bit float):
1 bit for the sign (S).
8 bits for the exponent (E).
23 bits for the mantissa (M).
Double Precision (64-bit float):
1 bit for the sign (S).
11 bits for the exponent (E).
52 bits for the mantissa (M).
3. Breaking Down the Representation
Let’s dive into how these three parts work together to represent a number.
a) Sign Bit (S)
The sign bit is 0 for positive numbers and 1 for negative numbers.
For example, if the sign bit is 1, the number is negative; if it’s 0, the number is positive.
b) Exponent (E)
The exponent determines the scale of the number (how large or small it is).
It is stored in a biased form, meaning a fixed value (called the bias) is added to the actual exponent to ensure it is always positive when stored. This makes comparisons easier.
For single precision, the bias is 127.
For double precision, the bias is 1023.
The stored exponent is the actual exponent plus the bias. For example, in single precision, an actual exponent of 3 is stored as ( 3 + 127 = 130 ).
c) Mantissa (M)
The mantissa represents the significant digits of the number.
It is stored as a binary fraction, but with an implicit leading 1 (called normalization). This means the mantissa is assumed to start with a 1, and only the fractional part after the 1 is stored in the 23 bits (for single precision).
For example, the number 1.101 in binary has a mantissa of 101 (the part after the 1).
d) Putting It All Together
The value of a floating-point number is calculated as:
[
\text{Value} = (-1)^{\text{sign}} \times 1.\text{mantissa} \times 2^{\text{exponent} - \text{bias}}
]
For example, let’s represent the number 5.5 in single precision:
Convert 5.5 to binary:
5 in binary is 101.
0.5 in binary is 0.1.
So, 5.5 in binary is 101.1.
Normalize the binary number:
Move the binary point so there’s only one digit to the left: ( 1.011 \times 2^2 ).
Here, the actual exponent is 2.
Determine the sign bit:
Since 5.5 is positive, the sign bit is 0.
Bias the exponent:
For single precision, the bias is 127, so the stored exponent is ( 2 + 127 = 129 ).
129 in binary is 10000001.
Store the mantissa:
The mantissa is the fractional part of 1.011, which is 011.
Pad it with zeros to fill 23 bits: 01100000000000000000000.
Combine the parts:
Sign bit: 0
Exponent: 10000001
Mantissa: 01100000000000000000000
Final 32-bit representation: 0 10000001 01100000000000000000000.
4. Precision and Limitations
Floating-point numbers are an approximation of real numbers, and this leads to some important limitations:
a) Finite Precision
Because the mantissa has a limited number of bits (23 for single precision, 52 for double precision), not all real numbers can be represented exactly.
For example, numbers like 0.1 (a simple decimal fraction) cannot be represented exactly in binary, leading to small rounding errors. This is why you might see results like 0.1 + 0.2 != 0.3 in many programming languages (it might be something like 0.30000000000000004 instead).
b) Range
The exponent allows floating-point numbers to represent very large or very small numbers, but there are limits.
For single precision, the range is approximately ( \pm 3.4 \times 10^{38} ).
For double precision, the range is approximately ( \pm 1.8 \times 10^{308} ).
c) Special Values
The IEEE 754 standard defines special values to handle edge cases:
Zero: Represented with an exponent of 0 and a mantissa of 0. There are both +0 and -0, depending on the sign bit.
Infinity: Represented with an exponent of all 1s and a mantissa of 0. There are both +∞ and -∞.
NaN (Not a Number): Represented with an exponent of all 1s and a non-zero mantissa. Used for undefined results, like ( 0/0 ) or ( \sqrt{-1} ).
5. Why Do We Use Floating-Point Numbers?
Floating-point numbers are used because they provide a good balance between:
Range: They can represent very large and very small numbers.
Precision: They can represent many digits of precision, though not infinite precision.
Efficiency: They use a fixed amount of memory (32 or 64 bits), making them practical for hardware and software.
However, if you need exact decimal arithmetic (e.g., for financial calculations), you might want to use a different representation, like a decimal type or fixed-point arithmetic, to avoid rounding errors.
6. Common Pitfalls and Tips
Here are some things to keep in mind when working with floats:
Avoid equality comparisons: Due to rounding errors, comparing floats directly with == can be unreliable. Instead, check if the difference between two numbers is smaller than a tiny threshold (e.g., abs(a - b) < 0.000001).
Understand precision limits: Single precision floats have about 7 decimal digits of precision, while double precision floats have about 15–16 digits.
Use doubles for more precision: If you’re working with very small or very large numbers, or need more precision, use double instead of float.
7. Example in Code
Here’s a quick example in Python to illustrate floating-point quirks:
python
# Floating-point addition
a = 0.1
b = 0.2
c = a + b
print(c) # Output: 0.30000000000000004 (not exactly 0.3!)
# Comparing floats
if c == 0.3:
print("Equal")
else:
print("Not equal") # This will print "Not equal"
# Better comparison
epsilon = 1e-10
if abs(c - 0.3) < epsilon:
print("Close enough") # This will print "Close enough"
8. Summary
Floating-point numbers are a way to represent real numbers in a computer using a fixed number of bits, following the IEEE 754 standard.
They are stored in three parts: sign, exponent, and mantissa.
They are an approximation, so they have limitations in precision and can lead to rounding errors.
Understanding these limitations is crucial when working with floats in programming, especially for tasks requiring high precision.
If you have more specific questions or want to dive deeper into any part of this, feel free to ask!