Re: [R] one is not one

From: Andrew Robinson <A.Robinson_at_ms.unimelb.edu.au>
Date: Sat 21 Oct 2006 - 09:14:30 GMT

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@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/

______________________________________________
R-help@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.
Received on Sun Oct 22 03:00:28 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 21 Oct 2006 - 17:30:11 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.