[R] problem with basic boolean selection in sequence
Richard.Cotton at hsl.gov.uk
Richard.Cotton at hsl.gov.uk
Wed Apr 9 11:45:25 CEST 2008
> 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}}
More information about the R-help
mailing list