bc.i

Broadcasted Integer Numeric Operations with Extra Overflow Protection

Description

The bc.i() method 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,]  725   NA  203
#> [2,]  858  675  264
#> [3,]  956  324   30
#> [4,]  132  945  879
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  926  760  494
#> [2,]  507  540  431
#> [3,]  144  988  996
#> [4,]  629  885  605
bc.i(x, y, "-")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]  723   NA  201
#> [2,]  854  671  260
#> [3,]  950  318   24
#> [4,]  124  937  871
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  924  758  492
#> [2,]  503  536  427
#> [3,]  138  982  990
#> [4,]  621  877  597
bc.i(x, y, "*")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]  724   NA  202
#> [2,] 1712 1346  524
#> [3,] 2859  963   81
#> [4,]  512 3764 3500
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  925  759  493
#> [2,] 1010 1076  858
#> [3,]  423 2955 2979
#> [4,] 2500 3524 2404
bc.i(x, y, "gcd") # greatest common divisor
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]    1   NA    1
#> [2,]    2    1    2
#> [3,]    1    3    3
#> [4,]    4    1    1
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    1    2    1
#> [3,]    3    1    3
#> [4,]    1    1    1
bc.i(x, y, "^")
#> , , 1
#> 
#>           [,1]         [,2]         [,3]
#> [1,]       724           NA          202
#> [2,]    732736       452929        68644
#> [3,] 865523177     33076161        19683
#> [4,] 268435456 784076601361 586181640625
#> 
#> , , 2
#> 
#>              [,1]         [,2]         [,3]
#> [1,]          925          759          493
#> [2,]       255025       289444       184041
#> [3,]      2803221    955671625    979146657
#> [4,] 152587890625 602425897921 130466162401

bc.i(x, y, "==")
#> , , 1
#> 
#>       [,1]  [,2]  [,3]
#> [1,] FALSE    NA 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   NA 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    NA 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   NA 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    NA 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   NA 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