From p.dalgaard at biostat.ku.dk  Mon May 29 18:49:09 2006
From: p.dalgaard at biostat.ku.dk (Peter Dalgaard)
Date: 29 May 2006 18:49:09 +0200
Subject: [Rd] Numerical error in R (win32) (PR#8909)
In-Reply-To: <20060529162220.E2ADE3F045@slim.kubism.ku.dk>
References: <20060529162220.E2ADE3F045@slim.kubism.ku.dk>
Message-ID: <x2k684ojy2.fsf@turmalin.kubism.ku.dk>

teck.lim at imperial.ac.uk writes:

> Hi
>     I had observed the following problem in R (also C, Matlab, and Python).
> sprintf('%1.2g\n', 3.15)
> give 3.1 instead of 3.2 whereas an input of 3.75 gives 3.8.
> Java's System.out.printf is ok though.  
>  
> > round(3.75,1)
> [1] 3.8
> > round(3.15,1)
> [1] 3.1
>  
> Similar outcome with sprintf in R.
> 
> 
> However, the right answer should be 3.2

According to what? Remember that we're dealing with finite precision
binary arithmetic here:

>  (3.15 - 3.1)<.05
[1] TRUE
>  abs(3.15 - 3.2)>.05
[1] TRUE

See also FAQ 7.31.
  
> Regards
> Teckpor
> 

-- 
   O__  ---- Peter Dalgaard             ?ster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



[Rd] Numerical error in R (win32) (PR#8909)
Peter Dalgaard