我将通过示例解释我的问题:
>>> #In this case, I get unwanted result
>>> k = 20685671025767659927959422028 / 2580360422
>>> k
8.016582043889239e+18
>>> math.floor(k)
8016582043889239040
>>> #I dont want this to happen ^^, let it remain 8.016582043889239e+18
>>> #The following case though, is fine
>>> k2 = 5/6
>>> k2
0.8333333333333334
>>> math.floor(k2)
0
如何使math.floor不对科学计数法的数字进行下限? 是否有一个规则以科学计数法表示数字(我想这是一定的界限)。
编辑 :
我首先以为math.floor函数会导致精度下降,但事实证明,第一个计算本身会失去计算的精度,这让我感到非常困惑,可以在这里轻松看到:
>>> 20685671025767659927959422028 / 2580360422
8016582043889239040
>>> 8016582043889239040 * 2580360422
20685671025767659370513274880
>>> 20685671025767659927959422028 - 20685671025767659370513274880
557446147148
>>> 557446147148 / 2580360422
216.0342184739958
>>> ##this is >1, meaning I lost quite a bit of information, and it was not due to the flooring
所以现在我的问题是如何获得除法的实际结果。 但是由于某种原因,我没有得到相同的结果。
编辑:我在这里找到了除法精度问题的简单解决方案: 如何在Python中管理大数除法? 显然//运算符返回一个int而不是float ,它对机器内存没有大小限制。