diff options
-rw-r--r-- | src/gleam/float.gleam | 61 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 99 |
2 files changed, 95 insertions, 65 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 2c7930f..87d232c 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -351,33 +351,62 @@ pub fn random_between(boundary_a: Float, boundary_b: Float) -> Float { // See: <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#getting_a_random_number_between_two_values> // case boundary_a, boundary_b { - a, b if a <. 0.0 && b == 0.0 -> { - io.debug("a is neg, b is 0.0") - let a = absolute_value(a) - // a1 prohibits variable 'A@1' is unbound {A, B} when (A@1 < 0.0) andalso (B@1 =:= 0.0) -> - let a = absolute_value(b) + a, b if a <. 0.0 && b == 0.0 -> + //io.debug("-a, b=0") random_uniform() *. a + a, b if a == 0.0 && b <. 0.0 -> + //io.debug("a=0, -b") + random_uniform() *. b + a, b if a <. 0.0 && b <. 0.0 && a <. b -> { + //io.debug("-a, -b, a < b") + let a1 = absolute_value(a) + let b1 = absolute_value(b) + random_uniform() *. { b1 -. a1 } +. b1 |> negate() } - a, b if a == 0.0 && b <. 0.0 -> { - io.debug("a 0.0, b is neg") + a, b if a <. 0.0 && b <. 0.0 && a >. b -> { + //io.debug("-a, -b, a > b") let a1 = absolute_value(a) let b1 = absolute_value(b) random_uniform() *. { a1 -. b1 } +. a1 |> negate() } - a, b if a <. b -> { - io.debug("a is smaller than b") - random_uniform() *. { b -. a } +. b + a, b if a >=. 0.0 && a <. b -> + //io.debug("+a < +b") + random_uniform() *. { b -. a } +. a + a, b if a >=. 0.0 && a >. b -> + //io.debug("+a > +b") + random_uniform() *. { a -. b } +. b + a, b if a <. 0.0 && b >. 0.0 -> { + //io.debug("a < 0.0, b > 0.0") + let range = + absolute_value(a) -. absolute_value(b) + |> absolute_value() + let offset = + absolute_value(a) + |> negate() + random_uniform() *. range +. offset } - a, b if a >. b -> { - io.debug("b is smaller than a") - random_uniform() *. { a -. b } +. a + a, b if a >. 0.0 && b <. 0.0 -> { + //io.debug("a > 0.0, b < 0.0") + let range = + absolute_value(a) -. absolute_value(b) + |> absolute_value() + let offset = + absolute_value(b) + |> negate() + random_uniform() *. range +. offset } - a, b if a == b -> a + a, b if a == b -> + //io.debug("a == b noop") + a } } -pub fn random_to(exclusive_boundary: Float) -> Float { - random_uniform() *. exclusive_boundary +/// If 0.0 it will yield `0.0`. +/// If negative, it will yield `-x < 0 ` +/// If positive, it will yield `0 < x ` +/// +pub fn random_to(boundary: Float) -> Float { + random_uniform() *. boundary } diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 39fa9a8..65878bf 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -327,54 +327,55 @@ pub fn random_uniform_test() { } pub fn random_between_test() { - // let one_random_between_test_set = fn(_acc, _e) { - // float.random_between(0.0, 0.0) - // |> should.equal(0.0) - // } - float.random_between(-1.0, 0.0) - |> function.tap(io.debug) - // |> function.tap(fn(x) { - // fn(x) { x >=. -1.0 }(x) - // |> should.be_true - // }) - |> function.tap(fn(x) { - fn(x) { x <. 0.0 }(x) + let one_random_between_test_set = fn(_acc, _e) { + float.random_between(0.0, 0.0) + |> should.equal(0.0) + + float.random_between(0.0, 1.0) + |> fn(x) { x >=. 0.0 && x <. 1.0 } + |> should.be_true + + float.random_between(0.0, -1.0) + |> fn(x) { x >=. -1.0 && x <. 0.0 } + |> should.be_true + + float.random_between(-1.0, 0.0) + |> fn(x) { x >=. -1.0 && x <. 0.0 } + |> should.be_true + + float.random_between(0.0, -2.0) + |> fn(x) { x >=. -2.0 && x <. 0.0 } |> should.be_true - }) - // float.random_between(0.0, -1.0) - // |> fn(x) { x >=. -1.0 && x <. 0.0 } - // |> should.be_true - // float.random_between(-1.0, 1.0) - // |> fn(x) { x >=. -1.0 && x <. 1.0 } - // |> should.be_true - // float.random_between(1.0, -1.0) - // |> fn(x) { x >=. -1.0 && x <. 1.0 } - // |> should.be_true - // float.random_between(-1.0, 2.0) - // |> fn(x) { x >=. -1.0 && x <. 2.0 } - // |> should.be_true - // float.random_between(2.0, -1.0) - // |> fn(x) { x >=. -1.0 && x <. 2.0 } - // |> should.be_true - // list.range(0, 1) - // |> iterator.from_list - // |> iterator.fold(Nil, one_random_between_test_set) + + float.random_between(-2.0, 0.0) + |> fn(x) { x >=. -2.0 && x <. 0.0 } + |> should.be_true + } + + list.range(0, 100) + |> iterator.from_list + |> iterator.fold(Nil, one_random_between_test_set) +} + +pub fn random_to_test() { + let one_random_to_test_set = fn(_acc, _e) { + float.random_to(0.0) + |> should.equal(0.0) + + float.random_to(-1.0) + |> fn(x) { x >=. -1.0 && x <. 0.0 } + |> should.be_true + + float.random_to(1.0) + |> fn(x) { x >=. 0.0 && x <. 1.0 } + |> should.be_true + + float.random_to(2.0) + |> fn(x) { x >=. 0.0 && x <. 2.0 } + |> should.be_true + } + + list.range(0, 100) + |> iterator.from_list + |> iterator.fold(Nil, one_random_to_test_set) } -// pub fn random_to_test() { -// let one_random_to_test_set = fn(_acc, _e) { -// float.random_to(0.0) -// |> should.equal(0.0) -// float.random_to(-1.0) -// |> fn(x) { x >=. -1.0 && x <. 0.0 } -// |> should.be_true -// float.random_to(1.0) -// |> fn(x) { x >=. 0.0 && x <. 1.0 } -// |> should.be_true -// float.random_to(2.0) -// |> fn(x) { x >=. 0.0 && x <. 2.0 } -// |> should.be_true -// } -// list.range(0, 100) -// |> iterator.from_list -// |> iterator.fold(Nil, one_random_to_test_set) -// } |