python3中大数除法不一致

Kin*_*ing 2 python division factorial python-2.7 python-3.x

当我计算出 24! 使用数学库,结果与24相比不同!除以 25 计算!by 25.这是为什么?

>>> import math
>>> f=math.factorial(25)
>>> int(f/25)
620448401733239409999872
>>> math.factorial(24)
620448401733239439360000
>>> 
Run Code Online (Sandbox Code Playgroud)

rda*_*das 6

/执行“真正的除法”。结果是一个浮点数,它没有足够的精度来表示精确的商。调用int无法逆转精度损失。浮点数学和舍入中的错误导致了差异。

// 是整数除法 - 这就是你想要的:

>>> f = math.factorial(25)
>>> f/25
6.204484017332394e+23
>>> int(f/25)
620448401733239409999872
>>> math.factorial(24)
620448401733239439360000
>>> f//25
620448401733239439360000   # correct answer
Run Code Online (Sandbox Code Playgroud)