问题描述
在 Python 2 中,floor() 返回一个浮点值.虽然对我来说不是很明显,但我找到了一些解释,阐明了为什么让 floor() 返回浮点数可能有用(对于像 float('inf') 和 <代码>浮动('nan')).
In Python 2, floor() returned a float value. Although not obvious to me, I found a few explanations clarifying why it may be useful to have floor() return float (for cases like float('inf') and float('nan')).
然而,在 Python 3 中,floor() 返回整数(并且对于前面提到的特殊情况返回溢出错误).
However, in Python 3, floor() returns integer (and returns overflow error for the special cases mentioned before).
那么 int() 和 floor() 现在有什么区别?
So what is the difference, if any, between int() and floor() now?
推荐答案
floor() 循环 down.int() 截断.使用负数时区别很明显:
floor() rounds down. int() truncates. The difference is clear when you use negative numbers:
>>> import math
>>> math.floor(-3.5)
-4
>>> int(-3.5)
-3
对负数进行四舍五入意味着它们远离 0,截断使它们更接近 0.
Rounding down on negative numbers means that they move away from 0, truncating moves them closer to 0.
换句话说,floor() 总是会低于或等于原始值.int() 将接近于零或等于.
Putting it differently, the floor() is always going to be lower or equal to the original. int() is going to be closer to zero or equal.
这篇关于Python 3 中的 int() 和 floor() 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)