Skip to content

Add Math.Int.ceil #218

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

Merged
merged 2 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/Core__Math.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@ function floor(f) {
return Math.floor(f) | 0;
}

function ceil(f) {
return Math.ceil(f) | 0;
}

function random(min, max) {
var f = Math.random() * (max - min | 0);
return (Math.floor(f) | 0) + min | 0;
}

var Int = {
floor: floor,
ceil: ceil,
random: random
};

Expand Down
1 change: 1 addition & 0 deletions src/Core__Math.res
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ module Int = {
@val external pow: (int, ~exp: int) => int = "Math.pow"
@val external sign: int => int = "Math.sign"
let floor: float => int = f => f->floor->Core__Float.toInt
let ceil: float => int = f => f->ceil->Core__Float.toInt
let random: (int, int) => int = (min, max) =>
floor(random() *. Core__Int.toFloat(max - min)) + min
}
18 changes: 15 additions & 3 deletions src/Core__Math.resi
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ module Int: {

/**
floor(v) returns the largest `int` less than or equal to the argument;
the result is pinned to the range of the `int` data type: -2147483648 to 2147483647.
See [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor)
on MDN.

Expand All @@ -289,12 +288,25 @@ module Int: {
Math.Int.floor(3.7) == 3
Math.Int.floor(3.0) == 3
Math.Int.floor(-3.1) == -4
Math.Int.floor(-1.0e15) == -2147483648
Math.Int.floor(1.0e15) == 2147483647
```
*/
let floor: float => int

/**
ceil(v) returns the smallest `int` greater than or equal to the argument;
See [`Math.floor`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/floor)
on MDN.

## Examples

```rescript
Math.Int.ceil(3.7) == 4
Math.Int.ceil(3.0) == 3
Math.Int.ceil(-3.1) == -3
```
*/
let ceil: float => int

/**
`random(minVal, maxVal)` returns a random integer number in the half-closed interval [minVal, maxVal).
See [`Math.random`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)
Expand Down