From 23ee9e00e0f45dd9595a3b16633f9e60d3a4e3c6 Mon Sep 17 00:00:00 2001 From: Herman <cai.keith@gmail.com> Date: Thu, 26 May 2022 16:37:00 +0800 Subject: [PATCH 1/2] Editorial updates --- docs/lang/articles/basic/type.md | 47 ++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/docs/lang/articles/basic/type.md b/docs/lang/articles/basic/type.md index 1e2fd7511ced5..c874c5d3a7e2a 100644 --- a/docs/lang/articles/basic/type.md +++ b/docs/lang/articles/basic/type.md @@ -102,31 +102,42 @@ As a rule of thumb, implicit type casting is a major source of bugs. And Taichi Taichi implements its own implicit type casting rules for binary operations, which are slightly different from [those for the C programming language](https://en.cppreference.com/w/c/language/conversion). -In general we have three rules with descending priority: +In general we have three rules in descending order of priority: -1. integral OP floating_point -> floating_point -- `i32 + f32 -> f32` -- `i16 + f16 -> f16` +1. Integer + floading point -> floating point -2. low_precision_bits OP high_precision_bits -> high_precision_bits -- `i16 + i32 -> i32` -- `u8 + u16 -> u16` + - `i32 + f32 -> f32` -3. signed OP unsigned -> unsigned -- `u32 + i32 -> u32` -- `u8 + i8 -> u8` + - `i16 + f16 -> f16` -For conflicting rules, only the highest priority one will be applied. -- `u8 + i16 -> i16` (rule #2 conflicts with rule #3: apply rule #2) -- `f16 + i32 -> f16` (rule #1 conflicts with rule #2: apply rule #1) +2. Low-precision bits + high-precision bits -> high-precision bits + + - `i16 + i32 -> i32` + - `f16 + f32 -> f32` + + - `u8 + u16 -> u16` + +3. Signed integer + unsigned integer -> unsigned integer + + - `u32 + i32 -> u32` + + - `u8 + i8 -> u8` + + +When it comes to rule conflicts, the rule of the highest priority applies: +- `u8 + i16 -> i16` (when rule #2 conflicts with rule #3, rule #2 applies.) +- `f16 + i32 -> f16` (when rule #1 conflicts with rule #2, rule #1 applies.) A few exceptions: -1. bit-shift operations: always follow lhs's dtype -- `u8 << i32 -> u8` -- `i16 << i8 -> i16` +- Bit-shift operations always follow lhs' (left hand side's) data type: + + - `u8 << i32 -> u8` + + - `i16 << i8 -> i16` + -2. logical operations: always return i32 -3. comparison operations: always return i32 +- Logical operations always return `i32`. +- Comparison operations always return `i32`. #### Implicit type casting in assignments From 9a2abe887bf5c0be0b3f5be2caeff0f449402d0d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 26 May 2022 09:33:17 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/lang/articles/basic/type.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/type.md b/docs/lang/articles/basic/type.md index ea074b358d8f4..3fc01da91f6d3 100644 --- a/docs/lang/articles/basic/type.md +++ b/docs/lang/articles/basic/type.md @@ -115,7 +115,7 @@ Taichi implements its own implicit type casting rules for binary operations, whi - `u32 + i32 -> u32` - `u8 + i8 -> u8` -When it comes to rule conflicts, the rule of the highest priority applies: +When it comes to rule conflicts, the rule of the highest priority applies: - `u8 + i16 -> i16` (when rule #2 conflicts with rule #3, rule #2 applies.) - `f16 + i32 -> f16` (when rule #1 conflicts with rule #2, rule #1 applies.)