[R] round giving different results on Windows and Mac

Ruth Ripley ruth at stats.ox.ac.uk
Sat Mar 10 15:49:41 CET 2012


Petr,

Many thanks for this detailed explanation. It seems that the printing is 
going to vary because it is not done by R. I will try alternative 
numbers of significant digits: I had set options(digits=4) in an attempt 
to avoid inter-platform printing differences, without really 
understanding what was causing them.

Ruth

>
> -------- Original Message --------
> Subject: Re: [R] round giving different results on Windows and Mac
> Date: Sat, 10 Mar 2012 10:08:21 +0100
> From: Petr Savicky <savicky at cs.cas.cz>
> To: r-help at r-project.org
>
> On Fri, Mar 09, 2012 at 09:34:14PM +0000, Ruth Ripley wrote:
>> Dear all,
>>
>> I have been running some tests of my package RSiena on different
>> platforms and trying to reconcile the results.
>>
>> With Mac, the commands
>>
>> options(digits=4)
>> round(1.81652, digits=4)
>>
>> print 1.817
>>
>> With Windows, the same commands print 1.816
>>
>> I am not bothered which answer I get, but it would be nice if they were
>> the same. A linux box agreed with the Mac.
>
> Hi.
>
> I obtain the same difference between Linux (1.817) and
> 32 bit Windows (1.816). As Duncan said, the number 1.8165
> is not exactly representable and printing it to 4
> significant digits may depend on the platform, since
> it is a middle case.
>
> Note that options(digits=4) means rounding to 4 significant
> digits, while round(1.81652, digits=4) is rounding to 4
> digits in the fractional part. Try signif(1.81652, digits=4)
> to get the same type of rounding as in options(digits=4).
>
> The problem is not in round(), since
>
> x <- round(1.81652, digits=4)
> print(x, digits=20)
> print(x, digits=4)
>
> yields on Linux
>
> [1] 1.8165000000000000036
> [1] 1.817
>
> and on 32 bit Windows
>
> [1] 1.8165000000000000036
> [1] 1.816
>
> The difference is not due to R, since R is responsible
> only for the choice of the number of printed digits
> and not for the digits themselves. The digits are computed
> by sprintf() on the given platform. So, the difference
> seems to be there.
>
> The command
>
> sprintf("%5.3f", 18165/10000)
>
> yields on Linux
>
> [1] "1.817"
>
> and on 32 bit Windows
>
> [1] "1.816"
>
> Thank you for the example.
>
> Petr Savicky.
>
> ______________________________________________
> 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.

-- 
Ruth M. Ripley,                         Email:ruth at stats.ox.ac.uk
Dept. of Statistics,                    http://www.stats.ox.ac.uk/~ruth/
University of Oxford,                   Tel:   01865 282857
1 South Parks Road, Oxford OX1 3TG, UK  Fax:   01865 272595



More information about the R-help mailing list