[R] Problem Matching Exact Values

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Wed Jun 9 23:07:04 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Brigid Mooney
> Sent: Wednesday, June 09, 2010 1:49 PM
> To: r-help at r-project.org
> Subject: [R] Problem Matching Exact Values
> 
> 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?

Yes, it is a finite precision problem, due to converting decimal to binary floating-point.  See FAQ 7.31

> 
> 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?.
> 

Hope this is helpuf,

Dan


Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204




More information about the R-help mailing list