Skip to content
Back to formatted view

Raw Message

Message-ID: <Pine.LNX.4.44.0306031008100.7851-100000@gannet.stats>
Date: 2003-06-03T09:13:28Z
From: Brian Ripley
Subject: Rounding problem R vs Excel
In-Reply-To: <00b701c329ad$14ef6460$ad002850@FSSFQCV7BGDVED>

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