当输入为 numeric 和 bigq 时,R 函数 `c( )` 强制转换是什么?
考虑这两个对比示例:
> library(Rmpfr)
> K = 1:5
> denoms = c(as.bigz(1), as.bigq(rep(1,times = length(K)), K) )
#Big Rational ('bigq') object of length 6:
#[1] 1 1 1/2 1/3 1/4 1/5
> foo = c(1, as.bigq(rep(1,times = length(K)), K) )
# [1] 1 5 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
# [39] 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
> bar <- c(as.bigz(1), as.bigq(rep(1,times = length(K)), K) )
#Big Rational ('bigq') object of length 6:
# [1] 1 1 1/2 1/3 1/4 1/5
我知道方法 c.bigq
可能不完整,导致所需的解释和数据类型强制转换失败。我的问题是:一些 bigq
对象是如何变成 1 和 0 的向量的?输出 foo
是数字。
Consider these two contrasting examples:
> library(Rmpfr)
> K = 1:5
> denoms = c(as.bigz(1), as.bigq(rep(1,times = length(K)), K) )
#Big Rational ('bigq') object of length 6:
#[1] 1 1 1/2 1/3 1/4 1/5
> foo = c(1, as.bigq(rep(1,times = length(K)), K) )
# [1] 1 5 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
# [39] 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
> bar <- c(as.bigz(1), as.bigq(rep(1,times = length(K)), K) )
#Big Rational ('bigq') object of length 6:
# [1] 1 1 1/2 1/3 1/4 1/5
I understand that the method c.bigq
may be incomplete, leading to failure of the desired interpretation and coercion of data types. My question is: how did some bigq
objects get turned into those vectors of 1s and 0s ? The output foo
is numeric.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
c()
上的调度是根据第一个元素的类完成的,因此c(1, as.bigq(rep(1,times = length(K)), K) )
调用默认方法。第二个参数
as.bigq(rep(1,times = length(K)), K)
具有类"bigq"
,但这无关紧要,因为默认方法会忽略班级。它只是查看类型,并且typeof(as.bigq(rep(1,times = length(K)), K))
给出"raw"
,所以它是存储为原始字节。如果您对其调用unclass()
,您会看到类似的内容,这就是所有这些数字的来源。
另一方面,
c(as.bigz(1), as.bigq(rep(1,times = length(K)), K) )
有一个bigz
object 作为第一个参数,因此它将调用c.bigz
。该方法了解bigz
和bigq
对象,并用它们做正确的事情。The dispatch on
c()
is done based on the class of the first element, soc(1, as.bigq(rep(1,times = length(K)), K) )
calls the default method.The second parameter
as.bigq(rep(1,times = length(K)), K)
has class"bigq"
, but that's irrelevant, because the default method ignores the class. It just looks at the type, andtypeof(as.bigq(rep(1,times = length(K)), K))
gives"raw"
, so it's stored as raw bytes. If you callunclass()
on it, you'll see something likeso that's where all those numbers came from.
On the other hand,
c(as.bigz(1), as.bigq(rep(1,times = length(K)), K) )
has abigz
object as the first argument, so it will callc.bigz
. That method knows aboutbigz
andbigq
objects and does the right thing with them.