Hi all, I have a very simple code, but gives a wrong answer. a=1000 b=1000^(1/3) c=ceiling(a/b) then c=101, if change the code to be a=1000 b=10 c=ceiling(a/b) then c=100 is fine. Thank you for the help. [[alternative HTML version deleted]]
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Ying Zheng > Sent: Wednesday, November 28, 2012 6:11 PM > To: r-help at r-project.org > Subject: [R] Ceiling function gives me wrong answer > > Hi all, > > I have a very simple code, but gives a wrong answer. > > a=1000 > b=1000^(1/3) > c=ceiling(a/b) > > then c=101, > > if change the code to be > a=1000 > b=10 > c=ceiling(a/b) > > then c=100 is fine. > > Thank you for the help. >This is another variation on FAQ 7.31. You are trying to calculate the cube root using a power of 1/3. But 1/3 cannot be represented exactly in a finite binary floating-point system. If you print the value of b with enough digits you will see that it is not equal to 10 because the representation of the value 1/3 ends up being slightly smaller that one-third.> print(1/3, digits=20)[1] 0.33333333333333331483> > b <- 1000^(1/3) > print(b, digits=20)[1] 9.9999999999999982236 Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA
not sure ceiling() is behaving inappropriately..> b <- 1000^( 1/3 ) > b[1] 10> options( digits = 22 ) > b[1] 9.999999999999998223643 check out http://blog.revolutionanalytics.com/2009/11/floatingpoint-errors-explained.htmlfor more detail On Wed, Nov 28, 2012 at 9:10 PM, Ying Zheng <martin.yingzheng@gmail.com>wrote:> Hi all, > > I have a very simple code, but gives a wrong answer. > > a=1000 > b=1000^(1/3) > c=ceiling(a/b) > > then c=101, > > if change the code to be > a=1000 > b=10 > c=ceiling(a/b) > > then c=100 is fine. > > Thank you for the help. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On 29-11-2012, at 03:10, Ying Zheng wrote:> Hi all, > > I have a very simple code, but gives a wrong answer. > > a=1000 > b=1000^(1/3) > c=ceiling(a/b) > > then c=101, > > if change the code to be > a=1000 > b=10 > c=ceiling(a/b) > > then c=100 is fine.See R FAQ 7.31 "Why doesn't R think these numbers are equal?"> sprintf("%.17f",1000/10)[1] "100.00000000000000000"> sprintf("%.17f",1000/1000^(1/3))[1] "100.00000000000001421" Berend