[R] Strange R behavior for product of two sum of integers

Thomas Lumley tlumley at uw.edu
Thu Oct 20 17:36:36 CEST 2011


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



More information about the R-help mailing list