Skip to content

Update 0001-int.md #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: int-semantic
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 9 additions & 51 deletions text/0001-int.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$

Expand All @@ -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`
Expand All @@ -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

Expand Down