Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 1d834de

Browse files
committed
fix/feat: applied suggestions from a code review
* 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
1 parent a840a80 commit 1d834de

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

pages/book/operators.mdx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,6 @@ Consider the following code:
8484

8585
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.
8686

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-
9587
## Parentheses, `()` [#parentheses]
9688

9789
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:
@@ -172,7 +164,9 @@ pow(2, 255) * pow(2, 255); // build error: integer overflow!
172164

173165
#### Divide, `/` [#binary-divide]
174166

175-
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`.
176170

177171
Can only be applied to values of type [`Int{:tact}`][int]:
178172

@@ -184,8 +178,22 @@ two / 1; // 0
184178
-1 / -5; // 0
185179
1 / -5; // -1
186180
1 / 5; // 0
181+
6 / 5; // 1, rounding down
182+
-6 / 5; // -2, rounding down (towards -∞)
187183
```
188184

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+
189197
#### Modulo, `%` [#binary-modulo]
190198

191199
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
463471

464472
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)).
465473

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+
466476
Can only be applied to values of type [`Int{:tact}`][int]:
467477

468478
```tact

0 commit comments

Comments
 (0)