Skip to content

Commit

Permalink
Add Math.Int.ceil (#218)
Browse files Browse the repository at this point in the history
* ✨ - Add ceil to Math.Int

* 📝 - Fix incorrect documentation of Math.floor
  • Loading branch information
jfrolich committed Jun 5, 2024
1 parent 0cc3d8e commit 98e30dd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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

0 comments on commit 98e30dd

Please sign in to comment.