int_operations.rst 3.9 KB

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