Renger van Nieuwkoop
2007-Oct-04 12:44 UTC
[R] Bug or just a problem with the computer precision?
Hi I have the following problem: I have a lot of numbers that identify import goods according to the Harmonised System (8 numbers in two groups with a dot in between). I want to move to the 6 numbers (4 numbers, dot, two numbers). My trick to do this works for all the numbers in the Harmonised Sytem but not for this one: A<-4709.9000 (A<-floor(100 * A ) / 100) = 4709.89 But this has to be 4709.90 If I do this in Excel it works fine...Any idea how to make sure that I get 4709.90 and not 4709.89? Renger _________________________________________________ ECOPLAN Forschung und Beratung in Wirtschaft und Politik Economic Research and Policy Consultancy Thunstrasse 22 / CH-3005 Berne (Switzerland) Phone: +41 31 356 61 61 / Fax: +41 31 356 61 60 mailto:renger at ecoplan.ch / http://www.ecoplan.ch>
You are getting round-off problems (I think it is FAQ 7.33). Use characters since thats what the numbers really are. Convert the characters and then use substring. Also sprintf works:> sprintf("%.2f", A*100)[1] "470990.00"> sprintf("%.2f", A)[1] "4709.90">On 10/4/07, Renger van Nieuwkoop <renger at ecoplan.ch> wrote:> Hi > > I have the following problem: I have a lot of numbers that identify > import goods according to the Harmonised System (8 numbers in two groups > with a dot in between). I want to move to the > 6 numbers (4 numbers, dot, two numbers). My trick to do this works for > all the numbers in the Harmonised Sytem but not for this one: > > A<-4709.9000 > (A<-floor(100 * A ) / 100) = 4709.89 > > But this has to be 4709.90 > > If I do this in Excel it works fine...Any idea how to make sure that I > get 4709.90 and not 4709.89? > > Renger > > > > _________________________________________________ > > ECOPLAN > Forschung und Beratung in Wirtschaft und Politik > Economic Research and Policy Consultancy > > Thunstrasse 22 / CH-3005 Berne (Switzerland) > Phone: +41 31 356 61 61 / Fax: +41 31 356 61 60 > mailto:renger at ecoplan.ch / http://www.ecoplan.ch> > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Prof Brian Ripley
2007-Oct-04 13:44 UTC
[R] Bug or just a problem with the computer precision?
First, I think you are misusing numbers for what are probably character strings. But with numbers,> A <- 4709.9000 > sprintf("%7.2f", A)[1] "4709.90" R will never print a single number as 4709.90: it will drop the trailing zero. With strings:> A <- "4709.9000" > substr(A, 1, 7)[1] "4709.90" On Thu, 4 Oct 2007, Renger van Nieuwkoop wrote:> Hi > > I have the following problem: I have a lot of numbers that identify > import goods according to the Harmonised System (8 numbers in two groups > with a dot in between). I want to move to the > 6 numbers (4 numbers, dot, two numbers). My trick to do this works for > all the numbers in the Harmonised Sytem but not for this one: > > A<-4709.9000 > (A<-floor(100 * A ) / 100) = 4709.89 > > But this has to be 4709.90No, as 4709.9000 is not the exact number represented in R. Consider> A <- 4709.9000 > round(A, 2)[1] 4709.9> 100*A - 470990[1] -5.820766e-11 so A is represented a number slightly less than 4709.9000> If I do this in Excel it works fine...Any idea how to make sure that I > get 4709.90 and not 4709.89?Then Excel would appear to have a different internal representation (or a bug). -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595