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
What is inf and nan?
提问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 + inf
i get nan
is 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 - inf
I don't get -inf
I get nan
I'm sure it's all pretty simple but I stumbled upon them and didn't know what they do.
此外,当我这样nan - inf
做时,我不明白,-inf
我nan
确信这一切都很简单,但我偶然发现了它们,不知道它们是做什么的。
采纳答案by Volatility
inf
is infinity - a value that is greater than any other value. -inf
is therefore smaller than any other value.
inf
无穷大 - 比任何其他值都大的值。-inf
因此小于任何其他值。
nan
stands 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 nan
is (literally) not a number, you can't do arithmetic with it, so the result of the second operation is also not a number (nan
)
因为nan
is(字面意思)不是数字,你不能用它做算术,所以第二个运算的结果也不是数字 ( nan
)
回答by Tim Pietzcker
nan
means "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 NaN
will also result in NaN
.
nan
表示“不是数字”,这是一个浮点值,如果您执行的计算结果不能表示为数字。您执行的任何计算也NaN
将导致NaN
.
inf
means infinity.
inf
意味着无穷大。
For example:
例如:
>>> 2*float("inf")
inf
>>> -2*float("inf")
-inf
>>> float("inf")-float("inf")
nan
回答by U2EF1
Inf
is 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
. -Inf
is similar, but smaller than everything.
Inf
是无穷大,它是一个“比所有其他数字都大”的数字。试着从中减去你想要的任何东西,它不会变小。所有数字都是< Inf
。-Inf
是相似的,但比一切都小。
NaN
means not-a-number. If you try to do a computation that just doesn't make sense, you get NaN
. Inf - Inf
is one such computation. Usually NaN
is used to just mean that some data is missing.
NaN
表示非数字。如果你试图做一个没有意义的计算,你会得到NaN
. Inf - Inf
就是这样一种计算。通常NaN
用于仅表示某些数据丢失。
回答by devnull
You say:
你说:
when i do
nan - inf
i dont get-inf
i getnan
当我这样做时,
nan - inf
我不明白-inf
我明白了nan
This is because any operation containing NaN
as an operand would return NaN
.
这是因为任何包含NaN
作为操作数的操作都会返回NaN
.
A comparison with NaN
would 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操作都是有效的,所以不需要处理异常。