From 26de35df75a7942b8964c1df70d03ce15741f2df Mon Sep 17 00:00:00 2001 From: Adhitya Suresh Date: Thu, 19 Oct 2023 11:42:28 +0530 Subject: [PATCH 1/6] Added random_int to Core__Math --- src/Core__Math.mjs | 5 +++++ src/Core__Math.res | 3 +++ src/Core__Math.resi | 2 ++ 3 files changed, 10 insertions(+) diff --git a/src/Core__Math.mjs b/src/Core__Math.mjs index 87e83ffa..81f4b536 100644 --- a/src/Core__Math.mjs +++ b/src/Core__Math.mjs @@ -5,8 +5,13 @@ var Constants = {}; var Int = {}; +function random_int(min, max) { + return (Math.floor(Math.random() * (max - min | 0)) | 0) + min | 0; +} + export { Constants , Int , + random_int , } /* No side effect */ diff --git a/src/Core__Math.res b/src/Core__Math.res index a468332b..f466e886 100644 --- a/src/Core__Math.res +++ b/src/Core__Math.res @@ -57,3 +57,6 @@ module Int = { @val external tan: float => float = "Math.tan" @val external tanh: float => float = "Math.tanh" @val external trunc: float => float = "Math.trunc" + +let random_int: (int, int) => int = (min, max) => + floor(random() *. Core__Int.toFloat(max - min))->Core__Float.toInt + min diff --git a/src/Core__Math.resi b/src/Core__Math.resi index eaecfb46..37a32807 100644 --- a/src/Core__Math.resi +++ b/src/Core__Math.resi @@ -845,3 +845,5 @@ Math.trunc(42.84) // 42.0 */ @val external trunc: float => float = "Math.trunc" + +let random_int: (int, int) => int From a27ea25d122f79dd0d2b3588b6f0f90b774595d2 Mon Sep 17 00:00:00 2001 From: Adhitya Suresh Date: Thu, 19 Oct 2023 11:53:31 +0530 Subject: [PATCH 2/6] added floor_int to Core__Math --- src/Core__Math.mjs | 8 +++++++- src/Core__Math.res | 4 +++- src/Core__Math.resi | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Core__Math.mjs b/src/Core__Math.mjs index 81f4b536..0c850a73 100644 --- a/src/Core__Math.mjs +++ b/src/Core__Math.mjs @@ -5,13 +5,19 @@ var Constants = {}; var Int = {}; +function floor_int(f) { + return Math.floor(f) | 0; +} + function random_int(min, max) { - return (Math.floor(Math.random() * (max - min | 0)) | 0) + min | 0; + var f = Math.random() * (max - min | 0); + return (Math.floor(f) | 0) + min | 0; } export { Constants , Int , + floor_int , random_int , } /* No side effect */ diff --git a/src/Core__Math.res b/src/Core__Math.res index f466e886..816d2fbe 100644 --- a/src/Core__Math.res +++ b/src/Core__Math.res @@ -58,5 +58,7 @@ module Int = { @val external tanh: float => float = "Math.tanh" @val external trunc: float => float = "Math.trunc" +let floor_int: float => int = f => f->floor->Core__Float.toInt + let random_int: (int, int) => int = (min, max) => - floor(random() *. Core__Int.toFloat(max - min))->Core__Float.toInt + min + floor_int(random() *. Core__Int.toFloat(max - min)) + min diff --git a/src/Core__Math.resi b/src/Core__Math.resi index 37a32807..3a91af15 100644 --- a/src/Core__Math.resi +++ b/src/Core__Math.resi @@ -846,4 +846,6 @@ Math.trunc(42.84) // 42.0 @val external trunc: float => float = "Math.trunc" +let floor_int: float => int + let random_int: (int, int) => int From f56ba87547a45827641a460ebae5a1763ed9ffc6 Mon Sep 17 00:00:00 2001 From: Adhitya Suresh Date: Fri, 20 Oct 2023 18:28:46 +0530 Subject: [PATCH 3/6] Moved random and floor int return types into Int module --- src/Core__Math.mjs | 13 +++++++------ src/Core__Math.res | 30 ++++++++++++++---------------- src/Core__Math.resi | 7 +++---- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/Core__Math.mjs b/src/Core__Math.mjs index 0c850a73..7f46c580 100644 --- a/src/Core__Math.mjs +++ b/src/Core__Math.mjs @@ -3,21 +3,22 @@ var Constants = {}; -var Int = {}; - -function floor_int(f) { +function floor(f) { return Math.floor(f) | 0; } -function random_int(min, max) { +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 , Int , - floor_int , - random_int , } /* No side effect */ diff --git a/src/Core__Math.res b/src/Core__Math.res index 816d2fbe..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" @@ -58,7 +46,17 @@ module Int = { @val external tanh: float => float = "Math.tanh" @val external trunc: float => float = "Math.trunc" -let floor_int: float => int = f => f->floor->Core__Float.toInt - -let random_int: (int, int) => int = (min, max) => - floor_int(random() *. Core__Int.toFloat(max - min)) + min +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 3a91af15..b69f476f 100644 --- a/src/Core__Math.resi +++ b/src/Core__Math.resi @@ -276,6 +276,9 @@ module Int: { */ @val external sign: int => int = "Math.sign" + + let floor: float => int + let random: (int, int) => int } /** @@ -845,7 +848,3 @@ Math.trunc(42.84) // 42.0 */ @val external trunc: float => float = "Math.trunc" - -let floor_int: float => int - -let random_int: (int, int) => int From ff2f99a85157cd3577c7f593d3a0b4d29e9b2f2c Mon Sep 17 00:00:00 2001 From: Adhitya Suresh Date: Mon, 23 Oct 2023 11:25:39 +0530 Subject: [PATCH 4/6] doc strings and examples added --- src/Core__Math.resi | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/Core__Math.resi b/src/Core__Math.resi index b69f476f..a9f39643 100644 --- a/src/Core__Math.resi +++ b/src/Core__Math.resi @@ -277,7 +277,37 @@ 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 } From 1268d47cc3b3a20e7b20cb4705f0e19a5547078f Mon Sep 17 00:00:00 2001 From: Adhitya Suresh Date: Thu, 26 Oct 2023 09:38:30 +0530 Subject: [PATCH 5/6] Added changelog --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc125acd..ae4b535f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ - Remove `Array.fromIterator` and `Array.fromIteratorWithMap`. The same functions exist in `Iterator` as `Iterator.fromArray` and `Iterator.fromArrayWithMapper`. https://github.com/rescript-association/rescript-core/pull/78 - Remove unsafe `Array.from` and `Array.fromWithMap`. https://github.com/rescript-association/rescript-core/pull/78 - Add `Int.clamp` and `Float.clamp`, https://github.com/rescript-association/rescript-core/pull/90 +- Add `Math.Int.floor` and `Math.Int.random`, https://github.com/rescript-association/rescript-core/pull/156 ### Documentation @@ -96,4 +97,5 @@ - 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 +- Docstrings for `Math.Int.floor` and `Math.Int.random`. https://github.com/rescript-association/rescript-core/pull/156 From 79f20ebdc5695d27c6f14cf6ac1e9335ef429aa1 Mon Sep 17 00:00:00 2001 From: Adhitya Suresh Date: Fri, 27 Oct 2023 13:19:12 +0530 Subject: [PATCH 6/6] Minor changes --- CHANGELOG.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ae4b535f..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 @@ -82,7 +90,6 @@ - Remove `Array.fromIterator` and `Array.fromIteratorWithMap`. The same functions exist in `Iterator` as `Iterator.fromArray` and `Iterator.fromArrayWithMapper`. https://github.com/rescript-association/rescript-core/pull/78 - Remove unsafe `Array.from` and `Array.fromWithMap`. https://github.com/rescript-association/rescript-core/pull/78 - Add `Int.clamp` and `Float.clamp`, https://github.com/rescript-association/rescript-core/pull/90 -- Add `Math.Int.floor` and `Math.Int.random`, https://github.com/rescript-association/rescript-core/pull/156 ### Documentation @@ -98,4 +105,3 @@ - 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 for `Array`. https://github.com/rescript-association/rescript-core/pull/78 -- Docstrings for `Math.Int.floor` and `Math.Int.random`. https://github.com/rescript-association/rescript-core/pull/156