Hi! I'm running R version 2.13.0 (2011-04-13) Platform: i386-pc-mingw32/i386 (32-bit) When i type in the command: sum(c(-0.2, 0.8, 0.8, -3.2, 1.8)) R returns the value: -5.551115e-17 Why doesn't R return zero in this case? There shouldn't be any rounding error in a simple sum. Thanks, Mark
Of course there's rounding error: your computer can't store those decimal numbers precisely. See R FAQ 7.31 for details. See also: sum(10*c(-0.2, 0.8, 0.8, -3.2, 1.8)) / 10 Sarah On Thu, Mar 1, 2012 at 4:49 PM, Mark A. Albins <kamokoi55 at gmail.com> wrote:> Hi! > > I'm running R version 2.13.0 (2011-04-13) > Platform: i386-pc-mingw32/i386 (32-bit) > > When i type in the command: > > sum(c(-0.2, 0.8, 0.8, -3.2, 1.8)) > > R returns the value: > > -5.551115e-17 > > Why doesn't R return zero in this case? ?There shouldn't be any rounding > error in a simple sum. > > Thanks, > > Mark-- Sarah Goslee http://www.functionaldiversity.org
In base ten, using any fixed number of digits, compute 1/3 + 1/3 + 1/3 (doing the divisions before the additions). Why isn't it 1? 1/5 has the same sort of problem in base two. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Mark A. Albins > Sent: Thursday, March 01, 2012 1:50 PM > To: r-help at r-project.org > Subject: [R] problem with sum function > > Hi! > > I'm running R version 2.13.0 (2011-04-13) > Platform: i386-pc-mingw32/i386 (32-bit) > > When i type in the command: > > sum(c(-0.2, 0.8, 0.8, -3.2, 1.8)) > > R returns the value: > > -5.551115e-17 > > Why doesn't R return zero in this case? There shouldn't be any rounding > error in a simple sum. > > Thanks, > > Mark > > ______________________________________________ > R-help at r-project.org mailing list > 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.
On Thu, Mar 01, 2012 at 01:49:44PM -0800, Mark A. Albins wrote:> Hi! > > I'm running R version 2.13.0 (2011-04-13) > Platform: i386-pc-mingw32/i386 (32-bit) > > When i type in the command: > > sum(c(-0.2, 0.8, 0.8, -3.2, 1.8)) > > R returns the value: > > -5.551115e-17 > > Why doesn't R return zero in this case? There shouldn't be any rounding > error in a simple sum.Hi. There is a rounding error, since numerical values are represented in binary system and, for example, 0.2 = 1/5 cannot be represented in binary exactly. A simpler version is 0.1 + 0.2 - 0.3 [1] 5.551115e-17 Use round(), for example round(sum(c(-0.2, 0.8, 0.8, -3.2, 1.8)), digits=7) [1] 0 See FAQ 7.31 and/or http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy for further hints. Hope this helps. Petr Savicky.
Others explained why it happens, but you might want to look at the zapsmall function for one way to deal with it. On Thu, Mar 1, 2012 at 2:49 PM, Mark A. Albins <kamokoi55 at gmail.com> wrote:> Hi! > > I'm running R version 2.13.0 (2011-04-13) > Platform: i386-pc-mingw32/i386 (32-bit) > > When i type in the command: > > sum(c(-0.2, 0.8, 0.8, -3.2, 1.8)) > > R returns the value: > > -5.551115e-17 > > Why doesn't R return zero in this case? ?There shouldn't be any rounding > error in a simple sum. > > Thanks, > > Mark > > ______________________________________________ > R-help at r-project.org mailing list > 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.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com