[R] wrong answer for simple expressions

Spencer Graves spencer.graves at pdf.com
Thu Nov 11 19:36:48 CET 2004


      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
>
>______________________________________________
>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
>  
>

-- 
Spencer Graves, PhD, Senior Development Engineer
O:  (408)938-4420;  mobile:  (408)655-4567




More information about the R-help mailing list