当输入为 numeric 和 bigq 时,R 函数 `c( )` 强制转换是什么?

发布于 2025-01-16 16:35:06 字数 735 浏览 4 评论 0原文

考虑这两个对比示例:

> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倚栏听风 2025-01-23 16:35:06

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() ,您会看到类似的内容,

 [1] 05 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00
[40] 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00
attr(,"denominator")
 [1] 05 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 02 00 00 00 01 00 00 00 01 00 00 00 03 00 00
[40] 00 01 00 00 00 01 00 00 00 04 00 00 00 01 00 00 00 01 00 00 00 05 00 00 00

这就是所有这些数字的来源。

另一方面,c(as.bigz(1), as.bigq(rep(1,times = length(K)), K) ) 有一个 bigz object 作为第一个参数,因此它将调用 c.bigz。该方法了解 bigzbigq 对象,并用它们做正确的事情。

The dispatch on c() is done based on the class of the first element, so c(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, and typeof(as.bigq(rep(1,times = length(K)), K)) gives "raw", so it's stored as raw bytes. If you call unclass() on it, you'll see something like

 [1] 05 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00
[40] 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00
attr(,"denominator")
 [1] 05 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 02 00 00 00 01 00 00 00 01 00 00 00 03 00 00
[40] 00 01 00 00 00 01 00 00 00 04 00 00 00 01 00 00 00 01 00 00 00 05 00 00 00

so 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 a bigz object as the first argument, so it will call c.bigz. That method knows about bigz and bigq objects and does the right thing with them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文