[R] Comparing decimal numbers

Petr Savicky savicky at praha1.ff.cuni.cz
Tue Feb 1 16:39:55 CET 2011


On Tue, Feb 01, 2011 at 05:49:10AM -0800, mlancee wrote:
> 
> Hi,
> 
> I have a seemingly easy question that has been keeping be busy for quite a
> while. The problem is the following:
> 
> 0.1 + 0.1 + 0.1 == 0.3
> [1] FALSE
> 
> Why is this false? Another example is
> 
> 0.2 + 0.1 == 0.3
> [1] FALSE
> 
> or
> 
> 0.25 + 0.05 == 0.20 + 0.10
> [1] FALSE
> 
> However, I do get TRUE if I use integers, or for example the following
> 
> 0.1 + 0.1 == 0.2
> [1] TRUE
> 
> It is probably something very basic, but I did not manage to find the
> answer.

Hi.

The reason is that a number, which has a finite expansion as a decimal
number, need not have a finite expansion as a binary number. Besides FAQ 7.31,
see also
  http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy 
for further examples and some hints.

Petr Savicky.



More information about the R-help mailing list