[R] %in% not working
Barry Rowlingson
B.Rowlingson at lancaster.ac.uk
Mon Nov 4 16:00:48 CET 2002
Neil Klepeis wrote:
> Thanks to Thomas and everyone on my floating point confusion,
>
> Is there any reason why conversion to 'character' wouldn't be the safest
> way to compare decimal numbers:
>
> > 1.253 %in% seq(1.1,1.3,by=0.001)
> [1] FALSE
> > 1.253 %in% as.character(seq(1.1,1.3,by=0.001))
> [1] TRUE
> > 0.001 %in% 1E-3
> [1] TRUE
> > "0.001" %in% "1E-3"
> [1] FALSE
> > as.character(0.001) %in% as.character(1E-3)
> [1] TRUE
Impressive, but:
> as.character(1e-4) == as.character(0.0001+.000000000000000000000001)
[1] TRUE
- as.character gets it wrong -
Wheras:
> 1e-4 == 0.0001+.0000000000000000001
[1] FALSE
- plain old numbers gets it right.
Far better to understand what is going on with floating point numbers
and their inherent imprecision, and then work round it!
Baz
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list