[R] : Ramanujan and the accuracy of floating point computations - using Rmpfr in R

Richard M. Heiberger rmh at temple.edu
Fri Jul 3 00:29:55 CEST 2015


There is a precedence error in your R attempt.  You need to convert
163 to 120 bits first, before taking
its square root.

> exp(sqrt(mpfr(163, 120)) * mpfr(pi, 120))
1 'mpfr' number of precision  120   bits
[1] 262537412640768333.51635812597335712954

## just the last four characters to the left of the decimal point.
> tmp <- c(baseR=8256, Wolfram=8744, Rmpfr=8333, wrongRmpfr=7837)
> tmp-tmp[2]
     baseR    Wolfram      Rmpfr wrongRmpfr
      -488          0       -411       -907
>

You didn't give the Wolfram alpha code you used.  There is no way of
verifying the correct value from your email.
Please check that you didn't have a similar precedence error in that code.

Rich


On Thu, Jul 2, 2015 at 2:02 PM, Aditya Singh via R-help
<r-help at r-project.org> wrote:
>
> Ravi
>
> I am a chemical engineer by training. Is there not something like law of corresponding states in numerical analysis?
>
> Aditya
>
>
>
> ------------------------------
> On Thu 2 Jul, 2015 7:28 AM PDT Ravi Varadhan wrote:
>
>>Hi,
>>
>>Ramanujan supposedly discovered that the number, 163, has this interesting property that exp(sqrt(163)*pi), which is obviously a transcendental number, is real close to an integer (close to 10^(-12)).
>>
>>If I compute this using the Wolfram alpha engine, I get:
>>262537412640768743.99999999999925007259719818568887935385...
>>
>>When I do this in R 3.1.1 (64-bit windows), I get:
>>262537412640768256.0000
>>
>>The absolute error between the exact and R's value is 488, with a relative error of about 1.9x10^(-15).
>>
>>In order to replicate Wolfram Alpha, I tried doing this in "Rmfpr" but I am unable to get accurate results:
>>
>>library(Rmpfr)
>>
>>
>>> exp(sqrt(163) * mpfr(pi, 120))
>>
>>1 'mpfr' number of precision  120   bits
>>
>>[1] 262537412640767837.08771354274620169031
>>
>>The above answer is not only inaccurate, but it is actually worse than the answer using the usual double precision.  Any thoughts as to what I am doing wrong?
>>
>>Thank you,
>>Ravi
>>
>>
>>
>>       [[alternative HTML version deleted]]
>>
>>______________________________________________
>>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>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.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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