While I was learning about range()
function, I got curious whether I can use range()
for float
numbers. Stumbled upon below:
i = 0.0
while i <= 1.0:
print(i)
i = i + 0.1
When I tried to run it, it did not give the result I expected. Why does it add slightly more/less than 0.1 in some steps? Want to know why python behaves like that to avoid this kind of issues in future.
The result:
> 0.1
> 0.2
> 0.30000000000000004
> 0.4
> 0.5
> 0.6
> 0.7999999999999999
> 0.8999999999999999
> 0.9999999999999999