donahchoo at me.com
2009-Dec-31 03:41 UTC
[R] what don't I get about numeric/double comparisons in R way?
Hi, I'm pretty much an R noob and I'm missing some paradigm in R I think. I can't figure our how to compare numerics. here's a transcript of my tests. Any pointers? > print(range_sd) [1] 34.40783 > is.numeric(range_sd) [1] TRUE > is.numeric(foo) [1] TRUE > is.double(range_sd) [1] TRUE > is.double(foo) [1] TRUE > > identical(range_sd, foo) [1] FALSE > identical(range_sd, 34.40783) [1] FALSE > identical(range_sd, 34.40783, num.eq=FALSE) [1] FALSE > identical(range_sd, 34.40783, num.eq=TRUE) [1] FALSE > library(RUnit) Warning message: package 'RUnit' was built under R version 2.10.1 > checkEquals(range_sd, 34.40783) Error in checkEquals(range_sd, 34.40783) : Mean relative difference: 5.79431e-08 > checkEquals(range_sd, foo) Error in checkEquals(range_sd, foo) : Mean relative difference: 5.79431e-08 > if( range_sd == 34.40783 ) { print("foo")} > if( range_sd == "34.40783" ) { print("foo")} > if( range_sd != "34.40783" ) { print("foo")} [1] "foo" > all.equal(range_sd, 34.40783) [1] "Mean relative difference: 5.79431e-08"
Jim Lemon
2009-Dec-31 04:10 UTC
[R] what don't I get about numeric/double comparisons in R way?
On 12/31/2009 02:41 PM, donahchoo at me.com wrote:> Hi, > > I'm pretty much an R noob and I'm missing some paradigm in R I think. > I can't figure our how to compare numerics. here's a transcript of my > tests. Any pointers?Hi donahchoo, You're comparing the printed value of range_sd, which has been truncated, to the actual value. As the printout says, the difference is small, but present. If you set range_sd to the printed value: range_sd<-34.40783 the comparisons will return TRUE. Jim