Search code examples
rlong-integerbigintegerbigdecimal

Operations on long numbers in R


I aim to use maximum likelihood methods (usually about 10^5 iterations) with a probability distribution that creates very big integers and very small float values that cannot be stored as a numeric nor a in a float type.

I thought I would use the as.bigq in the gmp package. My issue is that one can only add, substract, multiply and dived two objects of class/type bigq, while my distribution actually contains logarithm, power, gamma and confluent hypergeometric functions.

What is my best option to deal with this issue?

  • Should I use another package?
  • Should I code all these functions for bigq objects.
    • Coding these function on R may cause some functions to be very slow, right?
    • How to write the logarithm function using only the +,-,*,/ operators? Should I approximate this function using a taylor series expansion?
    • How to write the power function using only the +,-,*,/ operators when the exponent is not an integer?
    • How to write the confluent hypergeometric function (the equivalent of the Hypergeometric1F1Regularized[..] function in Mathematica)?

I could eventually write these functions in C and call them from R but it sounds like some complicated work for not much, especially if I have to use the gmp package in C as well to handle these big numbers.


Solution

  • All your problems can be solved with Rmpfr most likely which allows you to use all of the functions returned by getGroupMembers("Math") with arbitrary accuracy.

    Vignette: http://cran.r-project.org/web/packages/Rmpfr/vignettes/Rmpfr-pkg.pdf

    Simple example of what it can do:

    test <- mpfr(rnorm(100,mean=0,sd=.0001), 240)
    
    Reduce("*", test)
    

    I don't THINK it has hypergeometric functions though...