Hi List Try the following simple set of functions (R-1.6.1): square <- function(x){x*x} cube <- function(x) {x*x*x} f1 <- function(x){square(x)*(x+1)} f2 <- function(x){square(x)+cube(x)} error <- function(x){f1(x)-f2(x)} [see how f1() and f2() are algebraically identical]. Then: R> error(cube(20)) [1] 0>no problem. Then: R> error(cube(1:20)) [1] 0 0 0 0 0 0 0 0 0 0 NA NA NA NA NA NA NA NA NA NA Warning message: NAs produced by integer overflow in: x * x * x See how error() fails for the vector but not for the single number argument. Is R being reasonable here? -- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
On Mon, 31 Mar 2003, Robin Hankin wrote:> Try the following simple set of functions (R-1.6.1): > > square <- function(x){x*x} > cube <- function(x) {x*x*x} > f1 <- function(x){square(x)*(x+1)} > f2 <- function(x){square(x)+cube(x)} > error <- function(x){f1(x)-f2(x)} > > [see how f1() and f2() are algebraically identical]. Then: > > R> error(cube(20)) > [1] 0 > > > > no problem. Then: > > R> error(cube(1:20)) > [1] 0 0 0 0 0 0 0 0 0 0 NA NA NA NA NA NA NA NA NA NA > Warning message: > NAs produced by integer overflow in: x * x * x > > > See how error() fails for the vector but not for the single number > argument. Is R being reasonable here?Yes, but is the user being reasonable here? 20 is double 1:20 is integer. and if you give an integer argument you get integer arithmetic. Note that R did not `fail'': it returned a sensible answer and gave a warning. Programmers do need to know about representation issues, and this is a programming task. Don''t expect computers to be able to hold arbitrarily large numbers exactly. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
>>>>> "Robin" == Robin Hankin <r.hankin at auckland.ac.nz> >>>>> on Mon, 31 Mar 2003 16:00:23 +1200 writes:Robin> Hi List Try the following simple set of functions Robin> (R-1.6.1): Robin> square <- function(x){x*x} cube <- function(x) Robin> {x*x*x} f1 <- function(x){square(x)*(x+1)} f2 <- Robin> function(x){square(x)+cube(x)} error <- Robin> function(x){f1(x)-f2(x)} Robin> [see how f1() and f2() are algebraically identical]. Robin> Then: R> error(cube(20)) Robin> [1] 0 >> Robin> no problem. Then: R> error(cube(1:20)) Robin> [1] 0 0 0 0 0 0 0 0 0 0 NA NA NA NA NA NA NA NA NA Robin> NA Warning message: NAs produced by integer overflow Robin> in: x * x * x Robin> See how error() fails for the vector but not for the Robin> single number argument. Is R being reasonable here? yes, `as always'' :-) The clue is: 20 is not integer, but 20:20 or as.integer(20) is Martin Robin> -- Robin> Robin Hankin, Lecturer, School of Geography and Robin> Environmental Science Tamaki Campus Private Bag 92019 Robin> Auckland New Zealand Robin> r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; Robin> FAX 0064-9-373-7042 Robin> ______________________________________________ Robin> R-help at stat.math.ethz.ch mailing list Robin> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
As usual, Prof. Ripley helped enlighten the rest of us. Consider the following: > error(cube(as.integer(20))) [1] NA Warning message: NAs produced by integer overflow in: x * x * x > error(cube(as.numeric(1:20))) [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Best Regards, Spencer Graves ripley at stats.ox.ac.uk wrote:> On Mon, 31 Mar 2003, Robin Hankin wrote: > > >>Try the following simple set of functions (R-1.6.1): >> >> square <- function(x){x*x} >> cube <- function(x) {x*x*x} >> f1 <- function(x){square(x)*(x+1)} >> f2 <- function(x){square(x)+cube(x)} >> error <- function(x){f1(x)-f2(x)} >> >>[see how f1() and f2() are algebraically identical]. Then: >> >>R> error(cube(20)) >>[1] 0 >> >>no problem. Then: >> >>R> error(cube(1:20)) >> [1] 0 0 0 0 0 0 0 0 0 0 NA NA NA NA NA NA NA NA NA NA >>Warning message: >>NAs produced by integer overflow in: x * x * x >> >> >>See how error() fails for the vector but not for the single number >>argument. Is R being reasonable here? > > > Yes, but is the user being reasonable here? > > 20 is double > 1:20 is integer. > > and if you give an integer argument you get integer arithmetic. > Note that R did not `fail'': it returned a sensible answer and gave a > warning. > > Programmers do need to know about representation issues, and this is a > programming task. Don''t expect computers to be able to hold arbitrarily > large numbers exactly. >
Hi everyone As many people pointed out, R is behaving as documented. The issue was that a:b has storage mode integer if a is (numerically) equal to an integer. Thus: R> storage.mode(as.double(1.0):3) [1] "integer" But R> storage.mode(as.double(1.1):3) [1] "double" R> storage.mode(1) [1] "double" R> as documented under help(":"). Now _why_ does colon have this behaviour, when it seems that very few other functions return integers; help(":") gives no clue to the motivation. Splus does the same thing (apparently), so there must be some rationale. What is it? Also, it would appear, both "if(2+2==4)" and "(1:10)==4" are inadvisable, because in both cases, at least one side of the "==" is a double. How do I get round this? cheers rksh -- Robin Hankin, Lecturer, School of Geography and Environmental Science Tamaki Campus Private Bag 92019 Auckland New Zealand r.hankin at auckland.ac.nz tel 0064-9-373-7599 x6820; FAX 0064-9-373-7042
On Tue, 1 Apr 2003, Robin Hankin wrote: [...]> Now _why_ does colon have this behaviour, when it seems that very few > other functions return integers; help(":") gives no clue to the > motivation. Splus does the same thing (apparently), so there must be > some rationale. What is it?If you mean integers, it helps to have them to avoid rounding errors. S-PLUS (sic) currently thinks 20 is integer and 20. is double.> Also, it would appear, both "if(2+2==4)" and "(1:10)==4" are > inadvisable, because in both cases, at least one side of the "==" is a > double. How do I get round this??identical ?as.equal BTW, == does coerce as needed, so 2+2==as.integer(4) is fine. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595