Ceiling function gives me wrong answer
-----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