Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluation of as.integer(max(factorize(15)))

Tags:

r

This appears quite bizarre to me and I would like an explanation. I

library(gmp)
factorize(15) # => "3" "5"
max(factorize(15)) # => "5"
as.integer("5") # => 5
as.integer(max(factorize(15))) # => 1 0 0 0 1 0 0 0 1 0 0 0 5 0 0 0

I can do what I want with:

max(as.numeric(factorize(15))) # => [1]5

But it shook me that I couldn't rely on nesting functions inside of functions in a supposedly scheme-like language. Am I missing anything?

like image 634
wdkrnls Avatar asked Dec 27 '22 13:12

wdkrnls


2 Answers

Well, the answer is in the representation of factorize(15):

> dput(factorize(15))
structure(c(02, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 03, 
00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 05, 00, 00, 00), class = "bigz")

and

> dput(max(factorize(15)))
structure(c(01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 05, 
00, 00, 00), class = "bigz")

...max and as.numeric (actually, as.double) have methods for the bigz class, but apparently as.integer does not:

> methods(max)
[1] max.bigq max.bigz
> methods(as.numeric)
no methods were found
> methods(as.double)
[1] as.double.bigq     as.double.bigz     as.double.difftime as.double.POSIXlt 
> methods(as.integer)
no methods were found

...so as.integer treats the bigz objects as a simple vector of values.

like image 198
Tommy Avatar answered Dec 31 '22 02:12

Tommy


The result of factorize in package gmp is a object of class bigz:

> factorize(15)
[1] "3" "5"
> str(factorize(15))
Class 'bigz'  raw [1:28] 02 00 00 00 ...

From the help for ?biginteger it seems the following coercion functions are defined for objects of class bigz:

  • as.character
  • as.double

Notice there is no as.integer in the list. So, to convert your result to numeric, you must use as.double:

> as.double(max(factorize(15)))
[1] 5
like image 38
Andrie Avatar answered Dec 31 '22 03:12

Andrie