[R] A simple question??

Bill Venables venables at acland.qld.cmis.csiro.au
Tue Mar 7 07:35:25 CET 2000


Vincent Hoi Nam asks:
> 
> Dear all,
> 
>   I'm currently use R v0.99 on Windows 98 Second Edition. I have a =
> question on some simple calculations. I wonder that I've done something =
> wrong with the calculation.
> Here is the imput commands:
> 
> > a <- 25.01
> > b <- 56.08
> > a.trunc <- trunc(a)
> > b.trunc <- trunc(b)
> > a.tail <- a - a.trunc
> > b.tail <- b - b.trunc
> > a.trunc
> [1] 25
> > b.trunc
> [1] 56
> > a.tail
> [1] 0.01
> > b.tail
> [1] 0.08
> > a.tail <- (a - a.trunc) * 100
> > b.tail <- (b - b.trunc) * 100
> > a.tail
> [1] 1
> > b.tail
> [1] 8
> > N <- ( (b.trunc - a.trunc) * 12 ) + (b.tail - a.tail)
> > N
> [1] 379
> > N == 379
> [1] FALSE
> 
> According to the above command and output, the value of N is equal to 
> 379, but when I set the condition 'N =3D=3D 379' R gives "FALSE" as the 
> result. How do I avoid this problem?? I do the same thing in S-plus 4.0 
> release 3 on Win98 SE and obtained the same result.
> 
> I look forward to hear your reply.

You have mistaken the true value with the one printed.  To see
why this happens try the following extra step

> abs(N - 379)
[1] 3.4106e-13

So while the two are very nearly equal, roundoff error foils (ie
spoils) exact equality.

-- 
Bill Venables,      Statistician,     CMIS Environmetrics Project
CSIRO Marine Labs, PO Box 120, Cleveland, Qld,  AUSTRALIA.   4163
Tel: +61 7 3826 7251           Email: Bill.Venables at cmis.csiro.au    
Fax: +61 7 3826 7304      http://www.cmis.csiro.au/bill.venables/



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list