R 中 "identical()" 函数的问题? "identical()" 如何适用于不同类型的对象?

A problem on "identical()" function in R? How does "identical()" work for different types of objects?

(添加了可重现的示例)

我无法理解为什么下面是 FALSE(我知道它们分别是 doubleinteger):

identical(1, as.integer(1)) # FALSE

?identical 显示:

num.eq: 逻辑指示是否应使用 ==(“等于”)或按位比较来比较(双精度和复数非 NA)数字。后者(非默认) 区分 -0 和 +0。

sprintf("%.8190f", as.integer(1))sprintf("%.8190f", 1) return 完全相等 位模式。所以,我认为至少有以下一项必须returnTRUE。但是,我在以下各项中得到 FALSE

identical(1, as.integer(1), num.eq=TRUE) # FALSE
identical(1, as.integer(1), num.eq=FALSE) # FALSE

我现在是这样考虑的:如果sprintf是一个记法指标,而不是存储指标,那么这意味着identical()是根据存储进行比较的。 IE。 identical(bitpattern1, bitpattern1bitpattern2) returns FALSE。对于上述 FALSE/FALSE 情况,我找不到任何其他合乎逻辑的解释。

我知道在 R 的 32 位/64 位架构中,整数都存储为 32 位。

它们并不完全相同,因为它们具有不同的类型。如果您查看 identical 的文档,您会找到带有注释 ## FALSE, stored as different types 的示例 identical(1, as.integer(1))。这是一个线索。 R language definition 提醒我们:

Single numbers, such as 4.2, and strings, such as "four point two" are still vectors, of length 1; there are no more basic types (emphasis mine).

所以,基本上所有东西都是一个有类型的向量(这也是为什么每次 R returns 某些东西时 [1] 出现的原因)。您可以通过使用 vector 明确创建长度为 1 的向量来检查这一点,然后将其与 0:

进行比较
x <- vector("double", 1)
identical(x, 0)
# [1] TRUE

也就是说,vector("double", 1)0都输出类型为"double"且长度== 1的向量。

typeofstorage.mode 指向同一件事,所以你说 "this means identical() compares based on storage" 是对的。我不认为这一定意味着 "bit patterns" 正在被比较,尽管我认为这是可能的。查看使用 storage.mode:

更改存储模式时会发生什么
## Assign integer to x. This is really a vector length == 1.
x <- 1L

typeof(x)
# [1] "integer"

identical(x, 1L)
# [1] TRUE

## Now change the storage mode and compare again. 
storage.mode(x) <- "double"

typeof(x)
# [1] "double"

identical(x, 1L) # This is no longer TRUE.
# [1] FALSE

identical(x, 1.0) # But this is.
# [1] TRUE

最后一点:identical 的文档指出 num.eq 是……

logical indicating if (double and complex non-NA) numbers should be compared using == (‘equal’), or by bitwise comparison.

因此,更改 num.eq 不会影响涉及整数的任何比较。请尝试以下操作:

# Comparing integers with integers.
identical(+0L, -0L, num.eq = T) # TRUE
identical(+0L, -0L, num.eq = F) # TRUE

# Comparing integers with doubles.
identical(+0, -0L, num.eq = T) # FALSE
identical(+0, -0L, num.eq = F) # FALSE

# Comparing doubles with doubles.
identical(+0.0, -0.0, num.eq = T) # TRUE
identical(+0.0, -0.0, num.eq = F) # FALSE