IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: 1.1.0
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

DivModable

The DivModable trait describes a type that defines division and modulo operations returning both quotient and remainder.

Types that conform to DivModable will work with the builtin divmod function, which will return the same type as the inputs.

For example:

from std.math import DivModable

@fieldwise_init
struct Bytes(DivModable):
var size: Int

def __divmod__(self, other: Self) -> Tuple[Self, Self]:
var quotient_int = self.size // other.size
var remainder_int = self.size % other.size
return (Bytes(quotient_int), Bytes(remainder_int))

Implemented traits​

AnyType, Copyable, ImplicitlyCopyable, Movable

Required methods​

__divmod__​

def __divmod__(self, denominator: Self) -> Tuple[_Self, _Self]

Performs division and returns the quotient and the remainder.

Args:

  • ​denominator (_Self): The value to divide by.

Returns:

Tuple[_Self, _Self]: A Tuple containing the quotient and the remainder.