Skip to content

Divide two expressions

Source code

Description

Method equivalent of float division operator expr / other.

Usage

<Expr>$div(other)

Arguments

other Numeric literal or expression value.

Details

Zero-division behaviour follows IEEE-754:

  • 0/0: Invalid operation - mathematically undefined, returns NaN.
  • n/0: On finite operands gives an exact infinite result, e.g.: ±infinity.

Value

Expr

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")$div(2),
  `x/y` = pl$col("x")$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  │
#> └─────┴──────┴──────┴───────┘