Re: [R] problem with basic boolean selection in sequence

From: <Richard.Cotton_at_hsl.gov.uk>
Date: Wed, 09 Apr 2008 10:45:25 +0100


> I have a surprising problem while selecting values in a sequence created

> with the seq() function...

>...

> > test2<-seq(from=0,to=1,by=.1)
>...

> > test2==0.3
> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
>...
> Does anyones has an explanation and a solution ?

I suspect that this is a problem with floating point rounding errors. The values in test2 are not exactly what you requested: format(test2, nsmall=20)

#[1] "0.00000000000000000000" "0.10000000000000000555" 
"0.20000000000000001110"
#[4] "0.30000000000000004441" "0.40000000000000002220"
"0.50000000000000000000"

#[7] "0.60000000000000008882" "0.70000000000000006661" "0.80000000000000004441"
#[10] "0.90000000000000002220" "1.00000000000000000000"

A workaround is to check that the difference between test2 and the target value is very small:
abs(test2 - .3) < .Machine$double.eps
# [1] FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE Regards,
Richie.

Mathematical Sciences Unit
HSL



ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 09 Apr 2008 - 12:24:17 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 09 Apr 2008 - 12:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive