为什么 NaN^0 == 1 [英] Why does NaN^0 == 1
问题描述
受到早期代码打高尔夫球的提示,为什么会:
Prompted by a spot of earlier code golfing why would:
>NaN^0
[1] 1
NA^0
为 1 是完全合理的,因为 NA
缺少数据,并且 任何 数字提高为 0 将给出 1,包括-Inf
和Inf
.然而 NaN
应该代表 not-a-number,那么为什么会这样呢?当 ?NaN
的帮助页面指出:
It makes perfect sense for NA^0
to be 1 because NA
is missing data, and any number raised to 0 will give 1, including -Inf
and Inf
. However NaN
is supposed to represent not-a-number, so why would this be so? This is even more confusing/worrying when the help page for ?NaN
states:
在 R 中,基本上所有的数学函数(包括基本的Arithmetic
),应该与 +/- Inf
和 NaN
一起正常工作输入或输出.
In R, basically all mathematical functions (including basic
Arithmetic
), are supposed to work properly with+/- Inf
andNaN
as input or output.
基本规则应该是Inf
的调用和关系确实是具有适当数学限制的语句.
The basic rule should be that calls and relations with Inf
s really are
statements with a proper mathematical limit.
涉及NaN
的计算将返回NaN
或NA
:哪个这两个不能保证,可能取决于 R 平台(因为编译器可能会重新排序计算).
Computations involving NaN
will return NaN
or perhaps NA
: which of
those two is not guaranteed and may depend on the R platform (since
compilers may re-order computations).
这背后是否有哲学上的原因,还是仅仅与 R 表示这些常量的方式有关?
Is there a philosophical reason behind this, or is it just to do with how R represents these constants?
推荐答案
这个在?'NaN'
IEC 60559 标准,也称为 ANSI/IEEE 754 浮点标准.
"The IEC 60559 standard, also known as the ANSI/IEEE 754 Floating-Point Standard.
http://en.wikipedia.org/wiki/NaN."
然后你会发现关于什么应该创建 NaN 的声明:
And there you find this statement regarding what should create a NaN:
"There are three kinds of operations that can return NaN:[5]
Operations with a NaN as at least one operand.
它可能来自特定的 C 编译器,如您引用的注释所示.这是 GNU C 文档所说的:
It is probably is from the particular C compiler, as signified by the Note you referenced. This is what the GNU C documentation says:
http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
另一方面,NaN 会感染任何涉及它的计算.除非无论用什么实际值替换 NaN,计算都会产生相同的结果,否则结果就是 NaN."
" NaN, on the other hand, infects any calculation that involves it. Unless the calculation would produce the same result no matter what real value replaced NaN, the result is NaN."
因此看来 GNU-C 人员在编写代码时有不同的标准.据报道,2008 版 ANSI/IEEE 754 浮点标准提出了该建议:
So it seems that the GNU-C people have a different standard in mind when writing their code. And the 2008 version of ANSI/IEEE 754 Floating-Point Standard is reported to make that suggestion:
http://en.wikipedia.org/wiki/NaN#Function_definition
已发布的标准不是免费的.因此,如果您有访问权限或金钱,您可以在这里查看:
The published standard is not free. So if you are have access rights or money you can look here:
http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?punumber=4610933
http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?punumber=4610933
这篇关于为什么 NaN^0 == 1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!