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