Divide two expressions
Description
Method equivalent of float division operator expr / other
.
$truediv()
is an alias for
$true_div()
, which exists for
compatibility with Python Polars.
Usage
<Expr>$true_div(other)
expr__truediv(other)
Arguments
other
|
Numeric literal or expression value. |
Details
Zero-division behaviour follows IEEE-754:
-
0/0
: Invalid operation - mathematically undefined, returnsNaN
. -
n/0
: On finite operands gives an exact infinite result, e.g.: ±infinity.
Value
A polars expression
See Also
- Arithmetic operators
-
\
$floor_div()
Examples
library("polars")
df <- pl$DataFrame(
x = -2:2,
y = c(0.5, 0, 0, -4, -0.5)
)
df$with_columns(
`x/2` = pl$col("x")$true_div(2),
`x/y` = pl$col("x")$true_div(pl$col("y"))
)
#> shape: (5, 4)
#> ┌─────┬──────┬──────┬───────┐
#> │ x ┆ y ┆ x/2 ┆ x/y │
#> │ --- ┆ --- ┆ --- ┆ --- │
#> │ i32 ┆ f64 ┆ f64 ┆ f64 │
#> ╞═════╪══════╪══════╪═══════╡
#> │ -2 ┆ 0.5 ┆ -1.0 ┆ -4.0 │
#> │ -1 ┆ 0.0 ┆ -0.5 ┆ -inf │
#> │ 0 ┆ 0.0 ┆ 0.0 ┆ NaN │
#> │ 1 ┆ -4.0 ┆ 0.5 ┆ -0.25 │
#> │ 2 ┆ -0.5 ┆ 1.0 ┆ -4.0 │
#> └─────┴──────┴──────┴───────┘