[R] R wrong, Python rigth in calcution

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed Sep 18 00:11:26 CEST 2019


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



More information about the R-help mailing list