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.
>Solution :
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.