Dear People, Consider the following fragment of R code choose <- function(n,r) { if( is.integer(n) && is.integer(r) && n > 0 && r >= 0 ) { .C("choose",as.double(n),as.double(r),comb = double(1))$comb } else stop("n must be a positive integer and r a non-negative integer.") } This is a practice function (n choose r), which I wrote using a call to C code. Unfortunately, there is the following problem. I thought I would put in error checking for whether the arguments were integers. However, is.integer(1), for example, returns FALSE. However is.numeric(1) returns TRUE. It seems that explicit coercion is required for this ie. is.integer(as.integer(1)) returns true However, I can't use as.integer, because it is too aggressive for just error checking: it rounds off the value to the greatest integer less than. Can anyone suggest a suitable method of checking for an integer in this case? Sincerely, Faheem Mitha. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Faheem Mitha <faheem at email.unc.edu> writes:> Consider the following fragment of R code > > choose <- function(n,r) > { > if( is.integer(n) && is.integer(r) && n > 0 && r >= 0 ) > { > .C("choose",as.double(n),as.double(r),comb = double(1))$comb > } > else stop("n must be a positive integer and r a non-negative integer.") > } > > This is a practice function (n choose r), which I wrote using a call to C > code. Unfortunately, there is the following problem. I thought I would put > in error checking for whether the arguments were integers. However, > is.integer(1), for example, returns FALSE. However is.numeric(1) returns > TRUE. It seems that explicit coercion is required for this ie. > is.integer(as.integer(1)) returns true However, I can't use as.integer, > because it is too aggressive for just error checking: it rounds off the > value to the greatest integer less than. Can anyone suggest a suitable > method of checking for an integer in this case?Generally the S languages do not encourage distinguishing between floating point and integer values at the level of S code. The way the values are stored should not be important to the user unless the user is going to call C or Fortran code in which case you really do want to use as.integer. If you prefer not to get truncation then I would suggest using as.integer(round(n)). A more common way of writing the function would be n <- as.numeric(n)[1] r <- as.numeric(r)[1] if( n <= 0 || r < 0 ) stop("n must be positive and r non-negative") .C("choose", as.integer(round(n)), as.integer(round(r)), comb = double(1))$comb -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Faheem, The following function isn't infallible, but will work to the machine precision: > is.int <- function (x) .Machine$double.eps >= abs(x - floor(x)) > > is.int(-1234.0000000000001) [1] TRUE > is.int(-1234.000000000001) [1] FALSE > (Actually is.int <- function (x) 0 == x - floor(x) accomplishes the same thing.) Does that help? John At 03:59 PM 24/02/2001 -0500, you wrote:>Dear People, > >Consider the following fragment of R code > >choose <- function(n,r) >{ > if( is.integer(n) && is.integer(r) && n > 0 && r >= 0 ) > { > .C("choose",as.double(n),as.double(r),comb = double(1))$comb > } > else stop("n must be a positive integer and r a non-negative integer.") >} > >This is a practice function (n choose r), which I wrote using a call to C >code. Unfortunately, there is the following problem. I thought I would put >in error checking for whether the arguments were integers. However, >is.integer(1), for example, returns FALSE. However is.numeric(1) returns >TRUE. It seems that explicit coercion is required for this ie. >is.integer(as.integer(1)) returns true However, I can't use as.integer, >because it is too aggressive for just error checking: it rounds off the >value to the greatest integer less than. Can anyone suggest a suitable >method of checking for an integer in this case? > > Sincerely, Faheem Mitha.________________________________ John Fox Department of Sociology McMaster University email: jfox at McMaster.ca web: www.socsci.mcmaster.ca/jfox ________________________________ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Faheem Mitha writes:> -----Original Message----- > From: [mailto:faheem at email.unc.edu] > Sent: Sunday, 25 February 2001 7:00 > To: r-help at stat.math.ethz.ch > Subject: [R] testing for integer > > Dear People, > > Consider the following fragment of R code > > choose <- function(n,r) > { > if( is.integer(n) && is.integer(r) && n > 0 && r >= 0 ) > { > .C("choose",as.double(n),as.double(r),comb = double(1))$comb > } > else stop("n must be a positive integer and r a non-negative integer.") > } > > This is a practice function (n choose r), which I wrote using a call to C > code. Unfortunately, there is the following problem. I thought I would put > in error checking for whether the arguments were integers. However, > is.integer(1), for example, returns FALSE. However is.numeric(1) returns > TRUE. It seems that explicit coercion is required for this ie. > is.integer(as.integer(1)) returns true However, I can't use as.integer, > because it is too aggressive for just error checking: it rounds off the > value to the greatest integer less than. Can anyone suggest a suitable > method of checking for an integer in this case? > > Sincerely, Faheem Mitha.There is a slight confusion here between the mathematical notion of "integer" (meaning a whole number) and the computing notion of "integer" (meaning a quantity stored in a particular way). is.integer is a test for the latter, you want a test for the former. Here is a pretty standard one-liner to do that. It handles vector arguments and tests if they are all whole number values: whole.number <- function(k) all((k %% 1) == 0) You might use if(whole.number(c(n,r)) && .... Bill Venables. -- Bill Venables, CSIRO/CMIS Environmetrics Project Email: Bill.Venables at cmis.csiro.au Phone: +61 7 3826 7251 Fax: +61 7 3826 7304 Postal: PO Box 120, Cleveland, Qld 4163, AUSTRALIA -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Sat, 24 Feb 2001, Faheem Mitha wrote:> Dear People, > > Consider the following fragment of R code[snipped] Thanks to everyone who replied. I now have two different tests! Sorry, I didn't realise I needed to roll my own test for this... Sincerely, Faheem Mitha. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Everything is double. You might try n==floor(n). Faheem Mitha wrote:> > Dear People, > > Consider the following fragment of R code > > choose <- function(n,r) > { > if( is.integer(n) && is.integer(r) && n > 0 && r >= 0 ) > { > .C("choose",as.double(n),as.double(r),comb = double(1))$comb > } > else stop("n must be a positive integer and r a non-negative integer.") > } > > This is a practice function (n choose r), which I wrote using a call to C > code. Unfortunately, there is the following problem. I thought I would put > in error checking for whether the arguments were integers. However, > is.integer(1), for example, returns FALSE. However is.numeric(1) returns > TRUE. It seems that explicit coercion is required for this ie. > is.integer(as.integer(1)) returns true However, I can't use as.integer, > because it is too aggressive for just error checking: it rounds off the > value to the greatest integer less than. Can anyone suggest a suitable > method of checking for an integer in this case? > > Sincerely, Faheem Mitha. > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Bob Wheeler --- (Reply to: bwheeler at echip.com) ECHIP, Inc. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._