简单的脚本是在数学中加入随机零?

2024-09-28 01:26:25 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,所以我做了一个简单的脚本,因为我很懒,只想添加我的公里数,以便所需的信息弹出。。。虽然我知道将变量更改为int()可以提供所需的正确信息,但如果没有它,我会收到一个奇怪的输出,我现在知道它是如何创建的了。。以下是脚本:

KMstandNu = 186645



KMsnaarhuisenterug = 18.7 * 2

KMnaarPascalenterug = 8.2 * 2

Wednesday2 = KMstandNu - KMsnaarhuisenterug

Wednesday1 = Wednesday2 - KMnaarPascalenterug

Tuesday2 = Wednesday1

Tuesday1 = Tuesday2 - KMnaarPascalenterug



print("22 oktober: " + str(Tuesday1) + " - " + str(Tuesday2))

print("23 oktober: " + str(Wednesday1) + " - " + str(Wednesday2))

输出:

22 oktober: 186574.80000000002 - 186591.2
23 oktober: 186591.2 - 186607.6

好吧,那么哪个聪明的头脑能帮我弄清楚为什么星期二1得到9 0和2落后于正确的数学


Tags: 脚本信息intprint我会str头脑oktober
1条回答
网友
1楼 · 发布于 2024-09-28 01:26:25

因为floating point precision。尝试使用十进制而不是浮点:

decimal Python docs

related question

from decimal import getcontext, Decimal

getcontext().prec = 10

KMstandNu = 186645

KMsnaarhuisenterug = Decimal(18.7) * 2

KMnaarPascalenterug = Decimal(8.2) * 2

Wednesday2 = KMstandNu - KMsnaarhuisenterug

Wednesday1 = Wednesday2 - KMnaarPascalenterug

Tuesday2 = Wednesday1

Tuesday1 = Tuesday2 - KMnaarPascalenterug


print("22 oktober: " + str(Tuesday1) + " - " + str(Tuesday2))

print("23 oktober: " + str(Wednesday1) + " - " + str(Wednesday2))

输出:

22 oktober:186574.8000-186591.2000

23 oktober:186591.2000-186607.6000

相关问题 更多 >

    热门问题