正数和负数的地板函数不对称

2024-04-25 18:23:54 发布

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

我正在使用Python3.4.3。math.floor函数为正数和负数给出了不同的答案。例如:

>>> math.floor (19.8)
19

>>> math.floor (-19.8)
-20

为什么我的答案会有这么大的差别?


Tags: 函数答案math负数floor正数
3条回答

根据documentation

Return the floor of x as a float, the largest integer value less than or equal to x.

math.floor()总是返回最接近的整数值。记住这一点,-20<-19.8<-19So-20按预期返回。

另一方面,对于正整数,比如5.5,5<5.5<6,所以math.floor()在这里返回5。

原因是地板的功能减弱了。所以19.8发子弹减至19发,而-19.8发子弹减至-20发,因为-20小于-19.8发。

floorgreatest integer函数,表示小于给定值的最大整数。

-20 < -19.5 < -19   --> -20 is the greatest integer less than -19.5
 19 <  19.5 <  20   -->  19 is the greatest integer less than  19.5

相关问题 更多 >