Floating point calculations are how computers represent and work with real numbers that have a fractional part, using a fixed number of bits to store a wide range of values. When you enter 0.1 + 0.2 into a calculator, the result is rarely exactly 0.3, and this article explains why that happens, how the system works, and how to handle it correctly.
Formula
The calculator above shows the core issue: the first output is the raw IEEE 754 double-precision sum, and the second is the same sum rounded to 12 decimal places. The formula for floating point addition is not a simple algebraic one; it is:
result = round_to_nearest( (significand_a × 2exponent_a) + (significand_b × 2exponent_b) )
Where:
- significand – the fractional part, normalized to be between 1 and 2 (for normal numbers).
- exponent – an integer power of two that scales the significand.
- round_to_nearest – the rounding mode that picks the closest representable value.
Worked Example
Using the default values: a = 0.1, b = 0.2.
- Convert 0.1 to binary: it is 0.00011001100110011… repeating (the pattern 1100 repeats forever). Stored as a double, it becomes 0.1000000000000000055511151231257827.
- Convert 0.2 to binary: it is 0.0011001100110011… repeating. Stored, it becomes 0.20000000000000001110223024625156504.
- Add the two stored values: 0.10000000000000000555 + 0.20000000000000001110 = 0.3000000000000000444089209850062616.
- Round to the nearest double: the result is 0.30000000000000004440892098500626.
- The calculator displays this raw result (0.30000000000000004) and then the rounded version (0.3).
Background: Why Not Just Use Decimals?
The earliest computers used fixed-point arithmetic, where the decimal point was in a fixed position, limiting range. Floating point, first standardized in IEEE 754 (1985), allows the decimal point to “float” by using an exponent, just like scientific notation (e.g., 6.022 × 1023). This gives a huge range—from about 10-308 to 10308 in double precision—while keeping the storage size constant (64 bits for a double). The trade-off is that not every real number can be represented exactly; only about 264 distinct values exist in that range.
Most programming languages (C, C++, Java, Python, JavaScript) use IEEE 754 doubles by default for fractional numbers. Some languages also offer single precision (32-bit, about 7 decimal digits) and half precision (16-bit, used in graphics). Decimal floating point (like Python’s decimal module or Java’s BigDecimal) stores numbers as base-10 digits, which avoids binary conversion errors but is slower and uses more memory.
How It Works: Sign, Exponent, Significand
A double-precision number uses 64 bits divided into three parts:
- 1 bit for the sign (positive or negative).
- 11 bits for the exponent (biased by 1023, so it can represent powers of two from about 2-1022 to 21023).
- 52 bits for the significand (the fraction, with an implicit leading 1 for normal numbers).
For example, the number 1.5 in binary is 1.1 × 20. The significand stores “1.1” (the leading 1 is implied), and the exponent stores 0 (biased to 1023). The number 0.1, as shown, has a repeating binary expansion, so the 52-bit significand truncates it, introducing an error of about 5.5 × 10-17.
Special values exist: infinity (exponent all 1s, significand 0), NaN (Not a Number, exponent all 1s, significand nonzero), and denormals (exponent all 0s) which allow numbers closer to zero than the normal minimum.
Common Problems and How to Avoid Them
The most visible problem is the classic 0.1 + 0.2 ≠ 0.3. Other issues include:
- Catastrophic cancellation: subtracting two nearly equal numbers loses precision. For example, 1.0000000000000002 – 1.0 gives 2.22e-16, but the result only has about one digit of accuracy.
- Accumulated rounding error: adding many small numbers to a large one can lose the small contributions. Summing 10 million values of 0.1 starting from 0 gives 999999.9998389754, not 1000000.
- Comparing floats for equality: using
if (x == 0.3)fails. Instead, compare with a tolerance:if (abs(x - 0.3) < 1e-9). - Overflow and underflow: multiplying two large numbers can exceed the maximum exponent, giving infinity; dividing by a very small number can underflow to zero.
Practical mitigations: use double precision unless memory is critical; use Kahan summation for long sums; use decimal types for financial calculations; and always round only at the final output stage, not in intermediate steps.
How to Choose: Float, Double, or Decimal?
Your choice depends on the application:
- Float (32-bit): ~7 decimal digits of precision. Use for graphics, machine learning inference, and anywhere memory or bandwidth is tight and small errors are acceptable.
- Double (64-bit): ~15-16 decimal digits. The default for scientific computing, engineering, and most general-purpose math. Use unless you have a specific reason not to.
- Decimal (base-10): Exact representation of decimal fractions like 0.1. Use for money, accounting, and any situation where the decimal representation matters legally or contractually. Slower, but correct.
- Rational or arbitrary precision: For symbolic math or when you need exact fractions (e.g., 1/3), use libraries like Python’s
fractionsor GMP.
A good rule: if you are adding or comparing monetary amounts, use decimal. If you are doing physics or graphics, double is fine. If you are writing a game shader, float is standard.
Comparisons: IEEE 754 vs. Alternatives
IEEE 754 is not the only floating point format. The main alternatives:
- Fixed point: stores a scaled integer (e.g., value × 1000). No rounding error for addition/subtraction, but limited range and multiplication needs care. Used in embedded systems without FPUs.
- Posit / unum: a newer format with tapered precision, giving more accuracy near 1 and a wider dynamic range. Not widely adopted in mainstream hardware.
- BFloat16: 16-bit with 8-bit exponent, used in AI training to save memory while keeping range. Less precision than half-float.
- Decimal floating point (IEEE 754-2008): stores base-10 digits, used in financial systems. Slower but avoids binary-to-decimal conversion issues.
Safety and Accuracy Limits
Floating point is not a safety-critical arithmetic system. In applications like aircraft control, medical devices, or nuclear reactor monitoring, you must not rely on a single floating point calculation without validation. The errors are deterministic, so you can analyze them, but the analysis is non-trivial. For critical systems, use integer arithmetic with known bounds, or formal verification tools. Even in non-critical work, be aware that a result like “0.30000000000000004” is not a bug in your calculator—it is the correct IEEE 754 result. Never use floating point for counting things (use integers), for exact equality checks, or for financial totals that must match to the cent.
Frequently Asked Questions
Why does 0.1 + 0.2 equal 0.30000000000000004?
Because 0.1 and 0.2 cannot be represented exactly in binary (they are repeating fractions in base 2). The stored approximations, when added, produce a result that is slightly above 0.3. The error is about 4.4 × 10-16, which is normal for double precision.
Is floating point math “wrong”?
No, it is correct under the IEEE 754 standard. The standard defines exactly how each operation should round to the nearest representable value. The result is the closest possible approximation given the finite number of bits. It is not a bug; it is a trade-off between range, precision, and speed.
How many decimal digits can a double store?
About 15 to 17 significant decimal digits. The 52-bit significand gives 252 ≈ 4.5 × 1015 distinct values, so you can reliably round-trip 15 digits, and often 16, but not 17. Always round to 15 digits when displaying to avoid misleading precision.
Should I use float or double for my project?
Use double unless you have a measured memory or performance problem. Doubles are the default in most languages and libraries, and the precision difference is often critical. Floats are for graphics where 7 digits is enough and memory is a real constraint.
How do I compare two floats for equality?
Never use ==. Instead, check if the absolute difference is smaller than a tolerance: abs(a - b) < 1e-9. For very large or very small numbers, use a relative tolerance: abs(a - b) <= eps * max(abs(a), abs(b)) where eps is about 1e-12 for doubles.
Can I avoid floating point errors entirely?
Yes, by using integers (for counts), rational numbers (for fractions like 1/3), or decimal types (for base-10 values). Each has costs: integers lack fractions, rationals can grow huge, decimals are slower. For most engineering and scientific work, the tiny errors in double precision are irrelevant.
Related
Formula used
in a | First number | - | 0.1 | -1000000 | 1000000 in b | Second number | - | 0.2 | -1000000 | 1000000 out sum | Sum (float64) | - | a + b | 17 out exact | Exact decimal sum | - | round((a + b) * 10^12) / 10^12 | 12
Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Upgrade MTG Foundations Commander Deck on a Budget
- Cardano Bridge Frozen Withdrawals: What to Do
- Pinterest Mood Board: What It Is & How to Make One
- Smoke Alarm Alerts via HomePod: Setup Guide
- Anti-Aliasing Explained: What It Is and How It Works
- Tree Shake Unused Endpoints from an Angular API Client