[R] one is not one

Andrew Robinson A.Robinson at ms.unimelb.edu.au
Sat Oct 21 11:14:30 CEST 2006


Patrick,

the problem arises because computers can't exactly represent real
numbers.  Try this for your test:

if(!all.equal(sum(x),1)) print("lost") else ("OK")

see FAQ

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

I hope that this helps

Andrew


On Sat, Oct 21, 2006 at 11:04:13AM +0200, Patrick Giraudoux wrote:
> Folks,
> 
> I have got a strange behaviour when testing this:
> 
> sum(x) != 1
> 
> let us set
> 
> x<-c(70,134,1,5,0)
> 
> and transform it in a vector of probabilities
> 
> x<-x/sum(x)
> 
> One expect  sum(x) should be equal to 1, which is apparently the case
> 
> > sum(x)
> [1] 1
> 
> However, when I try to test it I get:
> 
> > if(sum(x) !=1) print("lost") else ("OK")
> [1] "lost"
> 
> Which means that actually sum(x) is NOT considered equal to 1...
> 
> Any idea about what is going wrong?
> 
> Patrick
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.

-- 
Andrew Robinson  
Department of Mathematics and Statistics            Tel: +61-3-8344-9763
University of Melbourne, VIC 3010 Australia         Fax: +61-3-8344-4599
http://www.ms.unimelb.edu.au/~andrewpr
http://blogs.mbs.edu/fishing-in-the-bay/



More information about the R-help mailing list