[R] Rounding problem R vs Excel

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jun 3 11:13:28 CEST 2003


On Tue, 3 Jun 2003, Mike White wrote:

> If the numbers are not represently exactly how does R resolve problems like
> the one below? Is there something that needs to be set up in the R

What problem exactly?

> environment like the number of significant figures?

Printing is controlled by options("digits"): is that what you had in mind?

> > x<-4.145*100+0.5
> > x
> [1] 415

but
> print(x, digits=16)
[1] 414.9999999999999
> x - 415  # which would be exact if x were integer
[1] -5.684342e-14

> > floor(x)
> [1] 414
> > as.integer(x)
> [1] 414
> > trunc(x)
> [1] 414

All as expected as 414 < x < 415.

Don't confuse the printed representation of an object with the object 
itself.

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