为什么默认的 return 类型的 `ceiling` 和 `floor` 是数字?
Why is the default return type of `ceiling` and `floor` numeric?
为什么下面都是"numeric"
?
class(ceiling(3))
class(ceiling(3L))
class(ceiling(3.1))
class(floor(2))
class(floor(2L))
class(floor(2.1))
这似乎是一种算术运算,其结果明确为整数(与 exponentiation 不同),与输入无关(传递复数是错误的)。
我试着四处寻找与底层 C
代码相关的答案,但没有真正找到任何地方。
我还了解到,虽然 "%/%"(x,y)
也应该始终是整数,但结果的 class
取决于输入类型,例如5%/%2
、6%/%2
和6%/%2L
都是numeric
,但是5L%/%2L
和6L%/%2L
都是integer
(关于这个的东西提到了在 ?Arithmetic
);这对我来说也没有什么意义,但至少它被记录下来了。
从 ceiling
和 floor
返回 numeric
对象是否有一个简单的原因?如果是由于重铸导致效率低下(整数除法似乎可能是这种情况),我希望 class(ceiling(3L))
成为 "integer"
,那么这是怎么回事?
我不知道这是否就是 ceiling
被设计为 return numeric
的原因,但以下示例显示了限制ceiling
如果它实际上 returned 一个整数就会有:
options(digits = 15)
.Machine$integer.max + 1.4
#[1] 2147483648.4
ceiling(.Machine$integer.max + 1.4)
#[1] 2147483649
as.integer(ceiling(.Machine$integer.max + 1.4))
#[1] NA
#Warning message:
#NAs introduced by coercion
也就是说,我没有充分的理由明白为什么 ceiling
在给定整数输入时 return 不是整数。
为什么下面都是"numeric"
?
class(ceiling(3))
class(ceiling(3L))
class(ceiling(3.1))
class(floor(2))
class(floor(2L))
class(floor(2.1))
这似乎是一种算术运算,其结果明确为整数(与 exponentiation 不同),与输入无关(传递复数是错误的)。
我试着四处寻找与底层 C
代码相关的答案,但没有真正找到任何地方。
我还了解到,虽然 "%/%"(x,y)
也应该始终是整数,但结果的 class
取决于输入类型,例如5%/%2
、6%/%2
和6%/%2L
都是numeric
,但是5L%/%2L
和6L%/%2L
都是integer
(关于这个的东西提到了在 ?Arithmetic
);这对我来说也没有什么意义,但至少它被记录下来了。
从 ceiling
和 floor
返回 numeric
对象是否有一个简单的原因?如果是由于重铸导致效率低下(整数除法似乎可能是这种情况),我希望 class(ceiling(3L))
成为 "integer"
,那么这是怎么回事?
我不知道这是否就是 ceiling
被设计为 return numeric
的原因,但以下示例显示了限制ceiling
如果它实际上 returned 一个整数就会有:
options(digits = 15)
.Machine$integer.max + 1.4
#[1] 2147483648.4
ceiling(.Machine$integer.max + 1.4)
#[1] 2147483649
as.integer(ceiling(.Machine$integer.max + 1.4))
#[1] NA
#Warning message:
#NAs introduced by coercion
也就是说,我没有充分的理由明白为什么 ceiling
在给定整数输入时 return 不是整数。