Skip to contents

Element-wise sign function: -1, 0 or 1 at the input's data type. An unsigned input holds no negative value, so its sign is 0 or 1, like base R's sign() on a non-negative number. You can also use sign().

Usage

nv_sign(x)

Arguments

x

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

Value

(arrayish)
Has the input's shape and data type.

See also

prim_sign() for the underlying primitive, which takes a signed input only.

Examples

# the input's data type carries through
x <- nv_array(c(-3, 0, 5))
sign(x)
#> AnvlArray
#>  -1
#>   0
#>   1
#> [ CPUf32{3} ] 

# an unsigned input is 0 where it is 0 and 1 everywhere else
nv_sign(nv_array(c(0L, 3L), dtype = "ui32"))
#> AnvlArray
#>  0
#>  1
#> [ CPUui32{2} ] 

# an R value materializes at its default data type
nv_sign(-3)
#> AnvlArray
#>  -1
#> [ CPUf32{} ]