Divide two expressions, rounding the quotient toward zero
Description
Unlike <Expr>$floor_div(), which rounds down, this rounds
a negative quotient up, as integer division does in C and Rust. The
result has the same data type as
$floor_div()’s.
$truncdiv() is an alias for
$trunc_div(), which exists for
compatibility with Python Polars.
Usage
<Expr>$trunc_div(other)
expr__truncdiv(other)
Arguments
other
|
Numeric literal or expression value. |
Value
A polars expression
See Also
-
\$floor_div() -
\$true_div()
Examples
library("polars")
df <- pl$DataFrame(x = c(-5L, -4L, 4L, 5L))
df$with_columns(
`x%/%2` = pl$col("x")$floor_div(2L),
`trunc_div(x, 2)` = pl$col("x")$trunc_div(2L)
)
#> shape: (4, 3)
#> ┌─────┬───────┬─────────────────┐
#> │ x ┆ x%/%2 ┆ trunc_div(x, 2) │
#> │ --- ┆ --- ┆ --- │
#> │ i32 ┆ i32 ┆ i32 │
#> ╞═════╪═══════╪═════════════════╡
#> │ -5 ┆ -3 ┆ -2 │
#> │ -4 ┆ -2 ┆ -2 │
#> │ 4 ┆ 2 ┆ 2 │
#> │ 5 ┆ 2 ┆ 2 │
#> └─────┴───────┴─────────────────┘