Mojo struct
Int
@register_passable(trivial)
struct Int
This type represents an integer value.
Aliases
MAX = __init__[::Intable](SIMD(max_or_inf[::DType]()))
: Returns the maximum integer value.MIN = __init__[::Intable](SIMD(min_or_neg_inf[::DType]()))
: Returns the minimum value of type.
Fields
- value (
index
): The underlying storage for the integer value.
Implemented traits
Absable
,
AnyType
,
Boolable
,
CeilDivable
,
CollectionElement
,
Comparable
,
ComparableCollectionElement
,
Copyable
,
EqualityComparable
,
EqualityComparableCollectionElement
,
ExplicitlyCopyable
,
GreaterThanComparable
,
GreaterThanOrEqualComparable
,
Hashable
,
ImplicitlyBoolable
,
Indexer
,
Intable
,
KeyElement
,
LessThanComparable
,
LessThanOrEqualComparable
,
Movable
,
Powable
,
Representable
,
Roundable
,
Stringable
,
UnknownDestructibility
,
Writable
,
_CopyableGreaterThanComparable
,
_CopyableLessThanComparable
,
_CurlyEntryFormattable
,
_HashableWithHasher
Methods
__init__
__init__() -> Self
Default constructor that produces zero.
@implicit
__init__(value: IntLiteral) -> Self
Construct Int from the given IntLiteral value.
Args:
- value (
IntLiteral
): The init value.
@implicit
__init__(value: UInt) -> Self
Construct Int from the given UInt value.
Args:
- value (
UInt
): The init value.
__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.
__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.
@implicit
__init__[I: ImplicitlyIntable](value: I) -> Self
Construct Int from implicitly convertable type.
Parameters:
- I (
ImplicitlyIntable
): The type that is implicitly convertable to anInt
.
Args:
- value (
I
): The init value.
__init__(out self, value: StringSlice[origin], base: UInt = 10)
Parses and returns the given string as an integer in the given base.
If base is set to 0, the string is parsed as an Integer literal, with the following considerations:
- '0b' or '0B' prefix indicates binary (base 2)
- '0o' or '0O' prefix indicates octal (base 8)
- '0x' or '0X' prefix indicates hexadecimal (base 16)
- Without a prefix, it's treated as decimal (base 10)
Examples: >>> Int("32") 32 >>> Int("FF", 16) 255 >>> Int("0xFF", 0) 255 >>> Int("0b1010", 0) 10
Notes: This follows Python's integer literals.
Args:
- value (
StringSlice[origin]
): A string to be parsed as an integer in the given base. - base (
UInt
): Base used for conversion, value must be between 2 and 36, or 0.
Raises:
If the given string cannot be parsed as an integer value or if an incorrect base is provided.
__bool__
__bool__(self) -> Bool
Convert this Int to Bool.
Returns:
False Bool value if the value is equal to 0 and True otherwise.
__neg__
__neg__(self) -> Self
Return -self.
Returns:
The -self value.
__pos__
__pos__(self) -> Self
Return +self.
Returns:
The +self value.
__invert__
__invert__(self) -> Self
Return ~self.
Returns:
The ~self value.
__lt__
__lt__(self, rhs: Self) -> Bool
Compare this Int to the RHS using LT comparison.
Args:
- rhs (
Self
): The other Int to compare against.
Returns:
True if this Int is less-than the RHS Int and False otherwise.
__le__
__le__(self, rhs: Self) -> Bool
Compare this Int to the RHS using LE comparison.
Args:
- rhs (
Self
): The other Int to compare against.
Returns:
True if this Int is less-or-equal than the RHS Int and False otherwise.
__eq__
__eq__(self, rhs: Self) -> Bool
Compare this Int to the RHS using EQ comparison.
Args:
- rhs (
Self
): The other Int to compare against.
Returns:
True if this Int is equal to the RHS Int and False otherwise.
__ne__
__ne__(self, rhs: Self) -> Bool
Compare this Int to the RHS using NE comparison.
Args:
- rhs (
Self
): The other Int to compare against.
Returns:
True if this Int is non-equal to the RHS Int and False otherwise.
__gt__
__gt__(self, rhs: Self) -> Bool
Compare this Int to the RHS using GT comparison.
Args:
- rhs (
Self
): The other Int to compare against.
Returns:
True if this Int is greater-than the RHS Int and False otherwise.
__ge__
__ge__(self, rhs: Self) -> Bool
Compare this Int to the RHS using GE comparison.
Args:
- rhs (
Self
): The other Int to compare against.
Returns:
True if this Int is greater-or-equal than the RHS Int and False otherwise.
__add__
__add__(self, rhs: Self) -> Self
Return self + rhs
.
Args:
- rhs (
Self
): The value to add.
Returns:
self + rhs
value.
__sub__
__sub__(self, rhs: Self) -> Self
Return self - rhs
.
Args:
- rhs (
Self
): The value to subtract.
Returns:
self - rhs
value.
__mul__
__mul__(self, rhs: Self) -> Self
Return self * rhs
.
Args:
- rhs (
Self
): The value to multiply with.
Returns:
self * rhs
value.
__truediv__
__truediv__(self, rhs: Self) -> SIMD[float64, 1]
Return the floating point division of self
and rhs
.
Args:
- rhs (
Self
): The value to divide on.
Returns:
Float64(self)/Float64(rhs)
value.
__floordiv__
__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:
floor(self/rhs)
value.
__mod__
__mod__(self, rhs: Self) -> Self
Return the remainder of self divided by rhs.
Args:
- rhs (
Self
): The value to divide on.
Returns:
The remainder of dividing self by rhs.
__pow__
__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:
The value of self
raised to the power of exp
.
__lshift__
__lshift__(self, rhs: Self) -> Self
Return self << rhs
.
Args:
- rhs (
Self
): The value to shift with.
Returns:
self << rhs
.
__rshift__
__rshift__(self, rhs: Self) -> Self
Return self >> rhs
.
Args:
- rhs (
Self
): The value to shift with.
Returns:
self >> rhs
.
__and__
__and__(self, rhs: Self) -> Self
Return self & rhs
.
Args:
- rhs (
Self
): The RHS value.
Returns:
self & rhs
.
__or__
__or__(self, rhs: Self) -> Self
Return self | rhs
.
Args:
- rhs (
Self
): The RHS value.
Returns:
self | rhs
.
__xor__
__xor__(self, rhs: Self) -> Self
Return self ^ rhs
.
Args:
- rhs (
Self
): The RHS value.
Returns:
self ^ rhs
.
__radd__
__radd__(self, value: Self) -> Self
Return value + self
.
Args:
- value (
Self
): The other value.
Returns:
value + self
.
__rsub__
__rsub__(self, value: Self) -> Self
Return value - self
.
Args:
- value (
Self
): The other value.
Returns:
value - self
.
__rmul__
__rmul__(self, value: Self) -> Self
Return value * self
.
Args:
- value (
Self
): The other value.
Returns:
value * self
.
__rfloordiv__
__rfloordiv__(self, value: Self) -> Self
Return value // self
.
Args:
- value (
Self
): The other value.
Returns:
value // self
.
__rmod__
__rmod__(self, value: Self) -> Self
Return value % self
.
Args:
- value (
Self
): The other value.
Returns:
value % self
.
__rpow__
__rpow__(self, value: Self) -> Self
Return pow(value,self)
.
Args:
- value (
Self
): The other value.
Returns:
pow(value,self)
.
__rlshift__
__rlshift__(self, value: Self) -> Self
Return value << self
.
Args:
- value (
Self
): The other value.
Returns:
value << self
.
__rrshift__
__rrshift__(self, value: Self) -> Self
Return value >> self
.
Args:
- value (
Self
): The other value.
Returns:
value >> self
.
__rand__
__rand__(self, value: Self) -> Self
Return value & self
.
Args:
- value (
Self
): The other value.
Returns:
value & self
.
__ror__
__ror__(self, value: Self) -> Self
Return value | self
.
Args:
- value (
Self
): The other value.
Returns:
value | self
.
__rxor__
__rxor__(self, value: Self) -> Self
Return value ^ self
.
Args:
- value (
Self
): The other value.
Returns:
value ^ self
.
__iadd__
__iadd__(mut self, rhs: Self)
Compute self + rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__isub__
__isub__(mut self, rhs: Self)
Compute self - rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__imul__
__imul__(mut self, rhs: Self)
Compute self*rhs and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__itruediv__
__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__
__ifloordiv__(mut self, rhs: Self)
Compute self // rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__imod__
__imod__(mut self, rhs: Self)
Compute self % rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ipow__
__ipow__(mut self, rhs: Self)
Compute pow(self, rhs)
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ilshift__
__ilshift__(mut self, rhs: Self)
Compute self << rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__irshift__
__irshift__(mut self, rhs: Self)
Compute self >> rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__iand__
__iand__(mut self, rhs: Self)
Compute self & rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ixor__
__ixor__(mut self, rhs: Self)
Compute self ^ rhs
and save the result in self.
Args:
- rhs (
Self
): The RHS value.
__ior__
__ior__(mut self, rhs: Self)
Compute self|rhs and save the result in self.
Args:
- rhs (
Self
): The RHS value.
copy
copy(self) -> Self
Explicitly copy the provided value.
Returns:
A copy of the value.
__divmod__
__divmod__(self, rhs: Self) -> Tuple[Int, Int]
Computes both the quotient and remainder using integer division.
Args:
- rhs (
Self
): The value to divide on.
Returns:
The quotient and remainder as a Tuple(self // rhs, self % rhs)
.
__as_bool__
__as_bool__(self) -> Bool
Convert this Int to Bool.
Returns:
False Bool value if the value is equal to 0 and True otherwise.
__int__
__int__(self) -> Self
Gets the integral value (this is an identity function for Int).
Returns:
The value as an integer.
__abs__
__abs__(self) -> Self
Return the absolute value of the Int value.
Returns:
The absolute value.
__ceil__
__ceil__(self) -> Self
Return the ceiling of the Int value, which is itself.
Returns:
The Int value itself.
__floor__
__floor__(self) -> Self
Return the floor of the Int value, which is itself.
Returns:
The Int value itself.
__round__
__round__(self) -> Self
Return the rounded value of the Int value, which is itself.
Returns:
The Int value itself.
__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:
The Int value itself if ndigits >= 0 else the rounded value.
__trunc__
__trunc__(self) -> Self
Return the truncated Int value, which is itself.
Returns:
The Int value itself.
__str__
__str__(self) -> String
Get the integer as a string.
Returns:
A string representation.
__repr__
__repr__(self) -> String
Get the integer as a string. Returns the same String
as __str__
.
Returns:
A string representation.
__hash__
__hash__(self) -> UInt
Hash the int using builtin hash.
Returns:
A 64-bit hash value. This value is not suitable for cryptographic uses. Its intended usage is for data structures. See the hash
builtin documentation for more details.
__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.
__ceildiv__
__ceildiv__(self, denominator: Self) -> Self
Return the rounded-up result of dividing self by denominator.
Args:
- denominator (
Self
): The denominator.
Returns:
The ceiling of dividing numerator by denominator.
write_to
write_to[W: Writer](self, mut writer: W)
Formats this integer to the provided Writer.
Parameters:
- W (
Writer
): A type conforming to the Writable trait.
Args:
- writer (
W
): The object to write to.
write_padded
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.
__index__
__index__(self) -> index
Convert to index.
Returns:
The corresponding __mlir_type.index value.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!