bc.i

Broadcasted Integer Operations

Description

The bc.i() method performs broadcasted integer operations on 2 numeric or logical arrays.

Please note that these operations will treat the input as (53 or 32 bit) integers, and will efficiently truncate using trunc2int53() 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)

trunc2int53(x)

Arguments

x, y conformable vectors/arrays of type logical or numeric.
op a single string, giving the operator.
Supported integer 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 trunc2int53():
Base β€˜R’ supports 53 bit integers from -1 * (2^53 - 1) to 2^53 - 1, which thus range from approximately -9 quadrillion to +9 quadrillion.
trunc2int53() will truncate a numeric vector (like trunc()), and replace all values above 2^53 - 1 with Inf, and values below -1 * (2^53 - 1) with -Inf.

For integer operators:
A numeric (double or integer, depending on the situation) array of whole numbers, as a result of the broadcasted arithmetic operation.
Values outside of the 53 bit integer range will be returned as -Inf or Inf, as in trunc2int53(), as an extra protection against integer precision loss.

For relational operators:
A logical array as a result of the broadcasted integer relational comparison.
The relational operators efficiently truncate the input during broadcasting using trunc2int53().

References

On the integer division and remainder (modulo) computations, %% and %/%:
https://en.wikipedia.org/wiki/Modulo

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,]    0    0    0
#> [2,]    1    1    0
#> [3,]    1    1    1
#> [4,]    2    3    3
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]    0    0    0
#> [2,]    0    1    0
#> [3,]    2    2    2
#> [4,]    1    0    0
bc.i(x, y, "%/%")
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]  854  743  818
#> [2,]  181   14  410
#> [3,]  254   34  288
#> [4,]  206  158  115
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]  619  240  704
#> [2,]   88   36   69
#> [3,]  124  270  248
#> [4,]   62  115   41
bc.i(x, y, "gcd") # greatest common divisor
#> , , 1
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    1    1    2
#> [3,]    1    1    1
#> [4,]    2    1    1
#> 
#> , , 2
#> 
#>      [,1] [,2] [,3]
#> [1,]    1    1    1
#> [2,]    2    1    2
#> [3,]    1    1    1
#> [4,]    1    4    4

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