You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
* Property of division rounding down (division of real numbers without rounding them down won't have such property)
* Rounding towards -∞ mentioned and explained
* Bit masking example reference
Copy file name to clipboardExpand all lines: pages/book/operators.mdx
+19-9Lines changed: 19 additions & 9 deletions
Original file line number
Diff line number
Diff line change
@@ -84,14 +84,6 @@ Consider the following code:
84
84
85
85
Even though this example may be simple, neglection of precedence rules can often lead to confusing situations with operators. The correct order of operations can be ensured by wrapping every operation in [parentheses](#parentheses), since parentheses have the highest precedence of all expressions and operators there is.
86
86
87
-
That said, Tact's precedence levels are carefully crafted with predictability and convenience in mind, such that the following expression will always hold `true{:tact}`:
88
-
89
-
```tact
90
-
a / b * b + a % b == a; // true for any Int values of `a` and `b`,
91
-
// except when `b` is equal to 0 and we divide `a` by 0,
92
-
// which is not allowed
93
-
```
94
-
95
87
## Parentheses, `()`[#parentheses]
96
88
97
89
Parentheses (also can be called round brackets, `(){:tact}`) are more of a punctuation symbols than actual operators, but their [precedence](#precedence) is higher than precedence of any other operator. Use parentheses to override order of operations:
Binary slash (_division_) operator `/{:tact}` is used for integer division of two values, which truncates towards zero if result is positive, and away from zero if result is negative. An attempt to divide by zero would result in an error with [exit code 4](/book/exit-codes#4): `Integer overflow`.
167
+
Binary slash (_division_) operator `/{:tact}` is used for integer division of two values, which truncates towards zero if result is positive, and away from zero if result is negative. This is also called [rounding down](https://en.wikipedia.org/wiki/Rounding#Rounding_down) (or rounding towards $-∞$), where, for example, result value of $23.7$ gets rounded to $23$, and $−23.2$ gets rounded to $−24$.
168
+
169
+
An attempt to divide by zero would result in an error with [exit code 4](/book/exit-codes#4): `Integer overflow`.
176
170
177
171
Can only be applied to values of type [`Int{:tact}`][int]:
178
172
@@ -184,8 +178,22 @@ two / 1; // 0
184
178
-1 / -5; // 0
185
179
1 / -5; // -1
186
180
1 / 5; // 0
181
+
6 / 5; // 1, rounding down
182
+
-6 / 5; // -2, rounding down (towards -∞)
187
183
```
188
184
185
+
<Callout>
186
+
187
+
Because division operator `/{:tact}` in Tact rounds down, the following expression always holds `true{:tact}`:
188
+
189
+
```tact
190
+
a / b * b + a % b == a; // true for any Int values of `a` and `b`,
191
+
// except when `b` is equal to 0 and we divide `a` by 0,
192
+
// which is an attempt to divide by zero resulting in an error
193
+
```
194
+
195
+
</Callout>
196
+
189
197
#### Modulo, `%`[#binary-modulo]
190
198
191
199
Binary percent sign (_modulo_) operator `%{:tact}` is used for getting the modulo of an integer division, which must not be confused with getting a remainder. For two values of the same sign, modulo and remainder operations are equivalent, but when the operands are of different signs, the modulo result always has the same sign as the _divisor_ (value on the right), while the remainder has the same sign as the _dividend_ (value on the left), which can make them differ by one unit of the _divisor_.
@@ -463,6 +471,8 @@ two ^ 3; // 1
463
471
464
472
Binary bar (_bitwise OR_) operator `|{:tact}` applies a [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR), which performs the [logical OR](#binary-logical-or) operation on each pair of the corresponding bits of operands. This is useful when we want to apply a specific [bitmask](https://en.wikipedia.org/wiki/Mask_(computing)).
465
473
474
+
For example, _bitwise OR_ is commonly used in Tact to [combine base modes with optional flags](http://localhost:3000/book/message-mode#combining-modes-with-flags) by masking specific bits to $1$ in order to construct a target [message `mode`](/book/message-mode).
475
+
466
476
Can only be applied to values of type [`Int{:tact}`][int]:
0 commit comments