[R] R wrong, Python rigth in calcution
Richard M. Heiberger
rmh @end|ng |rom temp|e@edu
Wed Sep 18 00:26:45 CEST 2019
Your numbers are 70 bits long, R double precision numbers are 53 bits long.
You need Rmpfr to get the higher precision.
> log(569936821221962380720, 2)
[1] 68.94936
> print(569936821221962380720, digits=22)
[1] 569936821221962350592
> library(Rmpfr)
> mpfr("569936821221962380720", 70)
1 'mpfr' number of precision 70 bits
[1] 569936821221962380720
>
> mpfr("569936821221962380720", 210)^3 + (mpfr("-569936821113563493509", 210))^3 + (mpfr("-472715493453327032", 210))^3
1 'mpfr' number of precision 210 bits
[1] 3
See FAQ 7.31 and the help files for Rmpfr
Rich
On Tue, Sep 17, 2019 at 6:13 PM Duncan Murdoch <murdoch.duncan using gmail.com> wrote:
>
> On 17/09/2019 6:02 p.m., Martin Møller Skarbiniks Pedersen wrote:
> > Hi,
> > I don't understand why R computes this wrong.
>
> This is pretty well documented. R uses double precision floating point
> values for these expressions, which have about 15 digit precision. I
> believe for whole numbers Python uses variable size integer values, so
> should get integer calculations exactly right.
>
> You can also tell R to use exact 32 bit integer calculations, but your
> values are too big for that, so it wouldn't work in this example.
>
>
>
> I know I can use gmp and
> > R will do it correctly.
> >
> > $ echo '569936821221962380720^3 + (-569936821113563493509)^3 +
> > (-472715493453327032)^3' | Rscript - [1] -4.373553e+46
> > Correct answer is 3 and Python can do it:
> >
> > $ echo
> > 'pow(569936821221962380720,3)+pow(-569936821113563493509,3)+pow(-472715493453327032,3)'|python3
> > 3
> >
> > [[alternative HTML version deleted]]
>
> Please don't post HTML to the list -- it's a plain text list. That's
> also pretty well documented.
>
> Duncan Murdoch
>
> ______________________________________________
> R-help using 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