diff --git a/docs/src/content/docs/book/expressions.mdx b/docs/src/content/docs/book/expressions.mdx index f9a61eb86..2777b78e0 100644 --- a/docs/src/content/docs/book/expressions.mdx +++ b/docs/src/content/docs/book/expressions.mdx @@ -246,7 +246,7 @@ contract ExampleContract { ## `initOf` -Expression `initOf{:tact}` computes initial state (`StateInit{:tact}`) of a [contract](/book/contracts): +Expression `initOf{:tact}` computes initial state, i.e. `StateInit{:tact}` of a [contract](/book/contracts): ```tact // argument values for the init() function of the contract @@ -263,13 +263,32 @@ initOf ExampleContract( ); ``` -Where `StateInit{:tact}` is a built-in [Struct][s], that consists of: +The `StateInit{:tact}` is a built-in [Struct][s], that consists of: Field | Type | Description :----- | :----------------- | :---------- `code` | [`Cell{:tact}`][cell] | initial code of the [contract](/book/contracts) (the compiled bytecode) `data` | [`Cell{:tact}`][cell] | initial data of the [contract](/book/contracts) (arguments of `init(){:tact}` function of the contract) +:::note + + For workchain $0$, the [`Address{:tact}`][p] of the current contract obtained by calling the [`myAddress(){:tact}`](/ref/core-common#myaddress) function is identical to the one that can be obtained by calling the [`contractAddress(){:tact}`](/ref/core-common#contractaddress) function with the initial state of the current contract computed via `initOf{:tact}`: + + ```tact {6} + contract TheKingPrawn { + receive("keeping the address") { + let myAddr1 = myAddress(); + let myAddr2 = contractAddress(initOf TheKingPrawn()); + + myAddr1 == myAddr2; // true + } + } + ``` + + However, if you only need the address of the current contract at runtime and not its `StateInit{:tact}`, use the [`myAddress(){:tact}`](/ref/core-common#myaddress) function, as it consumes **significantly** less gas. + +::: + [p]: /book/types#primitive-types [cell]: /book/cells#cells [s]: /book/structs-and-messages#structs diff --git a/docs/src/content/docs/book/operators.mdx b/docs/src/content/docs/book/operators.mdx index 08ffc3f9a..eec51e5b7 100644 --- a/docs/src/content/docs/book/operators.mdx +++ b/docs/src/content/docs/book/operators.mdx @@ -220,7 +220,7 @@ two / 1; // 2 :::note - Note that the following relationship between the division and modulo operators always holds for `Int{:tact}` type: + The following relationship between the division and [modulo](#binary-modulo) operators always holds for `Int{:tact}` type: ```tact a / b * b + a % b == a; // true for any Int values of `a` and `b`, @@ -247,12 +247,24 @@ two % 1; // 1 -1 % -5; // -1 ``` -The simplest way to avoid confusion between the two is to prefer using positive values via [`abs(x: Int){:tact}`](/ref/core-math#abs): +The simplest way to avoid confusing getting the modulo with getting the remainder is to [use only unsigned integers](/book/security-best-practices#misuse-of-signed-integers). Alternatively, consider using [`abs(){:tact}`](/ref/core-math#abs) function to ensure non-negative values: ```tact abs(-1) % abs(-5); // 1 ``` +:::note + + The following relationship between the [division](#binary-divide) and modulo operators always holds for `Int{:tact}` type: + + ```tact + a / b * b + a % b == a; // true for any Int values of `a` and `b`, + // except when `b` is equal to 0 and we divide `a` by 0, + // which is an attempt to divide by zero resulting in an error + ``` + +::: + :::note Did you know, that in JavaScript `%{:tact}` works as a _remainder_ operator, but not _modulo_ operator (like in Tact)?\