Python inf 和 nan 是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17628613/
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 08:43:51  来源:igfitidea点击:

What is inf and nan?

pythonpython-2.7

提问by Serial

Just a question that I'm kind of confused about

只是一个我有点困惑的问题

So I was messing around with float('inf')and kind of wondering what it is used for.

所以我在胡闹float('inf'),有点想知道它是用来做什么的。

Also I noticed that if I add -inf + infi get nanis that the same as Zero or not.

我还注意到,如果我添加,-inf + inf我得到的结果nan是否与零相同。

I'm confused about what the uses of these two values are.

我对这两个值的用途感到困惑。

Also when I do nan - infI don't get -infI get nanI'm sure it's all pretty simple but I stumbled upon them and didn't know what they do.

此外,当我这样nan - inf做时,我不明白,-infnan确信这一切都很简单,但我偶然发现了它们,不知道它们是做什么的。

采纳答案by Volatility

infis infinity - a value that is greater than any other value. -infis therefore smaller than any other value.

inf无穷大 - 比任何其他值都大的值。-inf因此小于任何其他值。

nanstands for Not A Number, and this is not equal to 0.

nan代表 Not A Number,这不等于0

Although positive and negative infinity can be said to be symmetric about 0, the same can be said for any value n, meaning that the result of adding the two yields nan. This idea is discussed in this math.se question.

虽然可以说正无穷大和负无穷大是关于对称的0,但对于任何值都可以这样说n,这意味着两者相加的结果nan。这个想法在这个 math.se question 中讨论。

Because nanis (literally) not a number, you can't do arithmetic with it, so the result of the second operation is also not a number (nan)

因为nanis(字面意思)不是数字,你不能用它做算术,所以第二个运算的结果也不是数字 ( nan)

回答by Tim Pietzcker

nanmeans "not a number", a float value that you get if you perform a calculation whose result can't be expressed as a number. Any calculations you perform with NaNwill also result in NaN.

nan表示“不是数字”,这是一个浮点值,如果您执行的计算结果不能表示为数字。您执行的任何计算也NaN将导致NaN.

infmeans infinity.

inf意味着无穷大。

For example:

例如:

>>> 2*float("inf")
inf
>>> -2*float("inf")
-inf
>>> float("inf")-float("inf")
nan

回答by U2EF1

Infis infinity, it's a "bigger than all the other numbers" number. Try subtracting anything you want from it, it doesn't get any smaller. All numbers are < Inf. -Infis similar, but smaller than everything.

Inf是无穷大,它是一个“比所有其他数字都大”的数字。试着从中减去你想要的任何东西,它不会变小。所有数字都是< Inf-Inf是相似的,但比一切都小。

NaNmeans not-a-number. If you try to do a computation that just doesn't make sense, you get NaN. Inf - Infis one such computation. Usually NaNis used to just mean that some data is missing.

NaN表示非数字。如果你试图做一个没有意义的计算,你会得到NaN. Inf - Inf就是这样一种计算。通常NaN用于仅表示某些数据丢失。

回答by devnull

You say:

你说:

when i do nan - infi dont get -infi get nan

当我这样做时,nan - inf我不明白-inf我明白了nan

This is because any operation containing NaNas an operand would return NaN.

这是因为任何包含NaN作为操作数的操作都会返回NaN.

A comparison with NaNwould return an unordered result.

与 的比较NaN将返回无序结果

>>> float('Inf') == float('Inf')
True
>>> float('NaN') == float('NaN')
False

回答by user3765030

I use inf/-inf as initial values to find minimum/maximum value of a measurement. Lets say that you measure temperature with a sensor and you want to keep track of minimum/maximum temperature. The sensor might provide a valid temperature or might be broken. Pseudocode:

我使用 inf/-inf 作为初始值来查找测量的最小值/最大值。假设您使用传感器测量温度,并且想要跟踪最低/最高温度。传感器可能提供有效温度或可能已损坏。伪代码:

# initial value of the temperature
t = float('nan')          
# initial value of minimum temperature, so any measured temp. will be smaller
t_min = float('inf')      
# initial value of maximum temperature, so any measured temp. will be bigger
t_max = float('-inf')     
while True:
    # measure temperature, if sensor is broken t is not changed
    t = measure()     
    # find new minimum temperature
    t_min = min(t_min, t) 
    # find new maximum temperature
    t_max = max(t_max, t) 

The above code works because inf/-inf/nan are valid for min/max operation, so there is no need to deal with exceptions.

上面的代码是有效的,因为inf/-inf/nan对min/max操作都是有效的,所以不需要处理异常。