Lafaye de Micheaux Pierre
2011-Oct-20 00:44 UTC
[R] Strange R behavior for product of two sum of integers
Dear gentlemen, Can you explain me why the following happens (any OS I think, and even on 64 bits)? > sum(1000:1205)^2 [1] 51581223225 > sum(1000:1205)*sum(1000:1205) [1] NA Warning message: In sum(1000:1205) * sum(1000:1205) : NAs produced by integer overflow Best, Pierre -- Pierre Lafaye de Micheaux Adresse courrier: D?partement de Math?matiques et Statistique Universit? de Montr?al CP 6128, succ. Centre-ville Montr?al, Qu?bec H3C 3J7 CANADA Adresse physique: D?partement de Math?matiques et Statistique Bureau 4249, Pavillon Andr?-Aisenstadt 2920, chemin de la Tour Montr?al, Qu?bec H3T 1J4 CANADA T?l.: (00-1) 514-343-6607 / Fax: (00-1) 514-343-5700 lafaye at dms.umontreal.ca http://www.biostatisticien.eu
When using power operator ^, both the base and index are coerced to type "real". The range of "real" is larger than "integer". However, an ordinary multiplication operator * offers it to two integers (as always). So, to avoid the warning, just add "as.real(...)" to one or both multipliers (once one multiplier is "real", the other must be coerced to "real" in the operation):> sum(1000:1205)*as.real(sum(1000:1205))[1] 51581223225 HTH, Lei On Wed, Oct 19, 2011 at 7:44 PM, Lafaye de Micheaux Pierre < lafaye@dms.umontreal.ca> wrote:> Dear gentlemen, > > Can you explain me why the following happens (any OS I think, and even on > 64 bits)? > > > sum(1000:1205)^2 > [1] 51581223225 > > sum(1000:1205)*sum(1000:1205) > [1] NA > Warning message: > In sum(1000:1205) * sum(1000:1205) : NAs produced by integer overflow > > Best, > > Pierre > > -- > Pierre Lafaye de Micheaux > Adresse courrier: > Département de Mathématiques et Statistique > Université de Montréal > CP 6128, succ. Centre-ville > Montréal, Québec H3C 3J7 > CANADA > > Adresse physique: > Département de Mathématiques et Statistique > Bureau 4249, Pavillon André-Aisenstadt > 2920, chemin de la Tour > Montréal, Québec H3T 1J4 > CANADA > > Tél.: (00-1) 514-343-6607 / Fax: (00-1) 514-343-5700 > lafaye@dms.umontreal.ca > http://www.biostatisticien.eu > > ______________________________**________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help> > PLEASE do read the posting guide http://www.R-project.org/** > posting-guide.html <http://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Lei Jiang Center for Computation and Technology/ Department of Computer Science Louisiana State University E-mail: ljiang@cct.lsu.edu [[alternative HTML version deleted]]
David Winsemius
2011-Oct-20 14:50 UTC
[R] Strange R behavior for product of two sum of integers
On Oct 20, 2011, at 1:45 AM, Lei Jiang wrote:> When using power operator ^, both the base and index are coerced to > type > "real". The range of "real" is larger than "integer". > > However, an ordinary multiplication operator * offers it to two > integers (as > always). > > So, to avoid the warning, just add "as.real(...)" to one or both > multipliers > (once one multiplier is "real", the other must be coerced to "real" > in the > operation): > >> sum(1000:1205)*as.real(sum(1000:1205)) > [1] 51581223225That does seem to be an infelicity that ought to be fixed. Using the infix addition operator does that same sort of coercions, so why would one expect the infix multiplication operator to refuse to do it? > (sum(1000:1205))*(sum(1000:1204) +1205) [1] 51581223225 > (sum(1000:1205))*(sum(1000:1204) +0) [1] 51307549650 -- David.> > HTH, > Lei > > > On Wed, Oct 19, 2011 at 7:44 PM, Lafaye de Micheaux Pierre < > lafaye at dms.umontreal.ca> wrote: > >> Dear gentlemen, >> >> Can you explain me why the following happens (any OS I think, and >> even on >> 64 bits)? >> >>> sum(1000:1205)^2 >> [1] 51581223225 >>> sum(1000:1205)*sum(1000:1205) >> [1] NA >> Warning message: >> In sum(1000:1205) * sum(1000:1205) : NAs produced by integer overflow >> >> Best, >> >> Pierre >> >> -- >> Pierre Lafaye de Micheaux >> Adresse courrier: >> D?partement de Math?matiques et Statistique >> Universit? de Montr?al >> CP 6128, succ. Centre-ville >> Montr?al, Qu?bec H3C 3J7 >> CANADA >> >> Adresse physique: >> D?partement de Math?matiques et Statistique >> Bureau 4249, Pavillon Andr?-Aisenstadt >> 2920, chemin de la Tour >> Montr?al, Qu?bec H3T 1J4 >> CANADA >> >> T?l.: (00-1) 514-343-6607 / Fax: (00-1) 514-343-5700 >> lafaye at dms.umontreal.ca >> http://www.biostatisticien.eu >> >> ______________________________**________________ >> R-help at r-project.org mailing list >> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help >> > >> PLEASE do read the posting guide http://www.R-project.org/** >> posting-guide.html <http://www.R-project.org/posting-guide.html> >> and provide commented, minimal, self-contained, reproducible code. >> > > > > -- > Lei Jiang > > Center for Computation and Technology/ > Department of Computer Science > Louisiana State University > > E-mail: ljiang at cct.lsu.edu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Thomas Lumley
2011-Oct-20 15:36 UTC
[R] Strange R behavior for product of two sum of integers
On Thu, Oct 20, 2011 at 7:50 AM, David Winsemius <dwinsemius at comcast.net> wrote:> > On Oct 20, 2011, at 1:45 AM, Lei Jiang wrote: > >> When using power operator ^, both the base and index are coerced to type >> "real". The range of "real" is larger than "integer". >> >> However, an ordinary multiplication operator * offers it to two integers >> (as >> always). >> >> So, to avoid the warning, just add "as.real(...)" to one or both >> multipliers >> (once one multiplier is "real", the other must be coerced to "real" in the >> operation): >> >>> sum(1000:1205)*as.real(sum(1000:1205)) >> >> [1] 51581223225 > > That does seem to be an infelicity that ought to be fixed. Using the infix > addition operator does that same sort of coercions, so why would one expect > the infix multiplication operator to refuse to do it?This turns out not to be the case:> x<-2147483647L > y<-2147483647L > x+y[1] NA Warning message: In x + y : NAs produced by integer overflow -thomas -- Thomas Lumley Professor of Biostatistics University of Auckland