bc.i

Broadcasted Integer Numeric Operations with Extra Overflow Protection

Description

The bc.i() function performs broadcasted integer numeric operations on 2 numeric or logical arrays.

Please note that these operations will treat the input as (double typed) integers, and will efficiently truncate when necessary.
Therefore, something like bc.i(1, 1.5, “==”) returns TRUE, because trunc(1.5) equals 1.

Usage

bc.i(x, y, op)

Arguments

x, y conformable logical or numeric arrays.
op a single string, giving the operator.
Supported arithmetic operators: +, -, *, gcd, %%, %/%, ^, pmin, pmax.
Supported relational operators: ==, !=, <, >, <=, >=.
The "gcd" operator performs the Greatest Common Divisor" operation, using the Euclidean algorithm.

Value

For arithmetic operators:
A numeric array of whole numbers, as a result of the broadcasted arithmetic operation.
Base ‘R’ supports integers from -2^53 to 2^53, which thus range from approximately -9 quadrillion to +9 quadrillion.
Values outside of this range will be returned as -Inf or Inf, as an extra protection against integer overflow.

For relational operators:
A logical array as a result of the broadcasted integer relational comparison.

Examples

library("broadcast")

x.dim <- c(4:2)
x.len <- prod(x.dim)
x.data <- sample(c(NA, 1.1:1000.1), x.len, TRUE)
x <- array(x.data, x.dim)
y <- array(1:50, c(4,1,1))

bc.i(x, y, "+")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  701  618  114
## [2,]  848  259  783
## [3,]  492  126  185
## [4,]   56  157  376
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  654  156  840
## [2,]  142  949  381
## [3,]  335  575  576
## [4,]  249  911  155
bc.i(x, y, "-")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  699  616  112
## [2,]  844  255  779
## [3,]  486  120  179
## [4,]   48  149  368
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  652  154  838
## [2,]  138  945  377
## [3,]  329  569  570
## [4,]  241  903  147
bc.i(x, y, "*")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  700  617  113
## [2,] 1692  514 1562
## [3,] 1467  369  546
## [4,]  208  612 1488
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  653  155  839
## [2,]  280 1894  758
## [3,]  996 1716 1719
## [4,]  980 3628  604
bc.i(x, y, "gcd") # greatest common divisor
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    1    1
## [3,]    3    3    1
## [4,]    4    1    4
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    1    1
## [3,]    1    1    3
## [4,]    1    1    1
bc.i(x, y, "^")
## , , 1
## 
##           [,1]      [,2]        [,3]
## [1,]       700       617         113
## [2,]    715716     66049      609961
## [3,] 116930169   1860867     6028568
## [4,]   7311616 547981281 19150131456
## 
## , , 2
## 
##            [,1]         [,2]      [,3]
## [1,]        653          155       839
## [2,]      19600       896809    143641
## [3,]   36594368    187149248 188132517
## [4,] 3603000625 676751377201 519885601

bc.i(x, y, "==")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
## 
## , , 2
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
bc.i(x, y, "!=")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE
bc.i(x, y, "<")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
## 
## , , 2
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
bc.i(x, y, ">")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE
bc.i(x, y, "<=")
## , , 1
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
## 
## , , 2
## 
##       [,1]  [,2]  [,3]
## [1,] FALSE FALSE FALSE
## [2,] FALSE FALSE FALSE
## [3,] FALSE FALSE FALSE
## [4,] FALSE FALSE FALSE
bc.i(x, y, ">=")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,] TRUE TRUE TRUE
## [2,] TRUE TRUE TRUE
## [3,] TRUE TRUE TRUE
## [4,] TRUE TRUE TRUE