Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the R function `c( )` coercing when inputs are numeric and bigq?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading