[R] rounding problem

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Mar 2 15:08:30 CET 2009


Prof Brian Ripley wrote:
> 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)

did you say 'bug'?  one could expect that people who turn bright red
when they see the word 'bug' included in the subject or body of a
message sent to r-*, are more careful about using the word themselves.

is this really a bug in excel?  can you point us to where in the excel
documentation they say which of the more than one existing schemes for
rounding numbers is adopted in excel, and which would make it clear that
excel indeed has a *bug* -- behaviour that does not conform to the
documentation? 

or maybe you mean that not adopting the standard you mention is a bug? 
at best, i think, it would be a design flaw, but it may actually be "a
matter of opinion".  there is a long list of both commercial and open
source softwares that expose this 'bug' -- including openoffice calc
(dumb emulation of excel, perhaps?), sage (presumably a bug in python? 
scipy and numpy round 2.5 to 2), gnu octave (and matlab too), maple (but
not mathematica), etc. 

the following simple code shows that there is a bug in the c math
library, too:

    #include <math.h>
    #include <stdio.h>

    int main() {
       printf("round(1.5) = %d\n", round(1.5));
       printf("round(2.5) = %d\n", round(2.5));
       return 0; }


maybe you should report this 'bug' to all those innocent folks who
happen to have it in their products.  it's a true revelation.

vQ



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




More information about the R-help mailing list