# Precision in R

4 messages
Open this post in threaded view
|

## Precision in R

 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 > ______________________________________________ [hidden email] mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|

## Re: Precision in R

 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 [hidden email] 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 /////////////////////////////////////////////////////////////////////////////////////////// 2018-02-26 2:34 GMT+01:00 Iuri Gavronski <[hidden email]>: > 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 > > > > ______________________________________________ > [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-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.