diff --git a/__tests__/Functions__Test.re b/__tests__/Functions__Test.re new file mode 100644 index 00000000..874a143d --- /dev/null +++ b/__tests__/Functions__Test.re @@ -0,0 +1,44 @@ +open Jest; +open Expect; + +exception ShapeWrong(string); +describe("Functions", () => { + test("interpolate", () => { + let a = Functions.interpolate(10., 20., 1., 2., 15.); + let b = 1.5; + expect(a) |> toEqual(b); + }); + test("range#1", () => { + expect(Functions.range(1., 5., 3)) |> toEqual([|1., 3., 5.|]) + }); + test("range#2", () => { + expect(Functions.range(1., 5., 5)) |> toEqual([|1., 2., 3., 4., 5.|]) + }); + test("range#3", () => { + expect(Functions.range(-10., 15., 2)) |> toEqual([|(-10.), 15.|]) + }); + test("range#4", () => { + expect(Functions.range(-10., 15., 3)) |> toEqual([|(-10.), 2.5, 15.|]) + }); + test("range#5", () => { + expect(Functions.range(-10.3, 17., 3)) + |> toEqual([|(-10.3), 3.3499999999999996, 17.|]) + }); + test("range#6", () => { + expect(Functions.range(-10.3, 17., 5)) + |> toEqual([| + (-10.3), + (-3.4750000000000005), + 3.3499999999999996, + 10.175, + 17.0, + |]) + }); + test("range#7", () => { + expect(Functions.range(-10.3, 17.31, 3)) + |> toEqual([|(-10.3), 3.504999999999999, 17.31|]) + }); + test("range#8", () => { + expect(Functions.range(1., 1., 3)) |> toEqual([|1., 1., 1.|]) + }); +}); diff --git a/src/utility/lib/Functions.re b/src/utility/lib/Functions.re index a887be60..a2e4ba66 100644 --- a/src/utility/lib/Functions.re +++ b/src/utility/lib/Functions.re @@ -24,7 +24,8 @@ let range = (min: float, max: float, n: int): array(float) => { | _ when n < 0 => raise(RangeWrong("n is less then zero")) | _ when min > max => raise(RangeWrong("Min values is less then max")) | _ => - let diff = max -. min; - Belt.Array.makeBy(n, i => float_of_int(i) *. diff); + let diff = (max -. min) /. Belt.Float.fromInt(n - 1); + Belt.Array.makeBy(n, i => {min +. Belt.Float.fromInt(i) *. diff}); }; }; +let random = Js.Math.random_int; diff --git a/src/utility/lib/functions.js b/src/utility/lib/functions.js index a4353e52..c8c6fbfa 100644 --- a/src/utility/lib/functions.js +++ b/src/utility/lib/functions.js @@ -100,6 +100,7 @@ function max(arr) { } /** + * @Done * @param {number} min * @param {number} max * @return {number}