At times simple arithmetic equations like 0.1+0.2 can give answers like 0.30000000000000004 instead of 0.3, in Python. Is Python broken then? No, this is not Python’s fault, it is the side effect of how floating numbers are represented in computers.
Description of the Problem
You’re doing basic math in Python, but the results don’t make sense:
print(0.1 + 0.2) # Expected: 0.3, Actual: 0.30000000000000004
Or:
print(1.2 - 1.0) # Expected: 0.2, Actual: 0.19999999999999996
It seems like Python is doing something wrong, but it’s not just Python. This issue also occurs in most modern programming languages, including JavaScript, Java, and C.
Why This Happens
Computers represent floating-point numbers in binary code. Most decimal fractions are difficult to be represented in a binary format. This results in small rounding errors.
Example:
- The number 0.1 can’t be precisely represented in binary floating-point format.
- So, 0.1 + 0.2 ends up being something like 0.30000000000000004.
This issue is part of the IEEE 754 standard used by almost all programming languages for floating-point representation. It’s not a bug in Python, it’s just how computers work at the binary level.
Steps to Resolve the Issue
Step 1: Use round() for Display or Comparison
When displaying results or comparing float values, use round():
result = 0.1 + 0.2
print(round(result, 2)) # 0.3
Step 2: Use math.isclose() for Comparisons
Instead of doing:
if result == 0.3:
Do this:
import math
if math.isclose(result, 0.3, rel_tol=1e-9): print("Close enough!")
This is especially helpful in unit tests or financial applications where precision matters.
Step 3: Use decimal.Decimal for High-Precision Arithmetic
Python’s decimal module allows decimal arithmetic with better precision and control:
from decimal import Decimal
a = Decimal('0.1')
b = Decimal('0.2')
print(a + b) # 0.3
This is particularly useful for financial calculations where exact decimal representation is critical.
Step 4: Use fractions.Fraction for Exact Rational Numbers
You can also use the fractions module when you want exact representations of fractions:
from fractions import Fraction
x = Fraction(1, 10)
y = Fraction(2, 10)
print(x + y) # 3/10
This avoids all floating-point inaccuracies entirely but may not be suitable for large-scale computations.
Floating-Point Arithmetic Best Practices In Python
Floating-point inaccuracies are a result of binary representation, not a Python-specific issue. While such differences can be neglected in most cases, they can cause serious distrup they can cause serious concerns in comparisons or important applications.
Best Practices:
- Use round() for formatting float values
- Use math.isclose() for safe float comparisons
- Use decimal.Decimal for financial or high-precision applications
- Use fractions.Fraction if exact arithmetic with ratios is required
Conclusion
Python isn’t broken, floating-point math just has its quirks due to binary representation. If you’re working in finance, data science, or anything where precision matters, it’s smart to handle float operations carefully. And if you’re looking to build accurate and reliable Python-based solutions, it’s a good idea to hire Python developers who understand these nuances.