Message-ID: <20110124073925.GA27470@praha1.ff.cuni.cz>
Date: 2011-01-24T07:39:25Z
From: Petr Savicky
Subject: sensitivity logical operators in R
In-Reply-To: <4D3CA7F7.1030601@gmx.de>
On Sun, Jan 23, 2011 at 11:13:11PM +0100, Marc Jekel wrote:
> Hello R Fans,
>
> Another question for the community that really frightened me today. The
> following logical comparison produces a "false" as output:
>
> t = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,1,-1,-1,1))
> tt = sum((c(.7,.69,.68,.67,.66)-.5)*c(1,-1,1,1,-1))
>
> t == tt
>
> This is really strange behavior. Most likely this has something to do
> how R represents numbers internally and the possible sensitivity of a
> computer? Does anyone know when this strange behavior occurs and how to
> fix it?
The number 0.7 has infinite expansion in binary
0.1011001100110011001100110011...
so is rounded in the standard numeric data type, which is used for
speed needed in complex computations. If you know in advance that
the result has at most 2 decimal positions, then round(, digits=2)
yields the correct comparison
round(t, 2) == round(tt, 2)
# [1] TRUE
athough 0.2 is also not exactly representable. Both sides are rounded
to the same representable number.
See also
http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy
for other examples.
Petr Savicky.