Python 中 float('inf') 的意义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34264710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:42:55  来源:igfitidea点击:

What is the point of float('inf') in Python?

python

提问by Nirvik Baruah

Just wondering over here, what is the point of having a variable store an infinite value in a program? Is there any actual use and is there any case where it would be preferable to use foo = float('inf'), or is it just a little snippet they stuck in for the sake of putting it in?

只是想知道,在程序中让变量存储无限值有什么意义?是否有任何实际用途,是否有任何情况下最好使用foo = float('inf'),或者只是为了放入它而插入的一小段代码?

采纳答案by Nirvik Baruah

It acts as an unbounded upper value for comparison. This is useful for finding lowest values for something. for example, calculating path route costs when traversing trees.

它充当用于比较的无界上限值。这对于查找某事物的最低值很有用。例如,计算遍历树木时的路径路线成本。

e.g. Finding the "cheapest" path in a list of options:

例如,在选项列表中查找“最便宜”的路径:

>>> lowest_path_cost = float('inf')
>>> # pretend that these were calculated using some worthwhile algorithm
>>> path_costs = [1, 100, 2000000000000, 50]
>>> for path in path_costs:
...   if path < lowest_path_cost:
...     lowest_path_cost = path
...
>>> lowest_path_cost
1

if you didn't have float('Inf')available to you, what value would you use for the initial lowest_path_cost? Would 9999999be enough -- float('Inf')removes this guesswork.

如果你没有float('Inf')可用的东西,你会用什么价值the initial lowest_path_cost?就9999999足够-float('Inf')消除了这个猜测。

回答by Rahul Tripathi

From the documentation:

文档

Many floating-point features were added. The float() function will now turn the string nan into an IEEE 754 Not A Number value, and +inf and -inf into positive or negative infinity. This works on any platform with IEEE 754 semantics. (Contributed by Christian Heimes; issue 1635.)

添加了许多浮点功能。float() 函数现在会将字符串 nan 转换为 IEEE 754 Not A Number 值,并将 +inf 和 -inf 转换为正无穷大或负无穷大。这适用于任何具有 IEEE 754 语义的平台。(由 Christian Heimes 提供;第 1635 期。)

Also refer this: Working with Infinity and NaNs

另请参阅:使用无穷大和 NaN

回答by Yossarian42

float('inf')can be used in the comparison, thus making the code simpler and clear. For instance, in merge sort, a float('inf')can be added to the end of subarrays as a sentinel value. Don't confuse with the usage of infinity in maths, after all, programming is not all about maths.

float('inf')可以在比较中使用,从而使代码更简单明了。例如,在归并排序中,float('inf')可以将a作为标记值添加到子数组的末尾。不要与数学中无穷大的用法混为一谈,毕竟编程不仅仅是数学。

回答by Anuj

float('inf')

As stated in answer above, float('inf')is used for setting a variable with an infinitely large value. In simple words, it sets the value as +ve infinty.

如上面的回答所述,float('inf')用于设置具有无限大值的变量。简单来说,它将值设置为+ve infinty

ALTERNATIVELY, we can make use of the following statement,

或者,我们可以使用以下语句,

import sys
least_value = sys.maxsize()

The sys.maxsize()is more commonly used, for setting large value initially. When our aim is to find the least value from given set of values.

sys.maxsize()是比较常用的,用于初始设置较大的值。当我们的目标是从给定的一组值中找到最小值时。

Also, in case if we want to find largest value from given set of values. We can use the following.

此外,如果我们想从给定的一组值中找到最大值。我们可以使用以下方法。

import sys
greatest_value = -sys.maxsize()-1

# logic for comparing with rest of values

The -sys.maxsize()-1is used for setting initial value as -ve infinity.

所述-sys.maxsize()-1用于设置的初始值作为-ve无穷大。