Why Does 0.1 + 0.2 Not Equal 0.3 in Python?
Open Python and type this: print ( 0.1 + 0.2 ) You'd expect 0.3 . Python gives you this: 0.30000000000000004 And it gets stranger: print ( 0.1 + 0.2 == 0.3 ) False If you're learning to code, this is the moment you start wondering whether you broke something. You didn't. Python is doing exactly what it was designed to do, and so are JavaScript, Java, C# and almost every other language. Try 0.1 +…
When you type `print(0.1 + 0.2)` in Python, you expect the output `0.3`. However, Python actually displays `0.30000000000000004`. This phenomenon is not unique to Python, but occurs in many other programming languages as well. The reason behind this discrepancy lies in how computers store and calculate numbers.
Computers operate using a binary system, which means they represent numbers in base 2. While humans typically count in base 10 (decimal), where each digit represents a power of 10 (ones, tens, hundreds, etc.), computers can only work with halves, quarters, eighths, sixteenths, and so on. While some numbers, like 0.5 (one half) and 0.25 (one quarter), can be precisely represented in binary, others like 0.1 (one tenth) cannot.
The decimal number 0.1 is represented in binary as an endless repeating pattern: `0.0001100110011001100110011...`. Computers have a fixed amount of space, usually 64 bits, to store each number. To fit the binary representation of 0.1 within this constraint, the computer stores the closest value it can, which is slightly more than 0.1.
When you add 0.1 and 0.2 in Python, the resulting value is even slightly more than 0.3. The computer stores this slightly-off value, and when it is printed, Python displays the shortest number that maps back to the stored value. In this case, it is `0.30000000000000004`.
This same issue arises in other programming languages, such as JavaScript, Java, and C#. The same principles apply, regardless of the language you are using.
To address this problem, there are a few strategies you can employ:
1. Round when displaying a result: If you only need to show the number, round it to the desired number of decimal places. For example, `round(total, 2)` will display `0.3` instead of `0.30000000000000004`.
2. Never compare floats using the `==` operator: Instead of checking for exact equality, use a tolerance level to determine if two floating-point numbers are close enough to be considered equal. For instance, `math.isclose(0.1 + 0.2, 0.3)` returns `True`, indicating that the two numbers are close enough to be deemed equal.
3. Use the `Decimal` module for precise decimal calculations: When working with money or other situations where exact decimal arithmetic is crucial, use the `decimal` module in Python. Pass the numbers as strings, like `Decimal('0.1') + Decimal('0.2')`, which will yield the expected result of `0.3`.
Understanding the limitations of floating-point arithmetic and adopting these best practices can help avoid unexpected results when working with decimal numbers in programming.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.