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,]  333   12  628
## [2,]  303  880  650
## [3,]  120   97   71
## [4,]  603  119  185
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]   73  536   44
## [2,]  646  151  912
## [3,]  585   69  983
## [4,]  358  997   46
bc.i(x, y, "-")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  331   10  626
## [2,]  299  876  646
## [3,]  114   91   65
## [4,]  595  111  177
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]   71  534   42
## [2,]  642  147  908
## [3,]  579   63  977
## [4,]  350  989   38
bc.i(x, y, "*")
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]  332   11  627
## [2,]  602 1756 1296
## [3,]  351  282  204
## [4,] 2396  460  724
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]   72  535   43
## [2,] 1288  298 1820
## [3,] 1746  198 2940
## [4,] 1416 3972  168
bc.i(x, y, "gcd") # greatest common divisor
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    1    2    2
## [3,]    3    1    1
## [4,]    1    1    1
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]    1    1    1
## [2,]    2    1    2
## [3,]    3    3    1
## [4,]    2    1    2
bc.i(x, y, "^")
## , , 1
## 
##              [,1]      [,2]       [,3]
## [1,]          332        11        627
## [2,]        90601    770884     419904
## [3,]      1601613    830584     314432
## [4,] 128738157601 174900625 1073283121
## 
## , , 2
## 
##             [,1]         [,2]      [,3]
## [1,]          72          535        43
## [2,]      414736        22201    828100
## [3,]   197137368       287496 941192000
## [4,] 15704099856 972292630401   3111696

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