Skip to contents

Element-wise clamp: min(max(min_val, x), max_val).

Usage

nv_clamp(min_val, x, max_val)

Arguments

min_val, max_val

(arrayish)
Lower and upper bound, each scalar or the same shape as x. They are brought to x's data type: an R value is built at it when its category can reach it (0L serves an integer and a float x alike, 0 only a float one), and a value that already has a data type is converted unless that would narrow it – an f64 bound for an f32 x is an error rather than a silent narrowing.

x

(arrayish)
One input. Can be any data type. An R value materializes at its default data type.

Value

(arrayish)
Has x's shape and data type.

See also

prim_clamp() for the underlying primitive.

Examples

# the bounds are brought to `x`'s data type
x <- nv_array(c(-1, 0.5, 2))
nv_clamp(nv_scalar(0), x, nv_scalar(1))
#> AnvlArray
#>  0.0000
#>  0.5000
#>  1.0000
#> [ CPUf32{3} ] 

# an R integer serves a float `x` too, since a float can hold it
nv_clamp(0L, x, 1L)
#> AnvlArray
#>  0.0000
#>  0.5000
#>  1.0000
#> [ CPUf32{3} ]