[R] Problem Matching Exact Values

Brigid Mooney bkmooney at gmail.com
Wed Jun 9 22:48:36 CEST 2010


Sorry for the basic question - bur I ran into something I haven't
noticed before and would appreciate a little more perspective on my
problem.

I am using R to determine if various thresholds are hit (or surpassed)
in a data set.  If a threshold is surpassed, I have had no problems
identifying it.  However, when the threshold is matched *exactly*, not
all cases are being identified.

Please consider the following example, with base value of x = 59000
and threshold of 10% - so the target to hit is 59000*1.1 = 64900.

> x <- 59000
> thresh <- 0.10
>
> target <- x*(1+thresh)
> target
[1] 64900
>
>
> target == 64900
[1] FALSE
>
>
> target-64900
[1] 7.275958e-12


Why is there this (very) small difference in the value of target and
the numeric 64900?  Is this using a floating point system or something
else that I'm not understanding?

Is using round() the best work-around in cases such as these - or is
there a better (perhaps more accurate) way to classify data in cases
such as this - avoiding whatever floating points are taking place in
the background in the first place?.

I'm using an older version of R if that matters at all...
 R.version
               _
platform       i386-pc-mingw32
arch           i386
os             mingw32
system         i386, mingw32
status
major          2
minor          8.1
year           2008
month          12
day            22
svn rev        47281
language       R
version.string R version 2.8.1 (2008-12-22)


Thanks in advance,
Brigid



More information about the R-help mailing list