Why does: > numeric(0.2*25) return [1] 0 0 0 0 0 but > numeric((1-0.8)*25) returns [1] 0 0 0 0 [running version 3.2.0] [Apologies if this has been asked before - it's a hard question to find specific search terms for] Thanks, Scott
> numeric(0.2*25)[1] 0 0 0 0 0> numeric((1-0.8)*25)[1] 0 0 0 0> print(c(0.2*25, (1-0.8)*25), digits=17)[1] 5.0000000000000000 4.9999999999999991> 0.2*25 - (1-0.8)*25[1] 8.881784e-16> as.integer(c(0.2*25, (1-0.8)*25), digits=17)[1] 5 4> ## See FAQ 7.31 for other examples of floating point behavior >On Wed, Apr 27, 2016 at 3:29 PM, Scott Rifkin <sarifkin at ucsd.edu> wrote:> Why does: > >> numeric(0.2*25) > > return > > [1] 0 0 0 0 0 > > but > >> numeric((1-0.8)*25) > > returns > > [1] 0 0 0 0 > > [running version 3.2.0] > > > [Apologies if this has been asked before - it's a hard question to find > specific search terms for] > > Thanks, > Scott > > ______________________________________________ > 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.
FAQ 7.31 0.2 is slightly larger than 1/5, and 1-0.8 is slightly smaller. The argument to numeric has to be an integer, so fractions are discarded. This behavior is not unique to R. -- Sent from my phone. Please excuse my brevity. On April 27, 2016 8:29:13 PM GMT+01:00, Scott Rifkin <sarifkin at ucsd.edu> wrote:>Why does: > > > numeric(0.2*25) > >return > >[1] 0 0 0 0 0 > >but > > > numeric((1-0.8)*25) > >returns > >[1] 0 0 0 0 > >[running version 3.2.0] > > >[Apologies if this has been asked before - it's a hard question to find > >specific search terms for] > >Thanks, >Scott > >______________________________________________ >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]]