diff --git a/text/0001-int.md b/text/0001-int.md index 8e2393d..15d1689 100644 --- a/text/0001-int.md +++ b/text/0001-int.md @@ -37,7 +37,7 @@ The valid range of an integer literal is limited to the range of signed 32-bit i Using unbounded numbers in literals may result in compile-time errors with messages such as `"Integer literal exceeds the range of representable integers of type int."` -## Primitives +## Min and Max values Let `min_value` be $-2^{31}$ and `max_value` be $2^{31}-1$ @@ -47,10 +47,6 @@ Let `min_value` be $-2^{31}$ and `max_value` be $2^{31}-1$ The [`ToInt32`] behavior follows the definition in ECMA-262 as is. ReScript compiler uses `bitwiseOR(number, 0)` in action. This is what appears in the output as `number | 0`, which truncates all special numbers defined in IEEE-754. -The `fromNumber` shouldn't be directly exposed to the users. Applying the [`ToInt32`] operation to special numeric values, such as `Infinity`, can lead to subtle bugs[^1][^2][^3]. - -Instead, public APIs should wrap it and perform bounds-checking, if necessary, either emit errors (explained further in the ["API Consideration"](#api-consideration) section below) or notify the user via compiler warning. - `int` never contains the following values: - `-0` @@ -63,76 +59,38 @@ Instead, public APIs should wrap it and perform bounds-checking, if necessary, e ### `minus: (x: int) => int` -1. Let `number` be mathematically $-x$. -2. Let `int32` be `fromNumber(number)`, return `int32`. +- return `-x | 0`. ### `add: (x: int, y: int) => int` -1. Let `number` be mathematically $x + y$. -2. Let `int32` be `fromNumber(number)`, return `int32`. +- return `(x + y) | 0`. ### `subtract: (x: int, y: int) => int` -1. Let `number` be mathematically $x - y$. -2. Let `int32` be `fromNumber(number)`, return `int32`. +- return `(x - y) | 0`. ### `multiply: (x: int, y: int) => int` -1. Let `number` be mathematically $x * y$. -2. Let `int32` be `fromNumber(number)`, return `int32`. - -The `multiply(x, y)` must produce the same result as `add(x)` accumulated `y` times. - -```res -let multiply = (x, y) => { - let id = 0 - let rec multiply = (x, y, acc) => { - switch y { - | 0 => acc - | n => multiply(x, n - 1, add(x, acc)) - } - } - multiply(x, y, id) -} -``` +- return `(x * y) | 0`. ### `exponentiate: (x: int, y: int) => int` -1. Let `number` be mathematically $x ^ y$. -2. Let `int32` be `fromNumber(number)`, return `int32`. - -The `exponentiate(x, y)` must produce the same result as `multiply(x)` accumulated `y` times. - -```res -let exponentiate = (x, y) => { - let id = 1 - let rec exponentiate = (x, y, acc) => { - switch y { - | 0 => acc - | n => exponentiate(x, n - 1, multiply(x, acc)) - } - } - exponentiate(x, y, id) -} -``` +- return `(x ** y) | 0`. ### `divide: (x: int, y: int) => int` 1. If `y` equals `0`, raise `Divide_by_zero`. -2. Let `number` be mathematically $x / y$. -3. Let `int32` be `fromNumber(number)`, return `int32`. +2. Else return $(x / y) | 0$. ### `remainder: (x: int, y: int) => int` 1. If `y` equals `0`, raise `Divide_by_zero`. -2. Let `number` be mathematically $x \mod y$. -3. Let `int32` be `fromNumber(number)`, return `int32`. +2. Else return $x % y$. ### `abs: (x: int) => int` 1. If `x` is `min_value`, raise `Overflow_value`. -2. If `x` is less than `0`, return `minus(x)`. -3. return `x`. +2. Else return `-x` ## API consideration