Trait in prelude::ops auto-imported

Div

trait Div<T>
Source: compiler/src/builtin_docs/prelude/ops.hy

The division operator /.

a / b on a type with a Div instance calls div(a, b). int and float have builtin instances: int division truncates toward zero and panics with division by zero when b is 0; float division follows IEEE-754 (1.0 / 0.0 is infinity).

Required methods#

NameDescription
divReturns a / b.

Examples#

use tests::assert;
class V2 {
    pub x: int,
    pub y: int,
}

impl Div for V2 {
    pub fn div(V2 a, V2 b) -> V2 {
        return new V2(a.x / b.x, a.y / b.y);
    }
}

fn main() {
    let q = new V2(9, 8) / new V2(3, 2);
    assert!(q.x == 3);
    assert!(q.y == 4);
    assert!(-7 / 2 == -3);
}

See also#

  • int
  • float
  • Add: The addition operator +.
  • Sub: The subtraction operator -.
  • Mul: The multiplication operator *.
  • Neg: The unary negation operator -x.
  • Rem: The remainder operator %.
  • Pow: The exponentiation operator **.
↑ ↓ navigate ↵ open