From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>

Date: Tue 08 Mar 2005 - 20:45:26 EST

Date: Tue 08 Mar 2005 - 20:45:26 EST

Robin Hankin <r.hankin@soc.soton.ac.uk> writes:

*> hi
**>
**>
*

> ?integer says:

*>
**> Note that on almost all implementations of R the range of
**> representable integers is restricted to about +/-2*10^9: 'double's
**> can hold much larger integers exactly.
**>
**>
**> I am getting very confused as to when to use integers and when not to.
**> In my line
**> I need exact comparisons of large integer-valued arrays, so I often
**> use as.integer(),
**> but the above seems to tell me that doubles might be better.
**>
**> Consider the following R idiom of Euclid's algorithm for the highest
**> common factor
**> of two positive integers:
**>
**> gcd <- function(a, b){
**> if (b == 0){ return(a)}
**> return(Recall(b, a%%b))
**> }
**>
**> If I call this with gcd(10,12), for example, then a%%b is not an
**> integer, so the first
**> line of the function, testing b for being zero, isn't legitimate.
**>
**> OK, so I have some options:
**>
**> (1) stick in "a <- as.integer(a), b <- as.integer(b)" into the
**> function: then a%%b *will* be an
**> integer and the "==" test is appropriate
**> (2) use some test like abs(b) < TOL for some suitable TOL (0.5?)
**> (3) use identical(all.equal(b,0),TRUE) like it says in identical.Rd
**> (4) use identical(all.equal(b,as.integer(0)),TRUE)
**>
**> How does the List deal with this kind of problem?
**>
**> Also, gcd() as written returns a non-integer. Would the List
**> recommend rewriting the last
**> line as
**>
**> return(as.integer(Recall(b,a%%b)))
**>
**> or not?
*

Not if you want things to work in the large-integer domain...

You're in somewhat murky waters here because it all has to do with whether you can rely on the floating point aritmetic being exact for integers up to 2^53. *If* that works, then there's really no reason to distrust "==" in this context and the gcd() works as originally written. You might consider wrapping it in a function that checks whether a and b are both (1) in range and (2) that they are integers in the sense that round(x)==x. (Failing 2, you likely get an infinite recursion).

-- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk) FAX: (+45) 35327907 ______________________________________________ R-help@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.htmlReceived on Tue Mar 08 21:07:13 2005

*
This archive was generated by hypermail 2.1.8
: Fri 03 Mar 2006 - 03:30:41 EST
*