Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] Updated documentations for implicit type casting rules #4885

Merged
merged 2 commits into from
May 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/lang/articles/basic/type.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,31 @@ As a rule of thumb, implicit type casting is a major source of bugs. And Taichi

#### Implicit type casting in binary operations

Taichi follows the [implicit conversion rules](https://en.cppreference.com/w/c/language/conversion) for the C programming language and implicitly casts operands in a [binary operation](https://en.wikipedia.org/wiki/Binary_operation) into a *common type* if the operation involves different data types. Following are two most straightforward rules for determining the common type in a binary operation:
Taichi mostly follows the [implicit conversion rules](https://en.cppreference.com/w/c/language/conversion) for the C programming language and implicitly casts operands in a [binary operation](https://en.wikipedia.org/wiki/Binary_operation) into a *common type* if the operation involves different data types. Following are two most straightforward rules for determining the common type in a binary operation:

- `i32 + f32 = f32` (`int` + `float` = `float`)
- `i32 + i64 = i64` (low precision bits + high precision bits = high precision bits)

Note that in C++: `i8 + i8 = i32`, which makes no sense to Taichi use cases. Therefore we implemented our own rules for this kind of operations:
```
i8 + i8 = i8
i8 + i16 = i16
i8 + u8 = u8
i8 + u16 = u16
i16 + i8 = i16
i16 + i16 = i16
i16 + u8 = i16
i16 + u16 = u16
u8 + i8 = u8
u8 + i16 = i16
u8 + u8 = u8
u8 + u16 = u16
u16 + i8 = u16
u16 + i16 = u16
u16 + u8 = u16
u16 + u16 = u16
```

#### Implicit type casting in assignments

When you assign a value to a variable of a different data type, Taichi implicitly casts the value into that type. Further, if the type of the variable is *not* the common type, a warning of precision loss occurs.
Expand Down