R wrong, Python rigth in calcution

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

R wrong, Python rigth in calcution

Martin Møller Skarbiniks Pedersen
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R wrong, Python rigth in calcution

Ivan Krylov
On Wed, 18 Sep 2019 00:02:47 +0200
Martin Møller Skarbiniks Pedersen <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R wrong, Python rigth in calcution

Duncan Murdoch-2
In reply to this post by Martin Møller Skarbiniks Pedersen
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R wrong, Python rigth in calcution

Richard M. Heiberger
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 <[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R wrong, Python rigth in calcution

Abby Spurdle
In reply to this post by Ivan Krylov
> 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...

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: R wrong, Python rigth in calcution

Richard O'Keefe-2
Here's a tip for the original poster.
> ?numeric
and then follow the link it suggests
> ?double
which says amongst other things
     All R platforms are required to work with values conforming to the
     IEC 60559 (also known as IEEE 754) standard.  This basically works
     with a precision of 53 bits, and represents to that precision a
     range of absolute values from about 2e-308 to 2e+308.
and reminds us that
> .Machine
will give you the parameters of the 'double' type.


On Wed, 18 Sep 2019 at 12:03, Abby Spurdle <[hidden email]> wrote:

> > 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...
>
> ______________________________________________
> [hidden email] 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.
>

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.