我不明白为什么有些浮点数的末尾有一个非零数字。为什么会这样呢?我如何解决它?预先感谢!
n = 0
p1 = 1.20
p2 = 0.80
max_price = 10
def a_def(n):
if n > 0:
a = 1
else:
a = 0
return a
def price():
return a_def(n) * (p1 - p2) + n * p2
while price() <= max_price:
print(n, price())
n += 1
输出:
0 0.0
1 1.2
2 2.0
3 2.8000000000000003
4 3.6
5 4.4
6 5.200000000000001
7 6.0
8 6.800000000000001
9 7.6
10 8.4
11 9.200000000000001