Martin Møller Skarbiniks Pedersen
2019-Sep-17 22:02 UTC
[R] R wrong, Python rigth in calcution
Hi, I don't understand why R computes this wrong. 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]]
On Wed, 18 Sep 2019 00:02:47 +0200 Martin M?ller Skarbiniks Pedersen <traxplayer at gmail.com> wrote:> I know I can use gmp and R will do it correctly.Which is equivalent to what Python does: it uses so-called long arithmetic, allowing scalar variables with as many digits as it fits in the computer memory. R by default uses floating-point arithmetic, which is subject to problems described in [*]. -- Best regards, Ivan [*] https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf or https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
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
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))^31 '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 at 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 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 by default uses floating-point arithmetic, which > is subject to problems described in [*].Yes. I want to note that both graphics and modern statistics, require efficient floating point arithmetic. So, R does what it's designed to do...