Skip to content

wrong answer for simple expressions

6 messages · Drew Hoysak, Peter Dalgaard, Thomas Lumley +3 more

#
I am experiencing strange (to me) output when trying to do simple
calculations.  Expressions that should equal zero yield non-zero
values.  
Examples:
[1] -4.440892e-16
[1] -4.440892e-16


When this last expression is expanded, I get the right answer:
[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
#
Drew Hoysak <dhoysak at ccs.carleton.ca> writes:
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.
#
On Thu, 11 Nov 2004, Drew Hoysak wrote:

            
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
Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle
#
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:

            

  
    
#
On Thu, 2004-11-11 at 12:24 -0500, Drew Hoysak wrote:
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:
[1] 0.10000000000000000555
[1] 4.0999999999999996447
[1] 0.99999999999999955591
[1] -4.0999999999999996447


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
#
Hi
On 11 Nov 2004 at 12:24, Drew Hoysak wrote:

            
Floating point arithmetic is imprecise.

Cheers
Petr
Petr Pikal
petr.pikal at precheza.cz