[R] rounding problem

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Mar 2 08:38:03 CET 2009


I think your subject line should read 'Excel bug'.  From the R help 
for round()

      Note that for rounding off a 5, the IEC 60559 standard is expected
      to be used, '_go to the even digit_'.

In case you did not recognize it, IEC 60559 is an international 
standard: Excel is not.

R is Open Source and so you can modify it to emulate the bugs in 
other software: that is not one of the aims of its developers so 
please don't expect us to do so for you.

It is rare for round() to be called explicitly in R code: rounding is 
usually going on inside print routines.  But a version of round that 
comes close to always rounding away from zero is

excel_round <- function(x, digits) round(x*(1+1e-15), digits)

On Sun, 1 Mar 2009, tedzzx wrote:

>
> Yes, round(1.5)=2. but round(2.5)=2.  I want round(2.5)=3 just like the what
> the excel do.  Can we change the setting or do some trick so that the
> computer will work like what we usually do with respect to rounding.
> My system is R 2.8.1, winXP, Intel core 2 dual . Thanks.
>
>
> Daniel Nordlund-2 wrote:
>>
>>> -----Original Message-----
>>> From: r-help-bounces at r-project.org
>>> [mailto:r-help-bounces at r-project.org] On Behalf Of tedzzx
>>> Sent: Saturday, February 28, 2009 4:58 AM
>>> To: r-help at r-project.org
>>> Subject: [R] rounding problem
>>>
>>>
>>> Hi all,
>>>
>>> According to the help page on round(), round(1.5) could be
>>> either 1 or 2.
>>> But I want to the answere to be 2 for sure just what we
>>> usually do. How can
>>> I do that? Thanks advance.
>>>
>>> Cheers
>>>
>>> Ted
>>> --
>>
>> Ted,
>>
>> Actually, the documentation (at least for R-2.8.1) doesn't say that.  The
>> number 1.5 can be represented exactly on most systems that I know, and
>> therefore it will round to the even digit, i.e. round(1.5) = 2.0 .  The
>> documentation says that 0.15 cannot be represented exactly, and therefore
>> whether it rounds to 0.1 or 0.2 depends on the OS and the machine
>> architecture.  So on my WinXP Pentium IV system, 1.5 rounds to 2.0 and it
>> also happens that round(0.15, 1) equals 0.2  .  You say you want 1.5 to
>> round to 2.0.  What would you like the result of round(2.5) to equal?
>>
>> Dan
>>
>> Daniel Nordlund
>> Bothell, WA USA
>>
>> ______________________________________________
>> R-help at r-project.org 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.
>>
>>
>
> -- 
> View this message in context: http://www.nabble.com/rounding-problem-tp22261852p22280785.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org 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.
>

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