Skip to content
14 changes: 7 additions & 7 deletions docs/src/content/docs/book/operators.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pow(2, 255) * pow(2, 255); // build error: integer overflow!

#### Divide, `/` {#binary-divide}

The binary slash (_division_) operator `/{:tact}` is used for integer division of two values, which truncates toward zero if the result is positive and away from zero if the result is negative. This is also called [rounding down](https://en.wikipedia.org/wiki/Rounding#Rounding_down) or rounding toward $-∞$.
The binary slash (_division_) operator `/{:tact}` is used for integer division of two values. It rounds the result towards negative infinity (also known as floor division or rounding down). This means the quotient `q` is always `floor(x / y)`. The remainder `r` (where `x = q*y + r`) will have the same sign as the divisor `y` if `r` is not zero.

An attempt to divide by zero results in an error with [exit code 4](/book/exit-codes#4): `Integer overflow`.

Expand All @@ -218,12 +218,12 @@ It can only be applied to values of type [`Int{:tact}`][int]:
let two: Int = 2;
two / 2; // 1
two / 1; // 2
-1 / 5; // -1
-1 / -5; // 0
1 / -5; // -1
1 / 5; // 0
6 / 5; // 1, rounding down
-6 / 5; // -2, rounding down (toward -∞)
-1 / 5; // -1 (floor(-0.2) = -1)
-1 / -5; // 0 (floor(0.2) = 0)
1 / -5; // -1 (floor(-0.2) = -1)
1 / 5; // 0 (floor(0.2) = 0)
6 / 5; // 1 (floor(1.2) = 1)
-6 / 5; // -2 (floor(-1.2) = -2)
```

:::note
Expand Down
10 changes: 5 additions & 5 deletions docs/src/content/docs/ref/core-math.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ sqrt(-1); // ERROR! Exit code 5: Integer out of expected range
fun divc(x: Int, y: Int): Int;
```

Computes and returns the [rounded up][round-up] result of division of the [`Int{:tact}`][int] `x` by the [`Int{:tact}`][int] `y`.
Computes and returns the result of division of the [`Int{:tact}`][int] `x` by the [`Int{:tact}`][int] `y`, using ceiling division (rounds towards positive infinity). This corresponds to `ceil(x/y)` from the TVM specification.

Attempts to divide by `y` equal to $0$ throw an exception with [exit code 4](/book/exit-codes#4): `Integer overflow`.

Expand All @@ -134,7 +134,7 @@ divc(-3, 2); // -1
fun muldivc(x: Int, y: Int, z: Int): Int;
```

Computes and returns the [rounded up][round-up] result of `(x * y) / z{:tact}`.
Computes and returns the result of `(x * y) / z{:tact}`, using ceiling division (rounds towards positive infinity). This corresponds to `ceil((x*y)/z)` from the TVM specification.

If the value in calculation goes beyond the range from $-2^{256}$ to $2^{256} - 1$ inclusive, or if there is an attempt to divide by `z` equal to $0$, an exception with [exit code 4](/book/exit-codes#4) is thrown: `Integer overflow`.

Expand All @@ -157,7 +157,7 @@ muldivc(-3, 0, 0); // ERROR! Exit code 4: Integer overflow
fun mulShiftRight(x: Int, y: Int, z: Int): Int;
```

Computes and returns the [rounded down][round-down] result of `(x * y) / 2^z{:tact}`. It is a more gas-efficient equivalent of performing the [bitwise shift right](/book/operators#binary-bitwise-shift-right) on the result of multiplication of [`Int{:tact}`][int] `x` by [`Int{:tact}`][int] `y`, where [`Int{:tact}`][int] `z` is the right operand of the shift.
Computes and returns the result of `(x * y) / 2^z{:tact}`, using floor division (rounds towards negative infinity). This corresponds to `floor((x*y)/2^z)` from the TVM specification. It is a more gas-efficient equivalent of performing the [bitwise shift right](/book/operators#binary-bitwise-shift-right) on the result of multiplication of [`Int{:tact}`][int] `x` by [`Int{:tact}`][int] `y`, where [`Int{:tact}`][int] `z` is the right operand of the shift.

If the value in calculation goes beyond the range from $-2^{256}$ to $2^{256} - 1$ inclusive, an exception with [exit code 4](/book/exit-codes#4) is thrown: `Integer overflow`.

Expand All @@ -180,7 +180,7 @@ mulShiftRight(5, 5, -1); // ERROR! Exit code 5: Integer out of expected range
fun mulShiftRightRound(x: Int, y: Int, z: Int): Int;
```

Similar to [`mulShiftRight(){:tact}`](#mulshiftright), but instead of [rounding down][round-down], the result value is rounded to the nearest integer. If there are two equally close integers, rounding is done toward the even one.
Similar to [`mulShiftRight(){:tact}`](#mulshiftright), but the result value `(x * y) / 2^z` is rounded to the nearest integer. In cases where the fractional part is exactly 0.5, it rounds towards positive infinity. This corresponds to `floor((x*y)/2^z + 1/2)` from the TVM specification.

If the value in calculation goes beyond the range from $-2^{256}$ to $2^{256} - 1$ inclusive, an exception with [exit code 4](/book/exit-codes#4) is thrown: `Integer overflow`.

Expand All @@ -203,7 +203,7 @@ mulShiftRightRound(5, 5, -1); // ERROR! Exit code 5: Integer out of expected ran
fun mulShiftRightCeil(x: Int, y: Int, z: Int): Int;
```

Similar to [`mulShiftRight(){:tact}`](#mulshiftright), but instead of [rounding down][round-down], the result value is [rounded up][round-up].
Similar to [`mulShiftRight(){:tact}`](#mulshiftright), but the result value `(x * y) / 2^z` is rounded using ceiling division (rounds towards positive infinity). This corresponds to `ceil((x*y)/2^z)` from the TVM specification.

If the value in calculation goes beyond the range from $-2^{256}$ to $2^{256} - 1$ inclusive, an exception with [exit code 4](/book/exit-codes#4) is thrown: `Integer overflow`.

Expand Down