src/pylib/ops

Source   Edit  

why no /= defined?

For template `/=`*(x: var SomeInteger, y: SomeInteger):

Nim is static-typed, but /= will cause lhs convert from int to float

Procs

func `%`[T: SomeNumber](a, b: T): T
Python's modulo (a.k.a floor modulo) (quotient)
Hint: Nim's mod is the same as a - b * (a div b) (remainder), just like C.

Example:

assert 13 % -3 == -2
assert -13 % 3 == 2
Source   Edit  
func `//`[A, B: SomeFloat | SomeInteger](a: A; b: B): SomeNumber {.inline.}
Python's division (a.k.a. floor division)
Hint: Nim's div is division with any fractional part discarded (a.k.a."truncation toward zero"), just like C.

Example:

assert 13 // -3 == -5
assert 13 div -3 == -4
Source   Edit  
func divmod[T: SomeNumber](x, y: T): (T, T)
differs from std/math divmod, see // and % for details. Source   Edit  

Templates

template `%`[A: SomeFloat; B: SomeInteger](a: A; b: B): A
Source   Edit  
template `%`[A: SomeInteger; B: SomeFloat](a: A; b: B): B
Source   Edit  
template `%=`(self: var SomeNumber; x: SomeNumber)
Source   Edit  
template `**`[A: SomeFloat; B: SomeInteger](a: A; b: B): A
Source   Edit  
template `**`[A: SomeInteger; B: SomeFloat](a: A; b: B): B
Source   Edit  
template `**`[T: SomeFloat](a, b: T): T
Source   Edit  
template `**`[T: SomeInteger](a: T; b: static[int]): int | float

power for static int b

so that result type can be inferred:

  • int if b >= 0
  • float if b < 0

For non-static int b, `**`(a: T; b: Natural) is used.

Example:

const f = 5 ** -1  # only when the rhs is static[int]
assert f == 0.2
const i = 5 ** 2
assert i == int(25)
Source   Edit  
template `**`[T](a: T; b: Natural): T

power for b is a non-static integer:

only Natural is acceptable unless b is static, see `**`(a: T; b: static[int])

Example:

var i = -1
doAssertRaises RangeDefect:
  discard (5 ** i)
  ## this runs iff `i` is of static[int]
  ## e.g. `5 ** -1`
Source   Edit  
template `**=`(a: var SomeNumber; b: SomeNumber)
Source   Edit  
template `/`(x: SomeInteger; y: SomeInteger): float
Source   Edit  
template `//=`[A, B: SomeFloat | SomeInteger](a: var A; b: B)
Source   Edit  
template `<`[A: SomeFloat; B: SomeInteger](a: A; b: B): bool
Source   Edit  
template `<`[A: SomeInteger; B: SomeFloat](a: A; b: B): bool
Source   Edit  
template `<<`[I: SomeInteger](a, b: I): I
Warning: this causes overflow silently. Yet Python's int never overflows.
Source   Edit  
template `<<=`[I: SomeInteger](a, b: I)
Source   Edit  
template `<=`[A: SomeFloat; B: SomeInteger](a: A; b: B): bool
Source   Edit  
template `<=`[A: SomeInteger; B: SomeFloat](a: A; b: B): bool
Source   Edit  
template `<>`[A: SomeFloat; B: SomeInteger](a: A; b: B): bool
Source   Edit  
template `<>`[A: SomeInteger; B: SomeFloat](a: A; b: B): bool
Source   Edit  
template `==`(a, b: typedesc): bool
Compare 2 typedesc like Python.

Example:

doAssert type(1) == type(2)
Source   Edit  
template `==`[A: SomeFloat; B: SomeInteger](a: A; b: B): bool
Source   Edit  
template `==`[A: SomeInteger; B: SomeFloat](a: A; b: B): bool
Source   Edit  
template `>>`[I: SomeInteger](a, b: I): I
Source   Edit  
template `>>=`[I: SomeInteger](a, b: I)
Source   Edit