[R] round() and negative digits

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Oct 9 12:18:53 CEST 2011


On Sat, 8 Oct 2011, Duncan Murdoch wrote:

> On 11-10-07 5:26 PM, Carl Witthoft wrote:
>> Just wondering here -- I tested and found to my delight that
>> 
>> %  round(325.4,-2)
>> [1] 300
>> 
>> gave me exactly what I would have expected (and wanted).  Since it's not
>> explicitly mentioned in the documentation that negative 'digits' is
>> allowed, I just wanted to ask whether this behavior is intentional or a
>> happy turn of events.  I'm always paranoid that something not explicitly
>> documented might disappear in future revisons.
>> 
>
> It is intentional, and one of the regression tests confirms that it's there, 
> so it won't disappear by mistake, and would be very unlikely to disappear 
> intentionally.

It needs careful documentation though (as do the corner cases of 
signif).  Things like
> round(325.4,-3)
[1] 0
> signif(325.4,-3)
[1] 300
> signif(325.4,0)
[1] 300
may not be what you expect.

Sometimes it is better not to document things than try to give precise 
details which may get changed *and* there will be useRs who misread 
(and maybe even file bug reports on their misreadings).  The source is 
the ultimate documentation.

-- 
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



More information about the R-help mailing list