[R] problem with the precision of numbers
William Dunlap
wdunlap at tibco.com
Mon Jan 25 19:26:51 CET 2010
> -----Original Message-----
> From: r-help-bounces at r-project.org
> [mailto:r-help-bounces at r-project.org] On Behalf Of kayj
> Sent: Monday, January 25, 2010 8:04 AM
> To: r-help at r-project.org
> Subject: Re: [R] problem with the precision of numbers
>
>
> Hi All,
>
> thank you all for your help. I have tried Bill's script and
> it works! so I
> am trying to think what was the problem and it looks like it i sthe
> precision. so you defined a function of the precision and evaluates at
> precision=500. Bill, I was wondering how did you choose 500? is it
> arbitrary?
The problem was that you were computing several quantities
in the machine's native double precision format, not using
the mpfr format. I chose to parameterize the function
with precision so you could see how the precision affected
the result. I showed precision=500 mainly because that is
what you used for the variables that you did use in mpfr
format. I did not do any analysis to see what precision
would be needed to get a given final precision, but that
would be possible. Here is how precision affects the results:
> f <- function (precision){
require(Rmpfr)
s <- mpfr(0, precision)
j <- mpfr(-1, precision)
c <- mpfr(1, precision)
for (i in 0:80) {
i <- mpfr(i, precision)
s <- s + ((j^i) * c * (1 - (i + 1) * 1/200)^200)
c <- (c * (80 - i))/(i + 1)
}
s
}
> p<-2^(1:10) # choose 10 values for precision
> z<-lapply(p, f) # evaluate f at those 10 values
> invisible(lapply(z,print,digits=20)) # print(z, digits=20) doesn't
work.
1 'mpfr' number of precision 2 bits
[1] 7.2535549176877750482e24
1 'mpfr' number of precision 4 bits
[1] 1099511627776
1 'mpfr' number of precision 8 bits
[1] -99090432
1 'mpfr' number of precision 16 bits
[1] 1815744
1 'mpfr' number of precision 32 bits
[1] 40.655612170696258545
1 'mpfr' number of precision 64 bits
[1] 3.3910635476710543806e-9
1 'mpfr' number of precision 128 bits
[1] 6.6568524698115877284e-20
1 'mpfr' number of precision 256 bits
[1] 6.6568524789662037700e-20
1 'mpfr' number of precision 512 bits
[1] 6.6568524789662037700e-20
1 'mpfr' number of precision 1024 bits
[1] 6.6568524789662037700e-20
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com
>
> I have looked at the Rmpfr documentation and they defined the
> mfr object to
> be
> Usage
> mpfr(x, precBits, base = 10)
> Const(name = c("pi", "gamma", "catalan"), prec = 120L)
>
> so is the precision defined in Bill's script the same as precBits?
>
> my bc package works , I have used bc ( from run "raw" and not
> from R) so is
> it possible to use it from R?
>
> thanks again for your help
>
>
> --
> View this message in context:
> http://n4.nabble.com/problem-with-the-precision-of-numbers-tp1
288905p1289483.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>
More information about the R-help
mailing list