[R] round() and negative digits

(Ted Harding) ted.harding at wlandres.net
Sun Oct 9 00:11:31 CEST 2011


On 08-Oct-11 21:39:07, Duncan Murdoch wrote:
> On 11-10-08 5:32 PM, Rolf Turner wrote:
>> On 09/10/11 00:18, 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.
>>
>> Uh, wouldn't it be *nice* to mention this --- not completely
>> obvious --- capability in the help file?
> 
> If we told you all of R's secrets, we'd have to kill you.
> 
> Duncan Murdoch

A Fortunate remark??

That being said -- if such are your intentions, then over
my dead body ...

Carl Witthoft's serendipitous discovery is a nice example
of how secrets can be guessed by wondering "what if ... ?".
So probably you don;t need to tell the secrets.

Taking the "negative digits" to their logical extreme:

  round(654.321,2)
  # [1] 654.32
  round(654.321,1)
  # [1] 654.3
  round(654.321,0)
  # [1] 654
  round(654.321,-1)
  # [1] 650
  round(654.321,-2)
  # [1] 700
  round(654.321,-3)
  # [1] 1000
  round(654.321,-4)
  # [1] 0

which is what you'd logically expect (but is it what you
would intuitively expect?).

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <ted.harding at wlandres.net>
Fax-to-email: +44 (0)870 094 0861
Date: 08-Oct-11                                       Time: 23:11:27
------------------------------ XFMail ------------------------------



More information about the R-help mailing list