[Rd] [R] big numbers

Hin-Tak Leung hin-tak.leung at cimr.cam.ac.uk
Wed Aug 23 15:45:02 CEST 2006


Robin Hankin wrote:
> [[shifting to R-devel]]
> 
> Hi Roger
> 
> yes, I'm aware of gmp, but although it does handle big
> numbers, it uses exact integer arithmetic, which would be
> too slow for me.
> 
> My example of 10^10000 *(1+pi) ~= 4.14259*10^10000
> would require gmp to process 10000 digits, which would be
> time consuming.
> 
> My best idea so far is to define a new class of objects
> that have a signed floating point mantissa M in the range 1-10 and a
> signed integer exponent E.   Then (E,M) would be E*10^M.
> 
> So the ordered pair (M,E) would be able to represent positive numbers  
> from
> something like 10^(-10^9) to something like 10^(10^9), and negative
> numbers of the same magnitude.
> 
> Perhaps it would be possible to write a little C program that would
> implement this that would be as fast as regular floating-point
> arithmetic to within an order of magnitude?
> 
> Anyone got any advice here?

It sounds like you want more bits in a floating point number, and
I can see two ways of doing it...

(1) I don't know if the R people want to support quadruple precision
(REAL*16 in fortran) numeric types, but that may just about do what
you want. max double is 1E308 which is a bit small, and I don't
know how well it is supported by the major compilers, etc.

(2) roll you own... libjpeg and freetype (the font rendering library)
internally both uses integer operations to do floating-points maths
for speed reasons. I think typically libjpeg uses 32-bit-int >> 16
and there are 2.14 or 26.6 float types in freetype.


> 
> 
> 
> 
> On 22 Aug 2006, at 14:58, Roger D. Peng wrote:
> 
>> The 'gmp' package may be of use here, but I'm not sure.
>>
>> -roger
>>
>> Robin Hankin wrote:
>>> Hi
>>>
>>> Can I get R to handle really big numbers?    I am not interested
>>> in more than (say) 10 sig figs, but I would like to deal with numbers
>>> up to, say, 10^10000.
>>>
>>> If
>>>
>>> a <- 10^10000
>>> b <- pi* a
>>>
>>> I would like  "a+b" to return 3.1415926e10000.
>>>
>>>
>>> Toy example, illustrating why I can't deal with log(a) and log(b),
>>> follows.
>>>
>>>
>>>
>>> f <- function(a,n=100){
>>>    out <- rep(0,n)
>>>    out[1] <- a
>>>    for(i in 2:n){
>>>      out[i] <- sum(exp(out[1:i])) + rexp(1)
>>>    }
>>>    return(log(out))
>>> }
>>>
>>>
>>> then f(1,10)  has infinities in it, even though the values should be
>>> moderate size.
>>>
>>> What are my options here?
>>>
>>> --
>>> Robin Hankin
>>> Uncertainty Analyst
>>> National Oceanography Centre, Southampton
>>> European Way, Southampton SO14 3ZH, UK
>>>   tel  023-8059-7743
>>>
>>> ______________________________________________
>>> R-help at stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting- 
>>> guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>> -- 
>> Roger D. Peng  |  http://www.biostat.jhsph.edu/~rpeng/
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting- 
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
> --
> Robin Hankin
> Uncertainty Analyst
> National Oceanography Centre, Southampton
> European Way, Southampton SO14 3ZH, UK
>   tel  023-8059-7743
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



More information about the R-devel mailing list