[R] Precision in R

Thierry Onkelinx thierry.onkelinx at inbo.be
Mon Feb 26 09:06:00 CET 2018


This is described in R FAQ 7.31


ir. Thierry Onkelinx
Statisticus / Statistician

Vlaamse Overheid / Government of Flanders
INSTITUUT VOOR NATUUR- EN BOSONDERZOEK / RESEARCH INSTITUTE FOR NATURE AND
FOREST
Team Biometrie & Kwaliteitszorg / Team Biometrics & Quality Assurance
thierry.onkelinx at inbo.be
Havenlaan 88 bus 73, 1000 Brussel
www.inbo.be

///////////////////////////////////////////////////////////////////////////////////////////
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of. ~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data. ~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of data.
~ John Tukey
///////////////////////////////////////////////////////////////////////////////////////////

<https://www.inbo.be>

2018-02-26 2:34 GMT+01:00 Iuri Gavronski <iuri at proxima.adm.br>:

> Hi,
>
> Why sum() on a 10-item vector produces a different value than its
> counterpart on a 2-item vector? I understand the problems related to
> the arithmetic precision in storing decimal numbers in binary format,
> but shouldn't the errors be equal regardless of the method used?
>
> See my example:
>
> > options(digits=22)
> > x=rep(.1,10)
> > x
>  [1] 0.10000000000000001 0.10000000000000001 0.10000000000000001
>  [4] 0.10000000000000001 0.10000000000000001 0.10000000000000001
>  [7] 0.10000000000000001 0.10000000000000001 0.10000000000000001
> [10] 0.10000000000000001
> > sum(x)
> [1] 1
> > y=0
> > for (i in 1:10) {y=sum(y+x[i])}
> > y
> [1] 0.99999999999999989
> > y*10^6
> [1] 999999.99999999988
> > sum(x)*10^6
> [1] 1e+06
> > z=.1+.1+.1+.1+.1+.1+.1+.1+.1+.1
> > z
> [1] 0.99999999999999989
> >
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list