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.

For regular relational operators, see bc.rel.

Usage

bc.i(x, y, op, ...)

## S4 method for signature 'ANY'
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.
further arguments passed to or from methods.

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.

See Also

broadcast_operators

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,]  697  740  186
## [2,]  995  318  171
## [3,]  839  505  332
## [4,]  223  203  786
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  314   20  834
## [2,]  177  469  105
## [3,]  121  683  180
## [4,]  638  954  154
bc.i(x, y, "-")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  695  738  184
## [2,]  991  314  167
## [3,]  833  499  326
## [4,]  215  195  778
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  312   18  832
## [2,]  173  465  101
## [3,]  115  677  174
## [4,]  630  946  146
bc.i(x, y, "*")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  696  739  185
## [2,] 1986  632  338
## [3,] 2508 1506  987
## [4,]  876  796 3128
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  313   19  833
## [2,]  350  934  206
## [3,]  354 2040  531
## [4,] 2536 3800  600
bc.i(x, y, "gcd") # greatest common divisor
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    2    1
## [3,]    1    1    1
## [4,]    1    1    2
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    1    1
## [3,]    1    1    3
## [4,]    2    2    2
bc.i(x, y, "^")
## , , 1
## 
##            [,1]       [,2]         [,3]
## [1,]        696        739          185
## [2,]     986049      99856        28561
## [3,]  584277056  126506008     35611289
## [4,] 2300257521 1568239201 373961602576
## 
## , , 2
## 
##              [,1]         [,2]      [,3]
## [1,]          313           19       833
## [2,]        30625       218089     10609
## [3,]      1643032    314432000   5545233
## [4,] 161568625936 814506250000 506250000

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