Message-ID: <amundv8isi17k150le62u0m38v2v0kvfdo@4ax.com>
Date: 2003-06-03T01:40:41Z
From: Duncan Murdoch
Subject: Rounding problem R vs Excel
In-Reply-To: <5A1B0066-955D-11D7-9356-000393678426@cmu.edu>
On Mon, 2 Jun 2003 20:50:20 -0400, you wrote:
>Does this script do what you want?
>
>cround <- function(x,digits=0) {
> a <- ifelse(x>0,.5,-.5)
> if (digits==0) {
> floor(x+a)
> } else {
> m <- 10^digits
> floor(x*m+a)/m
> }
>}
No, the problem is that R uses binary formats, and some numbers aren't
representable there. So for example,
> cround(0.145,2)
[1] 0.14
because 0.145 isn't representable exactly, and is actually being
represented as 0.14999999999 or something similar.
Duncan Murdoch