[R] - round() strange behaviour
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Aug 6 13:02:42 CEST 2007
On 06/08/2007 5:46 AM, John Logsdon wrote:
> In R you may be tempted to use rounding to index something in an array. Hence
> my concern and suggestion that there be another argument to round() (and
> signif()) that explicitly sets the rounding mechanism.
>
> In Fortran IDNINT does as I would expect. You could do floor(x+0.5) but many
> people, myself included before this conversation, will unwittingly round and
> it will almost always work but the exception is something that may even be
> difficult to reproduce where random numbers are involved without setting the
> seed.
As you mention, where you want to round 0.5 upwards, you can use the
function
roundup <- function(x, digits=0) {
factor <- 10^digits
trunc(x*factor + 0.5)/factor
}
But beware of floating point representations: most decimal numbers
ending in 5 can't be represented exactly, so you'll see oddities like this:
> roundup(2.45, 1)
[1] 2.5
> roundup(2.4 + 0.05, 1)
[1] 2.4
which is partially explained by this:
> 2.45 - (2.4 + 0.05)
[1] 4.440892e-16
I suspect the same sort of thing would happen in Fortran's IDNINT though
it might be harder to see, because most Fortran compilers do fairly
substantial optimizations, and it might be that they actually store 2.4
+ 0.05 and 2.45 as the same constant. So I don't think round() leaves
any new traps for the unwary: if x = 0.5 is the result of a
calculation, it might actually be stored as 0.49999999999 and round(x)
should return 0 even using roundup().
Duncan Murdoch
> In this context you cannot leave rounding until the end of course! Clearly in
> an arithmetic context, rounding should always be left to the end but I don't
> think that is to avoid rounding-to-even but just to avoid unnecessary loss of
> precision in the calculation. Ask most engineers - and even physicists - and
> I suspect they would not be aware of this debate even though it dates from
> the 1940s.
>
> On Monday 06 August 2007 10:31:22 Jim Lemon wrote:
>> (Ted Harding) wrote:
>>> ...
>>> Thus "always up" or "always down" means that the difference between
>>> rounded numbers is always the same as between the unrounded numbers
>>> which for other methods the differences can differ by 2.0.
>>> This may or may not matter, but it is something to think about
>>> when choosing a rounding method.
>> Ah, but Ted, this is why, as I remarked in a private message to the
>> original poster, we were also taught never to round until the final
>> result. Had I not had a rather extended debate with an engineer who
>> apparently thought that one could get away with successive rounding, I
>> would not be so sensitive on the issue.
>>
>> Jim
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch 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.
>
>
>
More information about the R-help
mailing list