逻辑双重强制的基本原理是什么? [英] What is the rationale for as.logical double coercion?
问题描述
我试图理解双打的强制性理由:
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) and
that 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 insight here but no success.
我还查看了此相关问题.
有人可以解释为什么我应该期望上述每个测试值 像他们一样胁迫?
Can someone explain why I should expect each of the above tested values to coerce as they do?
推荐答案
我的猜测是这是从C继承的,例如来自此处:
My guess would be that this is inherited from C, e.g. from here:
在C中,true表示为任何不等于0的数值,false表示为0
In C true is represented by any numeric value not equal to 0 and false is represented by 0
另请参见此处;目前,我找不到官方语言规范的更好来源.
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):
对表达式求值;如果为真(,即,表达式具有非零值),则执行语句1.如果为假(表达式为零),并且存在其他部分,则改为执行语句2.
The expression is evaluated; if it is true (that is, if expression has a non-zero value), statement 1 is executed. If it is false (expression is zero) and if there is an else part, statement 2 is executed instead.
@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))
这篇关于逻辑双重强制的基本原理是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!