diff --git a/docs/type.rst b/docs/type.rst index 0173b74f2fda1..0f01ec8745852 100644 --- a/docs/type.rst +++ b/docs/type.rst @@ -1,33 +1,8 @@ Type system =========== -Taichi supports many data types. The type name is recognized as -a *prefix character* + a *digital number*. - -The *prefix character* can be one of: - -- ``i`` for signed integers, e.g. 233, -666 -- ``u`` for unsigned integers, e.g. 233, 666 -- ``f`` for floating point numbers, e.g. 2.33, 1e-4 - -The *digital number* can be one of: - -- ``8`` -- ``16`` -- ``32`` -- ``64`` - -It represents how many **bits** are used in storing the data. -The larger the bit number, the higher the precision is. - -For example, the two most commonly used types: - -- ``i32`` represents a 32-bit signed integer. -- ``f32`` represents a 32-bit floating pointer number. - Supported types --------------- - Currently, supported basic types in Taichi are - int8 ``ti.i8`` @@ -71,21 +46,13 @@ Currently, supported basic types in Taichi are (OK: supported, EXT: require extension, N/A: not available) -.. note:: +Boolean types should be represented using ``ti.i32``. - Boolean types are represented using ``ti.i32``. +Binary operations on different types will give you a promoted type, following the C programming language, e.g. - - -Type promotion --------------- - -Binary operations on different types will give you a promoted type, following the C programming language convention, e.g.: - -- ``i32 + f32 = f32`` (integer + float = float) -- ``i32 + i64 = i64`` (less-bits + more-bits = more-bits) - -Basically it will try to choose the more precise type to contain the result value. +- ``i32 + f32 = f32`` +- ``f32 + f64 = f64`` +- ``i32 + i64 = i64`` .. _default_precisions: @@ -95,7 +62,6 @@ Default precisions By default, numerical literals have 32-bit precisions. For example, ``42`` has type ``ti.i32`` and ``3.14`` has type ``ti.f32``. - Default integer and float-point precisions (``default_ip`` and ``default_fp``) can be specified when initializing Taichi: .. code-block:: python @@ -110,66 +76,24 @@ Default integer and float-point precisions (``default_ip`` and ``default_fp``) c Type casts ---------- -Implicit casts -************** - -WARNING: The type of a variable is **determinated on it's initialization**. - -When a *wide* variable is assigned with a *narrow* type, it will be -implicitly promoted to the *wide* type and no warning will be raised: +Use ``ti.cast`` to cast scalar values. .. code-block:: python - a = 1.7 - a = 1 - print(a) # 1.0 + a = 1.4 + b = ti.cast(a, ti.i32) + c = ti.cast(b, ti.f32) -When a *narrow* variable is assigned with a *wide* type, it will be -implicitly casted into the *narrow* type and Taichi will raise a warning: + # Equivalently, use ``int()`` and ``float()`` + # to convert values to default float-point/integer types + b = int(a) + c = float(b) -.. code-block:: python - - a = 1 - a = 1.7 - print(a) # 1 - -Explicit casts -************** - -You may use ``ti.cast`` to explicitly cast scalar values between different types: - -.. code-block:: python - - a = 1.7 - b = ti.cast(a, ti.i32) # 1 - c = ti.cast(b, ti.f32) # 1.0 - -Equivalently, use ``int()`` and ``float()`` to convert values to default float-point/integer types: - -.. code-block:: python - - a = 1.7 - b = int(a) # 1 - c = float(a) # 1.0 - -Casting vector / matrix elements -******************************** - -Type casts applied to vectors/matrices are element-wise: - -.. code-block:: python - - u = ti.Vector([2.3, 4.7]) - v = int(u) # ti.Vector([2, 4]) - # equivalent to: - v = ti.cast(u, ti.i32) # ti.Vector([2, 4]) - -Bit casting -*********** + # Element-wise casts in matrices + mat = ti.Matrix([[3.0, 0.0], [0.3, 0.1]]) + mat_int = mat.cast(int) + mat_int2 = mat.cast(ti.i32) Use ``ti.bit_cast`` to bit-cast a value into another data type. The underlying bits will be preserved in this cast. The new type must have the same width as the the old type. - -.. code-block:: - - For people from C++, ``ti.bit_cast`` is equivalent to ``reinterpret_cast``. +For example, bit-casting ``i32`` to ``f64`` is not allowed. Use this operation with caution.