Learn R Programming

broadcast (version 0.1.7)

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 ANY bc.i(x, y, op)

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.


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.

See Also

broadcast_operators

Examples

Run this code
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, "+")
bc.i(x, y, "-")
bc.i(x, y, "*")
bc.i(x, y, "gcd") # greatest common divisor
bc.i(x, y, "^")

bc.i(x, y, "==")
bc.i(x, y, "!=")
bc.i(x, y, "<")
bc.i(x, y, ">")
bc.i(x, y, "<=")
bc.i(x, y, ">=")

Run the code above in your browser using DataLab