Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

In the common Python binary64 case, 0.1 and 0.2 are stored as nearby binary fractions, not as exact tenths. The computer adds those stored values and rounds the result to the floating-point format; Python typically displays it as 0.30000000000000004. This is expected finite-precision behavior, not a failure of ordinary addition.

Why can’t a computer store 0.1 exactly?

Binary fractions are sums of powers of two. A fraction has a finite binary expansion only when its reduced denominator is a power of two. Since one tenth is 1/10, whose denominator includes a factor of 5, its binary expansion repeats indefinitely. A finite binary floating-point format must therefore use a nearby representable value.

In the common Python binary64 case, the float nearest to 0.1 is exactly 3602879701896397 / 2**55, or 0.1000000000000000055511151231257827021181583404541015625 in decimal. This is the exact rational value represented by that float—not exactly one tenth. Python documents binary64 as having 53 bits of precision on almost all platforms, though this is not a guarantee about every language, machine, or numeric type. Python’s floating-point tutorial explains the representation and its limitations.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What happens to 0.1 + 0.2 in memory?

  1. Parsing: The source text 0.1 is converted to the nearest representable binary64 value. The same happens to 0.2.
  2. Addition: The operation adds those stored values, rather than the exact mathematical fractions 1/10 and 2/10.
  3. Rounding: The mathematical sum of the stored values is rounded to a value representable in the destination floating-point format.
  4. Formatting: Python converts that result to a short decimal display that can be converted back to the same float. In this case the display is 0.30000000000000004.

The extra digits in that output are not decimal characters stored inside a float. The value is stored in binary floating-point form; the decimal text is generated for display. For technical background on floating-point rounding, see David Goldberg’s paper on floating-point arithmetic.

Why does Python sometimes print just 0.1?

Many decimal strings can convert to the same floating-point value. Python selects a short representation that preserves round-trip reconstruction: converting the displayed text back to a float produces the same value. Thus 0.1 is a convenient, faithful label for the stored float, but it does not mean that the stored fraction is exactly 1/10. Display formatting changes how a value looks; it does not make the underlying approximation exact.

Is floating-point arithmetic broken?

No. This is a consequence of representing numbers with finite precision in a binary format. As Python’s documentation puts it, “This is in the very nature of binary floating point: this is not a bug in Python, and it is not a bug in your code either.” The practical lesson is to choose a numeric representation and comparison method that fit the problem.

Which approach should you use?

Need Suitable approach What to account for
Decimal-domain rules, such as prescribed monetary rounding Decimal arithmetic with an explicit scale and rounding policy Define the business rounding rules and decimal context.
Scientific or engineering computation Binary floating point is often useful Finite precision and rounding are part of the calculation; assess errors for the algorithm and the decision being made.

For decimal rules, use Decimal deliberately

Python’s decimal module supports decimal floating-point arithmetic and can represent decimal inputs such as 0.1 exactly within its model. It is useful when calculations must follow decimal rules, but you still need to choose the relevant rounding policy. Construct a Decimal from the original decimal text when that is the intended value: converting an already-created float preserves that float’s exact binary value, including its approximation, rather than recovering the original text. See the Python Decimal documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For approximate calculations, compare with an appropriate tolerance

Exact equality is often not the right test when a result is an approximation. Python’s math.isclose can compare values using relative and absolute tolerances, but the tolerances should reflect the scale, accumulated error, and consequences of the decision. A single generic epsilon is not suitable for every magnitude or algorithm.

Rounding inputs first does not fix the underlying representation issue: for example, rounding a float to one decimal place does not turn it into exact one tenth. Decide how to represent and compare values based on the requirements of the calculation, rather than relying on display formatting to change the stored value.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Where to learn more

For a more technical treatment of IEEE 754 representation, correctly rounded arithmetic, exceptions, conditioning, and stability, see Michael L. Overton’s Numerical Computing with IEEE Floating Point Arithmetic, second edition, published by SIAM in 2025. SIAM’s book page provides publication details.

Rank #4

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.