An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101220/35415490/attachment.pl>
sample() issue
8 messages · cory n, David Winsemius, Peter Langfelder +4 more
On Dec 20, 2010, at 2:04 PM, cory n wrote:
length(sample(25000, 25000*(1-.55)))
[1] 11249
25000*(1-.55)
[1] 11250
length(sample(25000, 11250))
[1] 11250
length(sample(25000, 25000*.45))
[1] 11250 So the question is, why do I get 11249 out of the first command and not 11250? I can't figure this one out.
Read the FAQ: > 25000*(1-.55)-11250 [1] -1.818989e-12 > 25000*(1-.55)< 11250 [1] TRUE ?round ?all.equal ?zapsmall
David Winsemius, MD West Hartford, CT
See FAQ 7.31: http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f And try this: 25000*(1-.55) - 11250 25000*.45 - 11250 Notice a difference? Best, -- Wolfgang Viechtbauer Department of Psychiatry and Neuropsychology School for Mental Health and Neuroscience Maastricht University, P.O. Box 616 6200 MD Maastricht, The Netherlands Tel: +31 (43) 368-5248 Fax: +31 (43) 368-8689 Web: http://www.wvbauer.com ----Original Message---- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of cory n Sent: Monday, December 20, 2010 20:04 To: r-help at r-project.org Subject: [R] sample() issue
length(sample(25000, 25000*(1-.55)))
[1] 11249
25000*(1-.55)
[1] 11250
length(sample(25000, 11250))
[1] 11250
length(sample(25000, 25000*.45))
[1] 11250
So the question is, why do I get 11249 out of the first command and not
11250? I can't figure this one out.
Thanks
Cory
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org 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.
On Mon, Dec 20, 2010 at 11:04 AM, cory n <corynissen at gmail.com> wrote:
length(sample(25000, 25000*(1-.55)))
[1] 11249
25000*(1-.55)
[1] 11250
length(sample(25000, 11250))
[1] 11250
length(sample(25000, 25000*.45))
[1] 11250 So the question is, why do I get 11249 out of the first command and not 11250? ?I can't figure this one out.
Let me make a wild guess:
floor(25000*(1-.55))
[1] 11249
25000*(1-.55)
[1] 11250
25000*(1-.55) - 11250
[1] -1.818989e-12
25000*0.45 - 11250
[1] 0 I'm guessing that the machine representation of 0.55 is off by something like 1e-16, which gets multiplied by a lot (25000) and this is enough for the floor (or whatever rounding the internal code uses) to make it 11249. The morale of the story is do not multiply non-exact numbers by huge constants or you may get small inaccuracies. Peter
-----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- project.org] On Behalf Of cory n Sent: Monday, December 20, 2010 11:04 AM To: r-help at r-project.org Subject: [R] sample() issue
length(sample(25000, 25000*(1-.55)))
[1] 11249
25000*(1-.55)
[1] 11250
length(sample(25000, 11250))
[1] 11250
length(sample(25000, 25000*.45))
[1] 11250 So the question is, why do I get 11249 out of the first command and not 11250? I can't figure this one out. Thanks Cory
See FAQ 7.31 Then try
.45 == (1-.55)
Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20101220/fcd5da42/attachment.pl>
Trying to make the fastest referal to FAQ 7.31 is becoming quite a sport around here. David beat us all to it on this round. Chances are there will be many more rounds to come. Best, Wolfgang ----Original Message---- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Jorge Ivan Velez Sent: Monday, December 20, 2010 20:42 To: cory n Cc: R mailing list Subject: Re: [R] sample() issue
Hi Cory, Check out http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f HTH, Jorge On Mon, Dec 20, 2010 at 2:04 PM, cory n <> wrote:
length(sample(25000, 25000*(1-.55)))
[1] 11249
25000*(1-.55)
[1] 11250
length(sample(25000, 11250))
[1] 11250
length(sample(25000, 25000*.45))
[1] 11250 So the question is, why do I get 11249 out of the first command and not 11250? I can't figure this one out. Thanks Cory
On Mon, Dec 20, 2010 at 01:04:18PM -0600, cory n wrote:
length(sample(25000, 25000*(1-.55)))
[1] 11249
25000*(1-.55)
[1] 11250
Besides FAQ 7.31, look also at http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy for further examples and hints concerning rounding errors in simple situations. Petr Savicky.