Re: [R] wrong answer for simple expressions
Hi On 11 Nov 2004 at 12:24, Drew Hoysak wrote: > I am experiencing strange (to me) output when trying to do simple > calculations. Expressions that should equal zero yield non-zero > values. Examples: > > > a <- 4.1-3.1 > > b <- 5.1-4.1 > > a-b > [1] -4.440892e-16 > > > > (4.1-3.1)-(5.1-4.1) > [1] -4.440892e-16 > > > When this last expression is expanded, I get the right answer: > > > 4.1-3.1-5.1+4.1 > [1] 0 > > > I am using the binary packaged version R-2.0.0-0.fdr.1.fc2.i386.rpm > for Linux Fedora Core 2. I had the same problem with version 1.9.0-0 > > Can anyone tell me what is going on? Floating point arithmetic is imprecise. Cheers Petr > > Thanks, > > > Drew Hoysak > > __ > [EMAIL PROTECTED] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html Petr Pikal [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] wrong answer for simple expressions
On Thu, 2004-11-11 at 12:24 -0500, Drew Hoysak wrote: > I am experiencing strange (to me) output when trying to do simple > calculations. Expressions that should equal zero yield non-zero > values. > Examples: > > > a <- 4.1-3.1 > > b <- 5.1-4.1 > > a-b > [1] -4.440892e-16 > > > > (4.1-3.1)-(5.1-4.1) > [1] -4.440892e-16 > > > When this last expression is expanded, I get the right answer: > > > 4.1-3.1-5.1+4.1 > [1] 0 > > > I am using the binary packaged version R-2.0.0-0.fdr.1.fc2.i386.rpm for > Linux Fedora Core 2. I had the same problem with version 1.9.0-0 > Can anyone tell me what is going on? A lack of understanding as to how floating point numbers are represented by computers under the IEEE 754 floating point standard. Hint: Take note of the following: > print(0.1, digits = 20) [1] 0.1555 > print(4.1, digits = 20) [1] 4.0996447 > print(4.1 - 3.1, digits = 20) [1] 0.99955591 > print(4.1 - 3.1 - 5.1, digits = 20) [1] -4.0996447 Read the last FAQ "Why is 0.1 not 0.1?" here: http://grouper.ieee.org/groups/754/faq.html#binary-decimal and read David Goldberg's article, "What Every Computer Scientist Should Know about Floating-Point Arithmetic", which is available here: http://grouper.ieee.org/groups/754/ in a Postscript file or here in an edited form in HTML: http://docs.sun.com/source/806-3568/ncg_goldberg.html HTH, Marc Schwartz __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] wrong answer for simple expressions
R does double precision arithmetic and accumulates roundoff error like any other double precision computations. I would therefore expect it to accumulate roundoff error as you have reported. In most cases like you mentioned, a difference of 4e-16 is "not material", to use Accounting jargon. If it is an issue, you either need to do error analysis or use something like Mathematica that does infinite precision arithmetic. hope this helps. spencer graves Drew Hoysak wrote: I am experiencing strange (to me) output when trying to do simple calculations. Expressions that should equal zero yield non-zero values. Examples: a <- 4.1-3.1 b <- 5.1-4.1 a-b [1] -4.440892e-16 (4.1-3.1)-(5.1-4.1) [1] -4.440892e-16 When this last expression is expanded, I get the right answer: 4.1-3.1-5.1+4.1 [1] 0 I am using the binary packaged version R-2.0.0-0.fdr.1.fc2.i386.rpm for Linux Fedora Core 2. I had the same problem with version 1.9.0-0 Can anyone tell me what is going on? Thanks, Drew Hoysak __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Spencer Graves, PhD, Senior Development Engineer O: (408)938-4420; mobile: (408)655-4567 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] wrong answer for simple expressions
On Thu, 11 Nov 2004, Drew Hoysak wrote: I am experiencing strange (to me) output when trying to do simple calculations. Expressions that should equal zero yield non-zero values. No. There is no reason why these expressions should yield zero values. Remember that computers work in base 2, and that 0.1 has an infinitely recurring binary expansion in base 2. You should expect that 0.1 computed two different ways should differ in the last few bits. You have managed to get zero to 52 bits accuracy, which is not bad when you consider that the machine only works to 54 bits. -thomas Examples: a <- 4.1-3.1 b <- 5.1-4.1 a-b [1] -4.440892e-16 (4.1-3.1)-(5.1-4.1) [1] -4.440892e-16 When this last expression is expanded, I get the right answer: 4.1-3.1-5.1+4.1 [1] 0 I am using the binary packaged version R-2.0.0-0.fdr.1.fc2.i386.rpm for Linux Fedora Core 2. I had the same problem with version 1.9.0-0 Can anyone tell me what is going on? Thanks, Drew Hoysak __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Thomas Lumley Assoc. Professor, Biostatistics [EMAIL PROTECTED] University of Washington, Seattle __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] wrong answer for simple expressions
Drew Hoysak <[EMAIL PROTECTED]> writes: > I am experiencing strange (to me) output when trying to do simple > calculations. Expressions that should equal zero yield non-zero > values. > Examples: > > > a <- 4.1-3.1 > > b <- 5.1-4.1 > > a-b > [1] -4.440892e-16 > > > > (4.1-3.1)-(5.1-4.1) > [1] -4.440892e-16 > > > When this last expression is expanded, I get the right answer: > > > 4.1-3.1-5.1+4.1 > [1] 0 Welcome to the world of floating point arithmetic! Since one tenth cannot be represented exactly in binary, you are going to see these small deviations once in a while. It is is really no stranger than 3/3 - (1/3 + 1/3 + 1/3) = 1.000 - (0.333 + 0.333 + 0.333) = 0.001 in decimal notaion. -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] wrong answer for simple expressions
I am experiencing strange (to me) output when trying to do simple calculations. Expressions that should equal zero yield non-zero values. Examples: > a <- 4.1-3.1 > b <- 5.1-4.1 > a-b [1] -4.440892e-16 > (4.1-3.1)-(5.1-4.1) [1] -4.440892e-16 When this last expression is expanded, I get the right answer: > 4.1-3.1-5.1+4.1 [1] 0 I am using the binary packaged version R-2.0.0-0.fdr.1.fc2.i386.rpm for Linux Fedora Core 2. I had the same problem with version 1.9.0-0 Can anyone tell me what is going on? Thanks, Drew Hoysak __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html