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,]  207  147   47
#> [2,]  496  812  348
#> [3,]  260  902  282
#> [4,]  394  443  332
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  674  171  201
#> [2,]  273  244  875
#> [3,]   95   43  564
#> [4,]  890  106  938
bc.i(x, y, "-")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]  205  145   45
#> [2,]  492  808  344
#> [3,]  254  896  276
#> [4,]  386  435  324
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  672  169  199
#> [2,]  269  240  871
#> [3,]   89   37  558
#> [4,]  882   98  930
bc.i(x, y, "*")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]  206  146   46
#> [2,]  988 1620  692
#> [3,]  771 2697  837
#> [4,] 1560 1756 1312
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  673  170  200
#> [2,]  542  484 1746
#> [3,]  276  120 1683
#> [4,] 3544  408 3736
bc.i(x, y, "gcd") # greatest common divisor
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    2    2    2
#> [3,]    1    1    3
#> [4,]    2    1    4
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    1    2    1
#> [3,]    1    1    3
#> [4,]    2    2    2
bc.i(x, y, "^")
#> , , 1
#> 
#>             [,1]        [,2]        [,3]
#> [1,]         206         146          46
#> [2,]      244036      656100      119716
#> [3,]    16974593   726572699    21717639
#> [4,] 23134410000 37141383841 11574317056
#> 
#> , , 2
#> 
#>              [,1]      [,2]         [,3]
#> [1,]          673       170          200
#> [2,]        73441     58564       762129
#> [3,]       778688     64000    176558481
#> [4,] 616218720016 108243216 761004990736

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