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).
Int
struct Int
This type represents an integer value.
Implemented traits
Absable,
AnyType,
Boolable,
CeilDivable,
Ceilable,
Comparable,
ConvertibleFromPython,
CoordLike,
Copyable,
Defaultable,
DevicePassable,
DivModable,
Equatable,
Floorable,
Hashable,
ImplicitlyCopyable,
ImplicitlyDestructible,
Indexer,
Intable,
IntervalElement,
Movable,
Powable,
RegisterPassable,
Roundable,
TrivialRegisterPassable,
Truncable,
Writable,
_FromInt
comptime members
BITWIDTH
comptime BITWIDTH = bit_width_of[DType.int]()
The bit width of the integer type.
device_type
comptime device_type = Int
Int is remapped to the same type when passed to accelerator devices.
DTYPE
comptime DTYPE = DType.int
The data type for the runtime integer value.
is_static_value
comptime is_static_value = False
True if the value is known at compile time.
is_tuple
comptime is_tuple = False
True if this is a tuple type (Coord), False for scalar values.
is_value
comptime is_value = True
True if this is a scalar value, False for tuple types.
MAX
comptime MAX = Int[Int](Int.MAX)
Returns the maximum integer value.
MIN
comptime MIN = Int[Int](Int.MIN)
Returns the minimum value of type.
ParamListType
comptime ParamListType = *?
The element types (Self for scalar types).
static_value
comptime static_value = -1
Always -1 for runtime values (not statically known).
Methods
__init__
def __init__() -> Self
Default constructor that produces zero.
@implicit
def __init__(value: IntLiteral) -> Self
Construct Int from the given IntLiteral value.
Args:
- value (
IntLiteral): The init value.
def __init__[T: Intable](value: T) -> Self
Get the Int representation of the value.
Parameters:
- T (
Intable): The Intable type.
Args:
- value (
T): The object to get the integral representation of.
def __init__[T: IntableRaising](out self, value: T)
Get the Int representation of the value.
Parameters:
- T (
IntableRaising): The Intable type.
Args:
- value (
T): The object to get the integral representation of.
Raises:
If the type does not have an integral representation.
__bool__
def __bool__(self) -> Bool
Convert this Int to Bool.
Returns:
Bool: False Bool value if the value is equal to 0 and True otherwise.
__neg__
def __neg__(self) -> Self
Return -self.
Returns:
Self: The -self value.
__pos__
def __pos__(self) -> Self
Return +self.
Returns:
Self: The +self value.
__invert__
def __invert__(self) -> Self
Return ~self.
Returns:
Self: The ~self value.
__lt__
def __lt__(self, rhs: Self) -> Bool
Compare this Int to the RHS using LT comparison.
Args:
- rhs (
Self): The other Int to compare against.
Returns:
Bool: True if this Int is less-than the RHS Int and False otherwise.
__le__
def __le__(self, rhs: Self) -> Bool
Compare this Int to the RHS using LE comparison.
Args:
- rhs (
Self): The other Int to compare against.
Returns:
Bool: True if this Int is less-or-equal than the RHS Int and False
otherwise.
__eq__
def __eq__(self, rhs: Self) -> Bool
Compare this Int to the RHS using EQ comparison.
Args:
- rhs (
Self): The other Int to compare against.
Returns:
Bool: True if this Int is equal to the RHS Int and False otherwise.
__ne__
def __ne__(self, rhs: Self) -> Bool
Compare this Int to the RHS using NE comparison.
Args:
- rhs (
Self): The other Int to compare against.
Returns:
Bool: True if this Int is non-equal to the RHS Int and False otherwise.
__gt__
def __gt__(self, rhs: Self) -> Bool
Compare this Int to the RHS using GT comparison.
Args:
- rhs (
Self): The other Int to compare against.
Returns:
Bool: True if this Int is greater than the RHS Int and False otherwise.
__ge__
def __ge__(self, rhs: Self) -> Bool
Compare this Int to the RHS using GE comparison.
Args:
- rhs (
Self): The other Int to compare against.
Returns:
Bool: True if this Int is greater-or-equal than the RHS Int and False
otherwise.
__add__
def __add__(self, rhs: Self) -> Self
Return self + rhs.
Args:
- rhs (
Self): The value to add.
Returns:
Self: self + rhs value.
__sub__
def __sub__(self, rhs: Self) -> Self
Return self - rhs.
Args:
- rhs (
Self): The value to subtract.
Returns:
Self: self - rhs value.
__mul__
def __mul__(self, rhs: Self) -> Self
Return self * rhs.
Args:
- rhs (
Self): The value to multiply with.
Returns:
Self: self * rhs value.
__truediv__
def __truediv__(self, rhs: Self) -> Self
Return the result of the division of self and rhs.
Performs truncating division (toward zero) for integers.
Args:
- rhs (
Self): The value to divide on.
Returns:
Self: self / rhs value.
__floordiv__
def __floordiv__(self, rhs: Self) -> Self
Return the division of self and rhs rounded down to the nearest integer.
Args:
- rhs (
Self): The value to divide on.
Returns:
Self: floor(self/rhs) value.
__mod__
def __mod__(self, rhs: Self) -> Self
Return the remainder of self divided by rhs.
Args:
- rhs (
Self): The value to divide on.
Returns:
Self: The remainder of dividing self by rhs.
__pow__
def __pow__(self, exp: Self) -> Self
Return the value raised to the power of the given exponent.
Computes the power of an integer using the Russian Peasant Method.
Args:
- exp (
Self): The exponent value.
Returns:
Self: The value of self raised to the power of exp.
__lshift__
def __lshift__(self, rhs: Self) -> Self
Return self << rhs.
Args:
- rhs (
Self): The value to shift with.
Returns:
Self: self << rhs.
__rshift__
def __rshift__(self, rhs: Self) -> Self
Return self >> rhs.
Args:
- rhs (
Self): The value to shift with.
Returns:
Self: self >> rhs.
__and__
def __and__(self, rhs: Self) -> Self
Return self & rhs.
Args:
- rhs (
Self): The RHS value.
Returns:
Self: self & rhs.
__or__
def __or__(self, rhs: Self) -> Self
Return self | rhs.
Args:
- rhs (
Self): The RHS value.
Returns:
Self: self | rhs.
__xor__
def __xor__(self, rhs: Self) -> Self
Return self ^ rhs.
Args:
- rhs (
Self): The RHS value.
Returns:
Self: self ^ rhs.
__radd__
def __radd__(self, value: Self) -> Self
Return value + self.
Args:
- value (
Self): The other value.
Returns:
Self: value + self.
__rsub__
def __rsub__(self, value: Self) -> Self
Return value - self.
Args:
- value (
Self): The other value.
Returns:
Self: value - self.
__rmul__
def __rmul__(self, value: Self) -> Self
Return value * self.
Args:
- value (
Self): The other value.
Returns:
Self: value * self.
__rfloordiv__
def __rfloordiv__(self, value: Self) -> Self
Return value // self.
Args:
- value (
Self): The other value.
Returns:
Self: value // self.
__rmod__
def __rmod__(self, value: Self) -> Self
Return value % self.
Args:
- value (
Self): The other value.
Returns:
Self: value % self.
__rpow__
def __rpow__(self, value: Self) -> Self
Return pow(value,self).
Args:
- value (
Self): The other value.
Returns:
Self: pow(value,self).
__rlshift__
def __rlshift__(self, value: Self) -> Self
Return value << self.
Args:
- value (
Self): The other value.
Returns:
Self: value << self.
__rrshift__
def __rrshift__(self, value: Self) -> Self
Return value >> self.
Args:
- value (
Self): The other value.
Returns:
Self: value >> self.
__rand__
def __rand__(self, value: Self) -> Self
Return value & self.
Args:
- value (
Self): The other value.
Returns:
Self: value & self.
__ror__
def __ror__(self, value: Self) -> Self
Return value | self.
Args:
- value (
Self): The other value.
Returns:
Self: value | self.
__rxor__
def __rxor__(self, value: Self) -> Self
Return value ^ self.
Args:
- value (
Self): The other value.
Returns:
Self: value ^ self.
__iadd__
def __iadd__(mut self, rhs: Self)
Compute self + rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__isub__
def __isub__(mut self, rhs: Self)
Compute self - rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__imul__
def __imul__(mut self, rhs: Self)
Compute self*rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__itruediv__
def __itruediv__(mut self, rhs: Self)
Compute self / rhs, convert to int, and save the result in self.
Since floor(self / rhs) is equivalent to self // rhs, this yields
the same as __ifloordiv__.
Args:
- rhs (
Self): The RHS value.
__ifloordiv__
def __ifloordiv__(mut self, rhs: Self)
Compute self // rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__imod__
def __imod__(mut self, rhs: Self)
Compute self % rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__ipow__
def __ipow__(mut self, rhs: Self)
Compute pow(self, rhs) and save the result in self.
Args:
- rhs (
Self): The RHS value.
__ilshift__
def __ilshift__(mut self, rhs: Self)
Compute self << rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__irshift__
def __irshift__(mut self, rhs: Self)
Compute self >> rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__iand__
def __iand__(mut self, rhs: Self)
Compute self & rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__ixor__
def __ixor__(mut self, rhs: Self)
Compute self ^ rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
__ior__
def __ior__(mut self, rhs: Self)
Compute self|rhs and save the result in self.
Args:
- rhs (
Self): The RHS value.
get_type_name
static def get_type_name() -> String
Gets this type's name, for use in error messages when handing arguments to kernels. TODO: This will go away soon, when we get better error messages for kernel calls.
Returns:
String: This type's name.
__divmod__
def __divmod__(self, rhs: Self) -> Tuple[Self, Self]
Computes both the quotient and remainder using integer division.
Args:
- rhs (
Self): The value to divide on.
Returns:
Tuple[Self, Self]: The quotient and remainder as a tuple (self // rhs, self % rhs).
__int__
def __int__(self) -> Self
Gets the integral value (this is an identity function for Int).
Returns:
Self: The value as an integer.
__abs__
def __abs__(self) -> Self
Return the absolute value of the Int value.
The absolute value of Int.MIN is Int.MIN.
Returns:
Self: The absolute value.
__ceil__
def __ceil__(self) -> Self
Return the ceiling of the Int value, which is itself.
Returns:
Self: The Int value itself.
__floor__
def __floor__(self) -> Self
Return the floor of the Int value, which is itself.
Returns:
Self: The Int value itself.
__round__
def __round__(self) -> Self
Return the rounded value of the Int value, which is itself.
Returns:
Self: The Int value itself.
def __round__(self, ndigits: Self) -> Self
Return the rounded value of the Int value, which is itself.
Args:
- ndigits (
Self): The number of digits to round to.
Returns:
Self: The Int value itself if ndigits >= 0 else the rounded value.
__trunc__
def __trunc__(self) -> Self
Return the truncated Int value, which is itself.
Returns:
Self: The Int value itself.
__ceildiv__
def __ceildiv__(self, denominator: Self) -> Self
Return the rounded-up result of dividing self by denominator.
Args:
- denominator (
Self): The denominator.
Returns:
Self: The ceiling of dividing numerator by denominator.
is_power_of_two
def is_power_of_two(self) -> Bool
Check if the integer is a (non-zero) power of two.
Returns:
Bool: True if the integer is a power of two, False otherwise.
write_to
def write_to(self, mut writer: T)
Formats this integer to the provided Writer.
Args:
- writer (
T): The object to write to.
write_repr_to
def write_repr_to(self, mut writer: T)
Write the string representation of the Int".
Args:
- writer (
T): The value to write to.
write_padded
def write_padded[W: Writer](self, mut writer: W, width: Self)
Write the int right-aligned to a set padding.
Parameters:
- W (
Writer): A type conforming to the Writable trait.
Args:
- writer (
W): The object to write to. - width (
Self): The amount to pad to the left.
__hash__
def __hash__[H: Hasher](self, mut hasher: H)
Updates hasher with this int value.
Parameters:
- H (
Hasher): The hasher type.
Args:
- hasher (
H): The hasher instance.
__len__
static def __len__() -> Self
Get the length (always 1 for scalar types).
Returns:
Self: Always returns 1.
product
def product(self) -> Int
Calculate the product (returns the value for scalar types).
Returns:
Int: The integer value.
sum
def sum(self) -> Int
Calculate the sum (returns the value for scalar types).
Returns:
Int: The integer value.
value
tuple
def tuple(var self) -> Coord[Int]
Get as a tuple (not valid for Scalar).
Returns:
Coord[Int]: Never returns; aborts at compile time.