R: simple multiplication causes integer overflow -


in longer script have multiply length of vector (2614) numbers of rows of dataframe b (1456000). if directly length(a) * nrow(b) message nas produced integer overflow although there's no problem when multiply same numbers:

2614 * 1456000  [1] 3805984000  

the way multiplication work round(length(a)) * nrow(b) or length(a) * round(nrow(b)). numbers produced length , nrow must integers anyhow! moreover, tested following function suggested on page function is.integer...

is.wholenumber <- function(x, tol = .machine$double.eps^0.5) abs(x-round(x)) < tol 

... , of course, integers. why need crutches "round" here? puzzling... has got idea what's going on in background?

hopefully graphic representation of happening....

2614 * 1456000 #[1] 3805984000  ##  integers represented doubles class( 2614 * 1456000 ) #[1] "numeric"  #  force numbers integers 2614l * 1456000l #[1] na #warning message: #in 2614l * 1456000l : nas produced integer overflow  ##  , result integer overflow warning class( 2614l * 1456000l ) #[1] "integer" #warning message: #in 2614l * 1456000l : nas produced integer overflow 

2614 * 1456000 numeric because both operands of class numeric. overflow occurs because both nrow , length return integer's , hence result integer result exceeds maximum size representable integer class (+/-2*10^9). numeric or double can hold 2e-308 2e+308. solve problem, use as.numeric(length(a)) or as.double(length(a)).


Comments