From murdoch at stats.uwo.ca  Mon May 29 18:40:51 2006
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Mon, 29 May 2006 12:40:51 -0400
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: <447B2413.1@stats.uwo.ca>

On 5/29/2006 12:22 PM, teck.lim at imperial.ac.uk wrote:
> 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

This is not a bug.  There is no way to represent 3.15 exactly in double 
precision, so it is hard to predict whether it will round up or down. 
Apparently on the machine you were using it is represented as a number 
slightly less than 3.15, which rounds down.

Duncan Murdoch



[Rd] Numerical error in R (win32) (PR#8909)
Duncan Murdoch