diff options
author | inoas <mail@inoas.com> | 2022-11-22 13:12:01 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-11-27 11:26:43 +0000 |
commit | 8750fc3ec3eaa2f466b4bc0df8c1758d1f91f95f (patch) | |
tree | def4b5986a67eab356bf9cb8b35723f46e976916 /test | |
parent | b1b511e74ff321dff41c4606a8138b866397cdd6 (diff) | |
download | gleam_stdlib-8750fc3ec3eaa2f466b4bc0df8c1758d1f91f95f.tar.gz gleam_stdlib-8750fc3ec3eaa2f466b4bc0df8c1758d1f91f95f.zip |
add float.loosely_equals
Diffstat (limited to 'test')
-rw-r--r-- | test/gleam/float_test.gleam | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 44d9f52..a068647 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -100,6 +100,26 @@ pub fn loosely_compare_test() { |> should.equal(order.Eq) } +pub fn loosely_equals_test() { + float.loosely_equals(10.2, 10.5, tolerating: 0.) + |> should.be_false + + float.loosely_equals(10.2, with: 10.5, tolerating: 0.31) + |> should.be_true + + float.loosely_equals(10.5, 10.2, 0.31) + |> should.be_true + + float.loosely_equals(10.2, 10.5, 0.29) + |> should.be_false + + float.loosely_equals(10.5, 10.2, 0.29) + |> should.be_false + + float.loosely_equals(-10.2, -10.5, 0.31) + |> should.be_true +} + pub fn ceiling_test() { 8.1 |> float.ceiling @@ -264,7 +284,7 @@ pub fn power_test() { // float.power(-1.0, 0.5) is equivalent to float.square_root(-1.0) // and should return an error as an imaginary number would otherwise - // have to be returned + // have to be returned float.power(-1.0, 0.5) |> should.equal(Error(Nil)) @@ -277,7 +297,7 @@ pub fn power_test() { float.power(0.0, -1.0) |> should.equal(Error(Nil)) - // Check that a negative base and exponent is fine as long as the + // Check that a negative base and exponent is fine as long as the // exponent is not fractional float.power(-2.0, -1.0) |> should.equal(Ok(-0.5)) @@ -369,8 +389,8 @@ pub fn random_test() { with: fn(accumulator, _element) { accumulator +. float.random(min, max) }, ) |> fn(sum) { sum /. int.to_float(iterations) } - |> float.loosely_compare(expected_average, tolerance) - |> should.equal(order.Eq) + |> float.loosely_equals(expected_average, tolerance) + |> should.be_true } test_mean(100, 0.0, 0.0, 5.0) test_mean(1_000, 0.0, 100.0, 5.0) |