问题描述
我试图理解双打的强制性理由:
I'm trying to understand the coercion rationale for doubles:
as.logical(c(-1, -0.01, 0, 0.01, 0.1,1:10))
#> [1] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
#> [12] TRUE TRUE TRUE TRUE
我可以看到强制发生在标量上(符号无关紧要),FALSE
仅强制为0.我为此感到惊讶,似乎没有最接近..."或四舍五入为0L或1L的概念.我很好奇为什么.
I can see that coercion happens on a scalar (sign doesn't matter) andthat only exactly 0 is coerced to FALSE
. I was surprised by this,there seems to be no notion of "closest to..." or rounding to 0L or 1L.I'm curious as to why.
我确实尝试四处寻找洞察力此处但没有成功.
I did try to hunt around for insightherebut no success.
我还查看了此相关问题.
有人可以解释为什么我应该期望上述每个测试值像他们一样胁迫?
Can someone explain why I should expect each of the above tested valuesto coerce as they do?
推荐答案
我的猜测是这是从C继承的,例如来自此处:
My guess would be that this is inherited from C, e.g. from here:
另请参见此处;目前,我找不到官方语言规范的更好来源.
Also see e.g. here; at the moment I can't find a better source for the official language specification.
Kernighan和Ritchie 2d ed的盗版副本.我在网上发现(我不希望链接到它)在p上说. 50,第3.2节"If-Else"(添加了重点):
A pirated copy of Kernighan and Ritchie 2d ed. I found online (I'd rather not link to it) says on p. 50, Section 3.2, "If-Else" (emphasis added):
@hrbrmstr指出,用于转换的LOGICAL的R内部定义在Rinternals.h:
@hrbrmstr points out that the R internal definition of LOGICAL that's used for the conversion is in Rinternals.h:
#define LOGICAL(x) ((int *) DATAPTR(x))
这篇关于逻辑双重强制的基本原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!