maechler@stat.math.ethz.ch
2000-May-22 10:22 UTC
integer functions {was [Rd] Inconsistencies (PR#550)}
>>>>> "BDR" == Prof Brian D Ripley <ripley@stats.ox.ac.uk> writes:BDR> On Mon, 22 May 2000 berwin@maths.uwa.edu.au wrote: >> Dear all, >> >> I was playing around with some C-code that I dynamically linked to R >> and noticed the following inconsistency: > > dat <- matrix(1,3,3) > > is.double(dat) > [1] TRUE > > is.integer(dat) > [1] FALSE > > dat <- matrix(1:9,3,3) > > is.double(dat) > [1] FALSE > > is.integer(dat) > [1] TRUE >> How did I find out? I passed `dat' to a double * in the C-code and >> printed out the matrix from C. But in the .C() call I just passed >> down `dat' and forgot to coerce to double via `as.double(dat)'. Of >> course the print out from the C routine was garbage, but since I used >> dat <- matrix(1,3,3) >> first, I was confused. Note that R says >> that `dat' is.double! But it seems to pass dat to C as an integer >> matrix nevertheless! Using the second matrix it became clear to me >> why I saw garbage being printed out from the C-routine. My question >> is now why does the first `is.double(dat)' returns `TRUE'? BDR> Because it is double! This refers to the storage mode, not the BDR> mathematical properties of the object's content. exactly! Please (everyone who needs, probably Berwin doesn't) *do* study ?storage.mode and ?mode with some care BDR> I can find no inconsistency. 1 is double, and 1:9 is integer BDR> (just as in S-PLUS 3.x). Why should this not be the case? BDR> Currently the only time in R you are likely to get an integer BDR> result is a : or seq expression, but you are not supposed to rely BDR> on this. Well, "Values:" in ?seq says >> Value: >> The result is of `mode' `"integer"' if `from' is (numerically >> equal to an) integer. on which I do rely.. Also, there are other functions guaranteeing an integer (or NA / NULL) result, at least o length() o dim() o ncol(), nrow() --- o min() and max() --- when all args are integers o "+" and "-" if the args are integers -- however that might change: Currently, there's no overflow detection! BDR> (Since I know you have a copy, this is exactly the BDR> problem of the wam example on page 203 of `S Prograaming'.) BDR> For example, if printit.c is <... instructive example...> Martin Maechler <maechler@stat.math.ethz.ch> http://stat.ethz.ch/~maechler/ Seminar fuer Statistik, ETH-Zentrum LEO D10 Leonhardstr. 27 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-1-632-3408 fax: ...-1228 <>< -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-devel 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-devel-request@stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._