[R] round giving different results on Windows and Mac

Petr Savicky savicky at cs.cas.cz
Sat Mar 10 10:08:21 CET 2012


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.



More information about the R-help mailing list