Explanation:
Line 1: 0.1 + 0.2
What happens?
Python adds the two floating-point numbers:
0.1 + 0.2
Expected mathematical result
0.3
Actual stored result
Because computers store floating-point numbers in binary, some decimal values cannot be represented exactly.
Internally:
0.1 ≈ 0.10000000000000000555...
0.2 ≈ 0.20000000000000001110...
So:
0.1 + 0.2
becomes approximately:
0.3000000000000000444...
Line 2: == 0.3
What happens?
Python compares:
0.3000000000000000444...
with
0.3
Internally, 0.3 is stored as:
0.2999999999999999888...
So Python checks:
0.3000000000000000444...
==
0.2999999999999999888...
Since these values are not exactly equal:
False
is produced.
Line 3: print(...)
What happens?
The print() function displays the result of the comparison.
print(False)
Output
False

0 Comments:
Post a Comment