diff --git a/CHANGELOG.md b/CHANGELOG.md index fc125acd..23dc8533 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,14 @@ ## Next version +### API changes + +- Add `Math.Int.floor` and `Math.Int.random`, https://github.com/rescript-association/rescript-core/pull/156 + +### Documentation + +- Docstrings for `Math.Int.floor` and `Math.Int.random`. https://github.com/rescript-association/rescript-core/pull/156 + ## 0.5.0 ### API changes @@ -96,4 +104,4 @@ - Docstrings for `Date`. https://github.com/rescript-association/rescript-core/pull/61 - Docstrings for `Float`. https://github.com/rescript-association/rescript-core/pull/54 - Docstrings for `String`. https://github.com/rescript-association/rescript-core/pull/27 -- Docstrings from `Array`. https://github.com/rescript-association/rescript-core/pull/78 +- Docstrings for `Array`. https://github.com/rescript-association/rescript-core/pull/78 diff --git a/src/Core__Math.mjs b/src/Core__Math.mjs index 87e83ffa..7f46c580 100644 --- a/src/Core__Math.mjs +++ b/src/Core__Math.mjs @@ -3,7 +3,19 @@ var Constants = {}; -var Int = {}; +function floor(f) { + return Math.floor(f) | 0; +} + +function random(min, max) { + var f = Math.random() * (max - min | 0); + return (Math.floor(f) | 0) + min | 0; +} + +var Int = { + floor: floor, + random: random +}; export { Constants , diff --git a/src/Core__Math.res b/src/Core__Math.res index a468332b..38ddc153 100644 --- a/src/Core__Math.res +++ b/src/Core__Math.res @@ -9,18 +9,6 @@ module Constants = { @val external sqrt2: float = "Math.SQRT2" } -module Int = { - @val external abs: int => int = "Math.abs" - @val external clz32: int => int = "Math.clz32" - @val external imul: (int, int) => int = "Math.imul" - @val external min: (int, int) => int = "Math.min" - @variadic @val external minMany: array => int = "Math.min" - @val external max: (int, int) => int = "Math.max" - @variadic @val external maxMany: array => int = "Math.max" - @val external pow: (int, ~exp: int) => int = "Math.pow" - @val external sign: int => int = "Math.sign" -} - @val external abs: float => float = "Math.abs" @val external acos: float => float = "Math.acos" @val external acosh: float => float = "Math.acosh" @@ -57,3 +45,18 @@ module Int = { @val external tan: float => float = "Math.tan" @val external tanh: float => float = "Math.tanh" @val external trunc: float => float = "Math.trunc" + +module Int = { + @val external abs: int => int = "Math.abs" + @val external clz32: int => int = "Math.clz32" + @val external imul: (int, int) => int = "Math.imul" + @val external min: (int, int) => int = "Math.min" + @variadic @val external minMany: array => int = "Math.min" + @val external max: (int, int) => int = "Math.max" + @variadic @val external maxMany: array => int = "Math.max" + @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 random: (int, int) => int = (min, max) => + floor(random() *. Core__Int.toFloat(max - min)) + min +} diff --git a/src/Core__Math.resi b/src/Core__Math.resi index eaecfb46..a9f39643 100644 --- a/src/Core__Math.resi +++ b/src/Core__Math.resi @@ -276,6 +276,39 @@ module Int: { */ @val external sign: int => int = "Math.sign" + + /** + 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. + + ## Examples + + ```rescript + 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 + + /** + `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) + on MDN. + + ## Examples + + ```rescript + Math.Int.random(2, 5) == 4 + Math.Int.random(505, 2000) == 1276 + Math.Int.random(-7, -2) == -4 + ``` + */ + let random: (int, int) => int } /**