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 vectors/arrays of type logical or numeric.
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,]  894  982  848
## [2,]  619  902  412
## [3,]  378  543  733
## [4,]  364  439  641
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  595  249   60
## [2,]  608  849  929
## [3,]  277  759  841
## [4,]   79  886  441
bc.i(x, y, "-")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  892  980  846
## [2,]  615  898  408
## [3,]  372  537  727
## [4,]  356  431  633
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  593  247   58
## [2,]  604  845  925
## [3,]  271  753  835
## [4,]   71  878  433
bc.i(x, y, "*")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  893  981  847
## [2,] 1234 1800  820
## [3,] 1125 1620 2190
## [4,] 1440 1740 2548
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  594  248   59
## [2,] 1212 1694 1854
## [3,]  822 2268 2514
## [4,]  300 3528 1748
bc.i(x, y, "gcd") # greatest common divisor
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    2    2
## [3,]    3    3    1
## [4,]    4    1    1
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    1    1
## [3,]    1    3    1
## [4,]    1    2    1
bc.i(x, y, "^")
## , , 1
## 
##             [,1]        [,2]         [,3]
## [1,]         893         981          847
## [2,]      380689      810000       168100
## [3,]    52734375   157464000    389017000
## [4,] 16796160000 35806100625 164648481361
## 
## , , 2
## 
##          [,1]         [,2]        [,3]
## [1,]      594          248          59
## [2,]   367236       717409      859329
## [3,] 20570824    432081216   588480472
## [4,] 31640625 605165749776 36469158961

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