[R] round() and negative digits

Duncan Murdoch murdoch.duncan at gmail.com
Sun Oct 9 11:44:13 CEST 2011


On 11-10-09 4:00 AM, (Ted Harding) wrote:
> On 09-Oct-11 00:46:58, Carl Witthoft wrote:
>>
>> On 10/8/11 6:11 PM, (Ted Harding) wrote:
>>
>>> 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?).
>>>
>> Oh, oh, somebody's going all metaphysical on us.
>
> Nor should one forget the rounding rules (not OS-dependent
> in this case, I think ... ?):
>
>    round(5000,-4)
>    # [1] 0
>    round(15000,-4)
>    # [1] 20000

The intention is that those are not OS dependent, but since they rely on 
exact representations, there could be differences:  not all platforms 
support the "extended real" 80 bit intermediate representations.  (If 
you were rounding to 0 d.p., they should all agree on a "round to even" 
rule.  Rounding to -4 d.p. involves dividing by 10^4, and that could 
lead to errors.)

Duncan Murdoch



More information about the R-help mailing list