[R] bug when subtracting decimals?

Duncan Murdoch murdoch at stats.uwo.ca
Tue Apr 21 12:55:27 CEST 2009


On 21/04/2009 3:48 AM, Petr PIKAL wrote:
> Hi
> 
> r-help-bounces at r-project.org napsal dne 20.04.2009 19:01:46:
> 
>> wolfgang.siewert <wolfgang.siewert <at> gmail.com> writes:
>>
>>> There is a way around: 
>>> round(0.7-0.3,1)==0.4
>>> (TRUE)
>>>
>>> Obviously there is a problem with some combinations of decimal 
> subtractions,
>>> that - we have the feeling - shouldt be solved.
>> Oh no, not that one again! This was lecture two in my first computer
>> course in 1968, but it seems to be gone the way of the dodo since than.
> 
> Maybe that is because of Excel is so widespread now and gives expected 
> results (it probably silently rounds all decimal numbers before 
> calculation). 

I don't have Excel, but I expect OpenOffice duplicates its bugs pretty 
well.  And in OpenOffice I see all sorts of bugs due to this, e.g. 
examples where x = y and y = z but x != z, cases where I can calculate a 
number like 1 + 4.e-15 and get something different from 1, but if I 
enter it directly as 1.000000000000004, it gets changed to 1.

So it only gives expected results in some tests, not others.

Duncan Murdoch




More information about the R-help mailing list