0

Why pyton don't geav step 0.1 in this program?

>>> i = 3 while i >=0: print(i) i = i - 0.1

3rd Dec 2016, 8:02 PM
Tigran
Tigran - avatar
2 Respuestas
+ 1
Due to the way floats (decimals) are represented, they aren't quite precise. Think of it as how we can't represent 1/3 as a decimal (0.3333333333...). I'd recommend either using i = round(i - 0.1, 1) or using i = 30, print(i / 10), i = i - 1
4th Dec 2016, 2:49 AM
JayDepp
JayDepp - avatar
0
Well, thank you for answer... your recommend i=30 work... but! WHY?! When program run: i=30 while i>=0: print (i/10) i=i-1 It is positive (0.1, 0.0 at the end). But if just run: print (1/10) It is negative! 0.100000000000001. Or another... If 3-0.1 (3-1/10) then 2.8999999999999, but if (3-0.1)+0.1 ((3-1/10)+1/10) then 3.0 Why?! How to make serious calculations in this program or how to program a simple calculator in this case?
4th Dec 2016, 7:51 AM
Tigran
Tigran - avatar