Why Does 0.1 + 0.2 Equal 0.30000000000000004?

One of the most surprising things beginners encounter in programming is that adding 0.1 and 0.2 can produce 0.30000000000000004 instead of exactly 0.3.

This is not usually a bug in the programming language. It is a consequence of how computers represent numbers internally. Most modern programming languages use a binary floating-point format based on the IEEE 754 standard, and many decimal fractions cannot be represented exactly in that format.

The Short Answer

The simplest explanation is this: computers commonly store floating-point numbers in binary, but 0.1 and 0.2 do not have finite exact representations in binary.

The computer therefore stores nearby approximations of those numbers. When those approximations are added together, the result can be slightly different from the exact mathematical value of 0.3.

Why Can Computers Not Store 0.1 Exactly?

To understand the problem, it helps to first look at how decimal and binary numbers work.

Decimal Numbers

Humans commonly use base 10. A decimal number such as 0.125 can be represented exactly because it can be written as a finite sum of powers of 10.

Binary Numbers

Computers commonly use base 2. A binary fraction uses powers of 2 instead of powers of 10.

Some decimal fractions have a finite binary representation, while others continue indefinitely. 0.1 is one of the fractions that requires an infinitely repeating binary representation.

A Simple Binary Example

The decimal number 0.5 can be represented exactly in binary because 0.5 is 1/2.

Similarly, 0.25 is 1/4, so it can also be represented exactly using a finite number of binary digits.

But 0.1 is 1/10. Because 10 contains a factor of 5, it cannot be represented as a finite fraction using only powers of 2.

The Same Thing Happens in Decimal

This problem is not unique to computers or binary. The limitation depends on the number system being used.

For example, if you tried to represent 1/3 using decimal notation, you would get 0.333333... continuing indefinitely. Decimal cannot represent one third exactly with a finite number of digits.

Binary floating-point has a similar issue with many decimal fractions such as 0.1.

What Is Floating-Point Representation?

Floating-point representation is a way of storing real-number approximations using a fixed amount of computer memory.

A common format is IEEE 754 double-precision floating point, which uses 64 bits to represent a number.

Sign

One part of the representation indicates whether the number is positive or negative.

Exponent

Another part stores an exponent that allows the representation to cover a very large range of values.

Significand

The remaining significant bits represent the precision of the number.

Why Is the Number Only an Approximation?

A floating-point format has a limited number of bits. It cannot store every possible real number exactly.

When a number cannot be represented exactly, the system stores the closest representable value according to the rules of the floating-point format and rounding mode.

What Happens to 0.1?

Instead of storing mathematical 0.1 exactly, a typical binary floating-point representation stores a value extremely close to 0.1.

The same happens with 0.2. Each stored value is very close to the intended decimal value but is not necessarily exact.

Then What Happens During Addition?

When the computer calculates 0.1 + 0.2, it adds the stored binary approximations rather than ideal mathematical decimal values.

The resulting value is very close to 0.3, but it can lie slightly above or below the exact mathematical result. When converted back to decimal and displayed with enough digits, that tiny difference may appear as 0.30000000000000004.

Is 0.30000000000000004 Really Wrong?

It is not a failure of arithmetic. The mathematical result of 0.1 + 0.2 is exactly 0.3. The displayed floating-point result reflects the limited precision of the representation used by the computer.

The important distinction is between the mathematical number and its finite computer representation.

Why Does the Output Show So Many Digits?

Programming languages and runtimes use different rules for displaying floating-point values. Some display a short representation that communicates the stored value, while others may show fewer or more digits.

The extra digits are not necessarily stored as a separate mysterious error. They are a decimal representation of the nearby floating-point value.

Floating-Point Error Is Usually Very Small

The difference between the floating-point result and the mathematical result is generally tiny for an operation such as 0.1 + 0.2.

However, small errors can accumulate when many floating-point operations are performed. The impact depends on the calculation and the number of operations involved.

Why Does Repeated Addition Matter?

Suppose a program repeatedly adds a small floating-point value inside a loop. Each operation can introduce a small rounding effect. After many operations, the accumulated difference can become large enough to affect the result.

This is especially important in simulations, scientific computing, graphics, financial calculations, and other systems where numerical precision matters.

Floating-Point Numbers Have Limited Precision

A 64-bit floating-point number cannot represent an unlimited number of significant digits. As numbers become larger, the distance between adjacent representable values can also become larger.

This means that floating-point arithmetic has limitations not only for decimal fractions such as 0.1, but also for very large values and calculations involving very different scales.

What Is Rounding Error?

Rounding error occurs when an exact mathematical value must be approximated because the available numerical representation cannot store it exactly.

Every individual rounding error may be extremely small, but repeated calculations can cause errors to accumulate or interact in unexpected ways.

Floating-Point Error vs Floating-Point Overflow

Floating-point precision problems should not be confused with overflow.

Precision problems occur when a value cannot be represented exactly. Overflow happens when a calculation produces a value outside the representable range of the floating-point format.

What Is IEEE 754?

IEEE 754 is a widely used standard for representing and performing arithmetic with floating-point numbers.

It defines formats, special values, rounding behavior, and rules for floating-point operations. Many common programming languages and hardware platforms use IEEE 754 floating-point formats.

What Are Special Floating-Point Values?

Floating-point systems can represent more than ordinary positive and negative numbers. They can also represent special values such as positive infinity, negative infinity, and NaN.

Infinity

Infinity can occur when an operation produces a result outside the representable finite range or when certain mathematical operations are performed.

NaN

NaN stands for Not a Number. It is used to represent results that do not correspond to an ordinary numerical value under the floating-point rules.

Why Does 0.3 - 0.2 Also Sometimes Look Strange?

Because 0.3 and 0.2 can each be approximated when stored as binary floating-point values, subtracting them can produce a result that is extremely close to 0.1 without being exactly the mathematical value of 0.1.

Should You Compare Floating-Point Numbers With ==?

Direct equality comparisons can be dangerous when two values are expected to be mathematically equal but have been produced through different floating-point calculations.

For calculations where small numerical differences are acceptable, developers often compare values using a tolerance rather than requiring exact equality.

What Is an Epsilon Comparison?

An epsilon comparison checks whether two floating-point values are close enough to be considered equal for a particular application.

For example, instead of asking whether two calculated values are exactly identical, a program can check whether their absolute difference is smaller than an appropriate tolerance.

The correct tolerance depends on the scale and requirements of the calculation. There is no single epsilon that is appropriate for every problem.

When Should You Use Decimal Arithmetic?

Applications that require exact decimal behavior may use decimal or fixed-point arithmetic instead of ordinary binary floating-point.

Financial software is a common example because monetary values often need predictable decimal rounding rules.

Using Integers for Money

Another common approach is to represent monetary values using the smallest required unit, such as cents, instead of storing them as floating-point dollars.

For example, instead of representing a price as 19.99, an application could represent it as 1999 cents. This avoids many binary floating-point problems for fixed-precision monetary values.

Does Using Integers Solve Every Precision Problem?

No. Integer arithmetic avoids this particular floating-point representation problem, but integers have their own limitations such as finite range and overflow.

The appropriate numerical representation depends on what the application is trying to calculate.

Why Don't Computers Just Store Decimal Numbers?

Binary floating-point is widely used because it provides an efficient representation with a large range of values and hardware support for fast arithmetic.

Using decimal representations can be useful for certain applications, but it involves different implementation and performance trade-offs.

Floating-Point Numbers Are Not Broken

Floating-point arithmetic is designed around a specific trade-off: limited memory is used to represent a very large range of numerical values with useful precision.

The surprising 0.1 + 0.2 result is a reminder that the number representation matters when performing numerical calculations.

Common Mistakes With Floating-Point Numbers

1. Expecting Exact Decimal Arithmetic

Developers should not assume that every decimal fraction has an exact representation in binary floating point.

2. Using Exact Equality Everywhere

Comparing floating-point results with exact equality can produce unexpected results when calculations involve rounding.

3. Ignoring Accumulated Error

Repeated calculations can amplify or accumulate small numerical differences.

4. Using Floating Point for Every Financial Calculation

Applications that require exact decimal monetary behavior should consider decimal or fixed-point approaches instead of automatically using binary floating point.

How Should Developers Handle Floating-Point Values?

1. Understand the Representation

Knowing that floating-point values are approximations makes many surprising results easier to understand.

2. Compare With Appropriate Tolerances

When approximate equality is acceptable, use a comparison method appropriate for the scale and numerical requirements of the problem.

3. Choose the Right Number Type

Use integers, decimal types, fixed-point representations, arbitrary-precision numbers, or floating-point values according to the needs of the application.

4. Round Only When Appropriate

Rounding can make displayed values easier to understand, but rounding intermediate calculations unnecessarily can also introduce additional error.

The Future of Numerical Computing

Modern processors, programming languages, libraries, and numerical systems continue to provide increasingly sophisticated tools for handling precision-sensitive calculations.

However, no numerical representation is perfect for every situation. Developers still need to understand the mathematical requirements of their applications and choose suitable data types and algorithms.

The reason 0.1 + 0.2 can produce 0.30000000000000004 is that the computer usually represents these decimal values as nearby binary floating-point approximations. Adding those approximations produces a value that is extremely close to 0.3 but not necessarily exactly equal to it.

The simplest way to understand the 0.1 + 0.2 problem is this: decimal fractions that look simple to humans are not always simple in binary. Because computers have a finite number of bits, some values must be approximated.

This is normal floating-point behavior, not a broken calculator. The important lesson is to understand the precision requirements of your application and choose an appropriate way to represent numbers.

Note: Tip: After learning why 0.1 + 0.2 behaves this way, explore IEEE 754, binary numbers, floating-point precision, rounding modes, integer overflow, fixed-point arithmetic, and numerical stability.