Element-wise clamp: min(max(min, x), max).
Arguments
- x
(
arrayish)
One input. Can be any data type. An R value materializes at its default data type.- min, max
(
arrayish)
Lower and upper bound, each scalar or the same shape asx. They are brought tox's data type: an R value is built at it when its category can reach it (0Lserves an integer and a floatxalike,0only a float one), and a value that already has a data type is converted unless that would narrow it – anf64bound for anf32xis an error rather than a silent narrowing.
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(x, nv_scalar(0), 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(x, 0L, 1L)
#> AnvlArray
#> 0.0000
#> 0.5000
#> 1.0000
#> [ CPUf32{3} ]