r - 为什么 NaN^0 == 1

标签 r math na arithmetic-expressions

由早期高尔夫代码的提示为什么会:

>NaN^0
[1] 1

NA^0 为 1 是完全合理的,因为 NA 缺少数据,并且任何数字提高到 0 都会得到 1 ,包括-InfInf。然而 NaN 应该表示非数字,那么为什么会这样呢?当 ?NaN 的帮助页面指出时,这甚至更加令人困惑/令人担忧:

In R, basically all mathematical functions (including basic Arithmetic), are supposed to work properly with +/- Inf and NaN as input or output.

The basic rule should be that calls and relations with Infs really are statements with a proper mathematical limit.

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 如何表示这些常数有关?

最佳答案

这在 ?'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/

相关文章:

linux - 在 Linux CentOS 6.5 版上安装 R3.1.1 时出现问题(检查 LDFLAGS 以获取 Fortran 库的路径)

xml - 从 R 中的原始 html 中提取数据

c++ - 如何使用 Rcpp 就地缩放 NumericMatrix?

mysql - SQL - 在包含数字和字母的列中搜索特定范围的最佳方法是什么?

r - 随机或按比例为 NA 分配分类值

r - R 中使用 NA 权重的加权平均值

javascript - 计算撞击倾斜墙壁后的 Angular 变化

algorithm - 使用预言机有效地对整数进行质因数分解

algorithm - 确定用于解压缩流式压缩数据的最佳初始缓冲区大小

r - 在 R 中按 NA 聚合