由早期高尔夫代码的提示为什么会:
>NaN^0
[1] 1
NA^0
为 1 是完全合理的,因为 NA
缺少数据,并且任何数字提高到 0 都会得到 1 ,包括-Inf
和Inf
。然而 NaN
应该表示非数字,那么为什么会这样呢?当 ?NaN
的帮助页面指出时,这甚至更加令人困惑/令人担忧:
In R, basically all mathematical functions (including basic
Arithmetic
), are supposed to work properly with+/- Inf
andNaN
as input or output.The basic rule should be that calls and relations with
Inf
s really are statements with a proper mathematical limit.Computations involving
NaN
will returnNaN
or perhapsNA
: which of those two is not guaranteed and may depend on the R platform (since compilers may re-order computations).
这背后是否有哲学原因,或者只是与 R 如何表示这些常数有关?
最佳答案
这在 ?'NaN' 引用的帮助页面中引用了
“IEC 60559 标准,也称为 ANSI/IEEE 754 浮点标准。
http://en.wikipedia.org/wiki/NaN ”
在那里你可以找到关于什么应该创建 NaN 的声明:
"There are three kinds of operations that can return NaN:[5]
Operations with a NaN as at least one operand.
它可能来自特定的 C 编译器,如您引用的注释所示。 GNU C 文档是这样说的:
http://www.gnu.org/software/libc/manual/html_node/Infinity-and-NaN.html
“另一方面,NaN 会影响任何涉及它的计算。除非无论用什么实际值替换 NaN,计算都会产生相同的结果,否则结果就是 NaN。”
看来 GNU-C 人们在编写代码时有不同的标准。据报道 2008 版 ANSI/IEEE 754 浮点标准提出了这样的建议:
http://en.wikipedia.org/wiki/NaN#Function_definition
发布的标准不是免费的。因此,如果您有访问权限或资金,您可以查看此处:
http://ieeexplore.ieee.org/xpl/mostRecentIssue.jsp?punumber=4610933
关于r - 为什么 NaN^0 == 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17863619/