| 1 |
.. _int-ops: |
| 2 |
|
| 3 |
Native integer operations |
| 4 |
========================= |
| 5 |
|
| 6 |
Mypyc supports these integer types: |
| 7 |
|
| 8 |
* ``int`` (arbitrary-precision integer) |
| 9 |
* ``i64`` (64-bit signed integer) |
| 10 |
* ``i32`` (32-bit signed integer) |
| 11 |
* ``i16`` (16-bit signed integer) |
| 12 |
* ``u8`` (8-bit unsigned integer) |
| 13 |
|
| 14 |
``i64``, ``i32``, ``i16`` and ``u8`` are *native integer types* and |
| 15 |
are available in the ``mypy_extensions`` module. ``int`` corresponds |
| 16 |
to the Python ``int`` type, but uses a more efficient runtime |
| 17 |
representation (tagged pointer). Native integer types are value types. |
| 18 |
|
| 19 |
All integer types have optimized primitive operations, but the native |
| 20 |
integer types are more efficient than ``int``, since they don't |
| 21 |
require range or bounds checks. |
| 22 |
|
| 23 |
Operations on integers that are listed here have fast, optimized |
| 24 |
implementations. Other integer operations use generic implementations |
| 25 |
that are generally slower. Some operations involving integers and other |
| 26 |
types, such as list indexing, are documented elsewhere. |
| 27 |
|
| 28 |
Construction |
| 29 |
------------ |
| 30 |
|
| 31 |
``int`` type: |
| 32 |
|
| 33 |
* Integer literal |
| 34 |
* ``int(x: float)`` |
| 35 |
* ``int(x: i64)`` |
| 36 |
* ``int(x: i32)`` |
| 37 |
* ``int(x: i16)`` |
| 38 |
* ``int(x: u8)`` |
| 39 |
* ``int(x: str)`` |
| 40 |
* ``int(x: str, base: int)`` |
| 41 |
* ``int(x: int)`` (no-op) |
| 42 |
|
| 43 |
``i64`` type: |
| 44 |
|
| 45 |
* ``i64(x: int)`` |
| 46 |
* ``i64(x: float)`` |
| 47 |
* ``i64(x: i64)`` (no-op) |
| 48 |
* ``i64(x: i32)`` |
| 49 |
* ``i64(x: i16)`` |
| 50 |
* ``i64(x: u8)`` |
| 51 |
* ``i64(x: str)`` |
| 52 |
* ``i64(x: str, base: int)`` |
| 53 |
|
| 54 |
``i32`` type: |
| 55 |
|
| 56 |
* ``i32(x: int)`` |
| 57 |
* ``i32(x: float)`` |
| 58 |
* ``i32(x: i64)`` (truncate) |
| 59 |
* ``i32(x: i32)`` (no-op) |
| 60 |
* ``i32(x: i16)`` |
| 61 |
* ``i32(x: u8)`` |
| 62 |
* ``i32(x: str)`` |
| 63 |
* ``i32(x: str, base: int)`` |
| 64 |
|
| 65 |
``i16`` type: |
| 66 |
|
| 67 |
* ``i16(x: int)`` |
| 68 |
* ``i16(x: float)`` |
| 69 |
* ``i16(x: i64)`` (truncate) |
| 70 |
* ``i16(x: i32)`` (truncate) |
| 71 |
* ``i16(x: i16)`` (no-op) |
| 72 |
* ``i16(x: u8)`` |
| 73 |
* ``i16(x: str)`` |
| 74 |
* ``i16(x: str, base: int)`` |
| 75 |
|
| 76 |
Conversions from ``int`` to a native integer type raise |
| 77 |
``OverflowError`` if the value is too large or small. Conversions from |
| 78 |
a wider native integer type to a narrower one truncate the value and never |
| 79 |
fail. More generally, operations between native integer types don't |
| 80 |
check for overflow. |
| 81 |
|
| 82 |
Implicit conversions |
| 83 |
-------------------- |
| 84 |
|
| 85 |
``int`` values can be implicitly converted to a native integer type, |
| 86 |
for convenience. This means that these are equivalent:: |
| 87 |
|
| 88 |
from mypy_extensions import i64 |
| 89 |
|
| 90 |
def implicit() -> None: |
| 91 |
# Implicit conversion of 0 (int) to i64 |
| 92 |
x: i64 = 0 |
| 93 |
|
| 94 |
def explicit() -> None: |
| 95 |
# Explicit conversion of 0 (int) to i64 |
| 96 |
x = i64(0) |
| 97 |
|
| 98 |
Similarly, a native integer value can be implicitly converted to an |
| 99 |
arbitrary-precision integer. These two functions are equivalent:: |
| 100 |
|
| 101 |
def implicit(x: i64) -> int: |
| 102 |
# Implicit conversion from i64 to int |
| 103 |
return x |
| 104 |
|
| 105 |
def explicit(x: i64) -> int: |
| 106 |
# Explicit conversion from i64 to int |
| 107 |
return int(x) |
| 108 |
|
| 109 |
Operators |
| 110 |
--------- |
| 111 |
|
| 112 |
* Arithmetic (``+``, ``-``, ``*``, ``//``, ``/``, ``%``) |
| 113 |
* Bitwise operations (``&``, ``|``, ``^``, ``<<``, ``>>``, ``~``) |
| 114 |
* Comparisons (``==``, ``!=``, ``<``, etc.) |
| 115 |
* Augmented assignment (``x += y``, etc.) |
| 116 |
|
| 117 |
If one of the above native integer operations overflows or underflows |
| 118 |
with signed operands, the behavior is undefined. Signed native integer |
| 119 |
types should only be used if all possible values are small enough for |
| 120 |
the type. For this reason, the arbitrary-precision ``int`` type is |
| 121 |
recommended for signed values unless the performance of integer |
| 122 |
operations is critical. |
| 123 |
|
| 124 |
Operations on unsigned integers (``u8``) wrap around on overflow. |
| 125 |
|
| 126 |
It's a compile-time error to mix different native integer types in a |
| 127 |
binary operation such as addition. An explicit conversion is required:: |
| 128 |
|
| 129 |
from mypy_extensions import i64, i32 |
| 130 |
|
| 131 |
def add(x: i64, y: i32) -> None: |
| 132 |
a = x + y # Error (i64 + i32) |
| 133 |
b = x + i64(y) # OK |
| 134 |
|
| 135 |
You can freely mix a native integer value and an arbitrary-precision |
| 136 |
``int`` value in an operation. The native integer type is "sticky" |
| 137 |
and the ``int`` operand is coerced to the native integer type:: |
| 138 |
|
| 139 |
def example(x: i64, y: int) -> None: |
| 140 |
a = x * y |
| 141 |
# Type of "a" is "i64" |
| 142 |
... |
| 143 |
b = 1 - x |
| 144 |
# Similarly, type of "b" is "i64" |
| 145 |
|
| 146 |
Methods |
| 147 |
------- |
| 148 |
|
| 149 |
* ``n.bit_length()`` |
| 150 |
* ``n.to_bytes(length: int, byteorder: str)`` |
| 151 |
* ``n.to_bytes(length: int, byteorder: str, signed: bool)`` |
| 152 |
|
| 153 |
Statements |
| 154 |
---------- |
| 155 |
|
| 156 |
For loop over a range is compiled efficiently, if the ``range(...)`` object |
| 157 |
is constructed in the for statement (after ``in``): |
| 158 |
|
| 159 |
* ``for x in range(end)`` |
| 160 |
* ``for x in range(start, end)`` |
| 161 |
* ``for x in range(start, end, step)`` |
| 162 |
|
| 163 |
If one of the arguments to ``range`` in a for loop is a native integer |
| 164 |
type, the type of the loop variable is inferred to have this native |
| 165 |
integer type, instead of ``int``:: |
| 166 |
|
| 167 |
for x in range(i64(n)): |
| 168 |
# Type of "x" is "i64" |
| 169 |
... |