问题:为什么Python的math.ceil()和math.floor()操作返回浮点数而不是整数?

有人可以解释一下吗(直接来自文档 -我的重点):

math.ceil(x)以浮点数形式返回x的上限,最小数值大于或等于x。

math.floor(x)以浮点数形式返回x的下限,即小于或等于x 的最大数值。

为什么会.ceil.floor回报花车当它们被定义应该算整数?


编辑:

好吧,这有一些很好的论据来说明为什么它们应该返回浮点数,而当@jcollado指出它们实际上确实在Python 3中返回整数时,我才刚刚习惯这个想法。

Can someone explain this (straight from the docs– emphasis mine):

math.ceil(x) Return the ceiling of x as a float, the smallest integer value greater than or equal to x.

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

Why would .ceil and .floor return floats when they are by definition supposed to calculate integers?


EDIT:

Well this got some very good arguments as to why they should return floats, and I was just getting used to the idea, when @jcollado pointed out that they in fact do return ints in Python 3…


回答 0

浮点数的范围通常超过整数的范围。通过返回浮点值,函数可以为超出可表示的整数范围的输入值返回有意义的值。

考虑:如果floor()返回整数,应该floor(1.0e30)返回什么?

现在,尽管Python的整数现在具有任意精度,但并不总是这样。标准库函数是等效C库函数的精简包装。

The range of floating point numbers usually exceeds the range of integers. By returning a floating point value, the functions can return a sensible value for input values that lie outside the representable range of integers.

Consider: If floor() returned an integer, what should floor(1.0e30) return?

Now, while Python’s integers are now arbitrary precision, it wasn’t always this way. The standard library functions are thin wrappers around the equivalent C library functions.


回答 1

正如其他答案所指出的那样,在python中,它们返回浮点数的原因可能是出于防止溢出问题的历史原因。但是,它们在python 3中返回整数。

>>> import math
>>> type(math.floor(3.1))
<class 'int'>
>>> type(math.ceil(3.1))
<class 'int'>

您可以在PEP 3141中找到更多信息。

As pointed out by other answers, in python they return floats probably because of historical reasons to prevent overflow problems. However, they return integers in python 3.

>>> import math
>>> type(math.floor(3.1))
<class 'int'>
>>> type(math.ceil(3.1))
<class 'int'>

You can find more information in PEP 3141.


回答 2

您的评论明显说明了您的困惑:

ceil / floor操作的重点是将浮点数转换为整数!

ceil和floor操作的重点是将浮点数据舍入为整数值。不做类型转换。需要获取数值的用户可以在操作之后进行显式转换。

请注意,如果所有可用的都是返回整数的ceil或float运算,则不可能简单地实现舍入到整数值。您需要首先检查输入是否在可表示的整数范围内,然后调用该函数;您将需要在单独的代码路径中处理NaN和无穷大。

此外,如果要符合IEEE 754,则必须具有返回浮点数的ceil和floor版本。

The source of your confusion is evident in your comment:

The whole point of ceil/floor operations is to convert floats to integers!

The point of the ceil and floor operations is to round floating-point data to integral values. Not to do a type conversion. Users who need to get integer values can do an explicit conversion following the operation.

Note that it would not be possible to implement a round to integral value as trivially if all you had available were a ceil or float operation that returned an integer. You would need to first check that the input is within the representable integer range, then call the function; you would need to handle NaN and infinities in a separate code path.

Additionally, you must have versions of ceil and floor which return floating-point numbers if you want to conform to IEEE 754.


回答 3

因为python的数学库是对返回浮点数的C数学库的精简包装。

Because python’s math library is a thin wrapper around the C math library which returns floats.


回答 4

在Python 2.4之前的版本中,整数不能容纳所有范围的截断实数。

http://docs.python.org/whatsnew/2.4.html#pep-237-unifying-long-integers-and-integers

Before Python 2.4, an integer couldn’t hold the full range of truncated real numbers.

http://docs.python.org/whatsnew/2.4.html#pep-237-unifying-long-integers-and-integers


回答 5

因为float的范围大于整数的范围-返回整数可能会溢出

Because the range for floats is greater than that of integers — returning an integer could overflow


回答 6

这是一个非常有趣的问题!因为浮点数需要一些位来存储指数(= bits_for_exponent),所以任何大于2**(float_size - bits_for_exponent)整数的浮点数始终是整数!在另一个极端与负指数的浮动会给之一10-1。这使整数范围浮点范围的讨论变得毫无意义,因为只要数字超出整数类型的范围,这些函数将简单地返回原始数字。python函数是函数的包装器,C因此这确实是C函数的不足之处,它们应该返回整数并强制程序员执行范围NaN//Inf在调用ceil / floor之前检查。

因此,逻辑上的答案是这些函数唯一有用的函数,它们将返回整数范围内的值,因此,它们返回浮点数的事实是一个错误,您非常聪明地意识到这一点!

This is a very interesting question! As a float requires some bits to store the exponent (=bits_for_exponent) any floating point number greater than 2**(float_size - bits_for_exponent) will always be an integral value! At the other extreme a float with a negative exponent will give one of 1, 0 or -1. This makes the discussion of integer range versus float range moot because these functions will simply return the original number whenever the number is outside the range of the integer type. The python functions are wrappers of the C function and so this is really a deficiency of the C functions where they should have returned an integer and forced the programer to do the range/NaN/Inf check before calling ceil/floor.

Thus the logical answer is the only time these functions are useful they would return a value within integer range and so the fact they return a float is a mistake and you are very smart for realizing this!


回答 7

也许因为其他语言也这样做,所以它是普遍接受的行为。(有充分的理由,如其他答案所示)

Maybe because other languages do this as well, so it is generally-accepted behavior. (For good reasons, as shown in the other answers)


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。