单独语句和组合语句的Python模行为不同?

2024-09-29 02:22:21 发布

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

这是怎么回事

print 'steps divided',125**(1.0/3), 5.0%1 
   >> steps divided 5.0 0.0
print 'steps combined',(125**(1.0/3))%1
   >> steps combined 1.0

奇怪的是,这些步骤适用于其他数字。。。。例8:

print 'steps divided',8**(1.0/3), 2.0%1
   >> steps divided 2.0 0.0
print 'steps combined',(8**(1.0/3))%1
   >> steps combined 0.0

Tags: 步骤数字stepsprintcombineddivided
1条回答
网友
1楼 · 发布于 2024-09-29 02:22:21

我认为这解决了你的问题

>>> 125**(1.0/3), 5.0%1 
(4.999999999999999, 0.0)
>>> 4.999999999999%1
0.9999999999989999

125**(1.0/3)实际上是4.999...,而不是5.0

因此,您正在执行4.999999999999999%1,这将为您提供.0.9999999999989999的输出,它将四舍五入为1

相关问题 更多 >