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 simple arithmetic operators: +, -, *, ^, pmin, pmax.
Supported special division arithmetic operators: gcd, %%, %/%.
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,]  343  701  591
## [2,]  488  115  217
## [3,]  547  137  820
## [4,]  448  637  506
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  983  228   60
## [2,]  350  939  241
## [3,]  870  996  769
## [4,]  359  370  176
bc.i(x, y, "-")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  341  699  589
## [2,]  484  111  213
## [3,]  541  131  814
## [4,]  440  629  498
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  981  226   58
## [2,]  346  935  237
## [3,]  864  990  763
## [4,]  351  362  168
bc.i(x, y, "*")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  342  700  590
## [2,]  972  226  430
## [3,] 1632  402 2451
## [4,] 1776 2532 2008
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]  982  227   59
## [2,]  696 1874  478
## [3,] 2601 2979 2298
## [4,] 1420 1464  688
bc.i(x, y, "gcd") # greatest common divisor
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    1    1
## [3,]    1    1    1
## [4,]    4    1    2
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    1    1
## [3,]    3    3    1
## [4,]    1    2    4
bc.i(x, y, "^")
## , , 1
## 
##             [,1]         [,2]        [,3]
## [1,]         342          700         590
## [2,]      236196        12769       46225
## [3,]   160989184      2406104   545338513
## [4,] 38862602496 160551674721 63506016016
## 
## , , 2
## 
##             [,1]        [,2]      [,3]
## [1,]         982         227        59
## [2,]      121104      877969     57121
## [3,]   651714363   979146657 449455096
## [4,] 15882300625 17944209936 875213056

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