Jeff Newmiller
2015-Jul-02 23:13 UTC
[R] : Ramanujan and the accuracy of floating point computations - using Rmpfr in R
But not 120 bits of pi... just 120 bits of the double precision version of pi.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On July 2, 2015 3:51:55 PM PDT, "Richard M. Heiberger" <rmh at
temple.edu> wrote:>precedence does matter in this example. the square
>root was taken of a doubleprecision (53 bit) number. my revision
>takes the square root of a 120 bit number.
>
>> sqrt(mpfr(pi, 120))
>1 'mpfr' number of precision 120 bits
>[1] 1.7724538509055159927515191031392484397
>> mpfr(sqrt(pi), 120)
>1 'mpfr' number of precision 120 bits
>[1] 1.772453850905515881919427556567825377
>> print(sqrt(pi), digits=20)
>[1] 1.7724538509055158819
>>
>
>Sent from my iPhone
>
>> On Jul 3, 2015, at 00:38, Ravi Varadhan <ravi.varadhan at
jhu.edu>
>wrote:
>>
>> Hi Rich,
>>
>> The Wolfram answer is correct.
>> http://mathworld.wolfram.com/RamanujanConstant.html
>>
>> There is no code for Wolfram alpha. You just go to their web engine
>and plug in the expression and it will give you the answer.
>> http://www.wolframalpha.com/
>>
>> I am not sure that the precedence matters in Rmpfr. Even if it does,
>the answer you get is still wrong as you showed.
>>
>> Thanks,
>> Ravi
>>
>> -----Original Message-----
>> From: Richard M. Heiberger [mailto:rmh at temple.edu]
>> Sent: Thursday, July 02, 2015 6:30 PM
>> To: Aditya Singh
>> Cc: Ravi Varadhan; r-help
>> Subject: Re: [R] : Ramanujan and the accuracy of floating point
>computations - using Rmpfr in R
>>
>> 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.
>
>______________________________________________
>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.
Nordlund, Dan (DSHS/RDA)
2015-Jul-03 00:48 UTC
[R] : Ramanujan and the accuracy of floating point computations - using Rmpfr in R
Ravi,
Take a look at the following link.
https://code.google.com/p/r-bc/
I followed the instructions to get a Windows version of the 'nix utility
program , bc (a high precision calculator), and the source for an R to bc
interface. After installing them, I executed
exp(sqrt(bc(163))*4*atan(bc(1)))
in R and got this result
"262537412640768743.9999999999992500725971981856888793538563373369908627075374103782106479101186073116295306145602054347"
I don't know if this is helpful, but ...
Dan
Daniel Nordlund, PhD
Research and Data Analysis Division
Services & Enterprise Support Administration
Washington State Department of Social and Health Services
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Jeff
Newmiller
Sent: Thursday, July 02, 2015 4:13 PM
To: Richard M. Heiberger; Ravi Varadhan
Cc: r-help
Subject: Re: [R] : Ramanujan and the accuracy of floating point computations -
using Rmpfr in R
But not 120 bits of pi... just 120 bits of the double precision version of pi.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live
Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On July 2, 2015 3:51:55 PM PDT, "Richard M. Heiberger" <rmh at
temple.edu> wrote:>precedence does matter in this example. the square root was taken of a
>doubleprecision (53 bit) number. my revision takes the square root of
>a 120 bit number.
>
>> sqrt(mpfr(pi, 120))
>1 'mpfr' number of precision 120 bits
>[1] 1.7724538509055159927515191031392484397
>> mpfr(sqrt(pi), 120)
>1 'mpfr' number of precision 120 bits
>[1] 1.772453850905515881919427556567825377
>> print(sqrt(pi), digits=20)
>[1] 1.7724538509055158819
>>
>
>Sent from my iPhone
>
>> On Jul 3, 2015, at 00:38, Ravi Varadhan <ravi.varadhan at
jhu.edu>
>wrote:
>>
>> Hi Rich,
>>
>> The Wolfram answer is correct.
>> http://mathworld.wolfram.com/RamanujanConstant.html
>>
>> There is no code for Wolfram alpha. You just go to their web engine
>and plug in the expression and it will give you the answer.
>> http://www.wolframalpha.com/
>>
>> I am not sure that the precedence matters in Rmpfr. Even if it does,
>the answer you get is still wrong as you showed.
>>
>> Thanks,
>> Ravi
>>
>> -----Original Message-----
>> From: Richard M. Heiberger [mailto:rmh at temple.edu]
>> Sent: Thursday, July 02, 2015 6:30 PM
>> To: Aditya Singh
>> Cc: Ravi Varadhan; r-help
>> Subject: Re: [R] : Ramanujan and the accuracy of floating point
>computations - using Rmpfr in R
>>
>> 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.
>
>______________________________________________
>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.
RK
2015-Jul-03 14:16 UTC
[R] : Ramanujan and the accuracy of floating point computations - using Rmpfr in R
Also when I try the following with Rmpfr, it works jut fine.> exp(sqrt(mpfr(163, 120)) * Const("pi", 120))1 'mpfr' number of precision 120 bits [1] 262537412640768743.99999999999925007601 and> exp(sqrt(mpfr(163, 400)) * Const("pi", 400))1 'mpfr' number of precision 400 bits [1] 262537412640768743.99999999999925007259719818568887935385633733699086270 753741037821064791011860731295118134618606450419548 Which compares very nicely with the following: In[10]:= N[Exp[Sqrt[163] Pi], 125] Out[10]= 2.6253741264076874399999999999925007259719818568887935385633733699086270 753741037821064791011860731295118134618606450419308389*10^17 In the multiprecision business, you can never be too certain that you are using the right precision throughout your calculations. Nordlund, Dan (DSHS/RDA <NordlDJ <at> dshs.wa.gov> writes:> > Ravi, > > Take a look at the following link. > > https://code.google.com/p/r-bc/ > > I followed the instructions to get a Windows version of the 'nixutility program , bc (a high precision> calculator), and the source for an R to bc interface. Afterinstalling them, I executed> > exp(sqrt(bc(163))*4*atan(bc(1))) > > in R and got this result > >"262537412640768743.9999999999992500725971981856888793538563373369908627 075374103782106479101186073116295306145602054347"> > I don't know if this is helpful, but ... > > Dan > > Daniel Nordlund, PhD > Research and Data Analysis Division > Services & Enterprise Support Administration > Washington State Department of Social and Health Services >