[R] - round() strange behaviour

John Logsdon j.logsdon at quantex-research.com
Mon Aug 6 11:46:56 CEST 2007


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.  

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.



-- 
Best wishes

John

John Logsdon                               "Try to make things as simple
Quantex Research Ltd, Manchester UK         as possible but not simpler"
j.logsdon at quantex-research.com              a.einstein at relativity.org
+44(0)161 445 4951/G:+44(0)7717758675       www.quantex-research.com



More information about the R-help mailing list