在R中,为什么is.numeric(NaN)打印"TRUE"? [英] In R, why does is.numeric(NaN) print "TRUE"?
问题描述
NaN`的意思是不是数字".但是我发现
NaN` means "Not a Number". However I found out that the result of
is.numeric(NaN)
是
[1] "TRUE"
有人知道为什么吗?我认为结果应该是FALSE
.
Anybody know why? I think the result should be FALSE
.
推荐答案
不是数字"并不表示它不是数字.这是浮点数的特殊编码.请参见 ANSI/IEEE 754浮点标准,或者仅是NaN的Wikipedia页面.
"Not a Number" does not really mean it is not a number. It is a special coding of a floating-point number. See ANSI/IEEE 754 floating-point standard, or simply the Wikipedia page for NaN.
这是所有计算语言的通用标准,R的处理也不例外.
This is a universal standard for all computing languages and R's handling is no exception.
在R中,typeof(NaN)
给出双精度",而mode(NaN)
给出数值".现在,如果您阅读?is.numeric
,您会发现,如果x
的模式为数字"且不是一个因子,则is.numeric(x)
会返回TRUE
.
In R, typeof(NaN)
gives "double", and mode(NaN)
gives "numeric". Now if you read ?is.numeric
, you see that is.numeric(x)
returns TRUE
if x
has mode "numeric" and is not a factor.
IEEE标准定义了三个特殊的浮点数Inf
,-Inf
和NaN
.
The IEEE standard defines three special floating-point numbers, Inf
, -Inf
and NaN
.
前两个数学意义很明确:正无穷大和负无穷大.尽管它们不是实数的一部分,但它们是定义明确的限制.例如,1 / 0
是Inf
.
The first two have a clear mathematical meaning: positive infinite and negative infinite. Although they are not part of real number, they are well-defined limit. For example, 1 / 0
is Inf
.
那么NaN
是什么意思?恰好是定义不明确的实数.简单的例子是
So what does NaN
mean? Precisely it is not well-defined real number. Simple cases are
0 / 0 # can be either Inf or -Inf
sqrt(-1) # not well-defined on real number set
Inf - Inf # can not be decided, can be 0, Inf or -Inf
在数值计算中,如果结果不能用规则的实数或Inf
或-Inf
书写,则将其表示为NaN
.在这方面,任何浮点数之间的算术都变得完全可表示. NaN
在这方面很有参考价值,表示出了点问题".
In numerical computing, if the result can not be written in regular real number, or Inf
or -Inf
, it is expressed as NaN
. In this regard, arithmetics between any floating-point numbers becomes completely representable. NaN
is quite informative in this regard, it means "something has gone wrong".
请注意,与NA
具有多种类型(NA_real_
,NA_integer_
等)不同,NaN
仅具有一种浮点数类型.任何逻辑,字符,整数值都不是浮点数,因此不能为NaN
. NA的定义不是通用标准,R有其自己的定义.
Note that unlike NA
which has various types (NA_real_
, NA_integer_
, etc), NaN
has only one type for floating-point numbers. Any logical, character, integer values are not floating-point numbers hence can not be NaN
. The definition of NA is not a universal standard and R has its own.
这篇关于在R中,为什么is.numeric(NaN)打印"TRUE"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!