as.logical 双重强制的基本原理是什么?
What is the rationale for as.logical double coercion?
我正在尝试了解双打的强制原理:
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
我可以看到强制转换发生在标量上(符号无关紧要)并且
只有恰好 0 被强制转换为 FALSE
。我对此感到惊讶,
似乎没有 "closest to..." 或四舍五入到 0L 或 1L 的概念。
我很好奇为什么。
我确实尝试四处寻找洞察力
here
但没有成功。
我也看了this related question。
有人可以解释为什么我应该期望以上每个测试值
像他们那样胁迫?
我猜这是从 C 继承而来的,例如来自 here:
In C true is represented by any numeric value not equal to 0 and false is represented by 0
另见例如here;目前我找不到更好的官方语言规范来源。
Kernighan 和 Ritchie 的盗版 2d ed。我在网上找到(我宁愿不 link 它)在 p. 上说。 50,第 3.2 节,"If-Else"(强调已添加):
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:
#define LOGICAL(x) ((int *) DATAPTR(x))
我正在尝试了解双打的强制原理:
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
我可以看到强制转换发生在标量上(符号无关紧要)并且
只有恰好 0 被强制转换为 FALSE
。我对此感到惊讶,
似乎没有 "closest to..." 或四舍五入到 0L 或 1L 的概念。
我很好奇为什么。
我确实尝试四处寻找洞察力 here 但没有成功。
我也看了this related question。
有人可以解释为什么我应该期望以上每个测试值 像他们那样胁迫?
我猜这是从 C 继承而来的,例如来自 here:
In C true is represented by any numeric value not equal to 0 and false is represented by 0
另见例如here;目前我找不到更好的官方语言规范来源。
Kernighan 和 Ritchie 的盗版 2d ed。我在网上找到(我宁愿不 link 它)在 p. 上说。 50,第 3.2 节,"If-Else"(强调已添加):
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:
#define LOGICAL(x) ((int *) DATAPTR(x))