aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSebastian <s@porto5.com>2022-02-18 14:51:41 +1100
committerLouis Pilfold <louis@lpil.uk>2022-02-22 13:19:08 +0000
commit34ce79f8e896e23348d88b5bfe0c3b7a28d273d8 (patch)
treecbbc5694d913712da461eee38b3b864900832a4f /src
parent813f11c00795db6036362e9392300aec13823d34 (diff)
downloadgleam_stdlib-34ce79f8e896e23348d88b5bfe0c3b7a28d273d8.tar.gz
gleam_stdlib-34ce79f8e896e23348d88b5bfe0c3b7a28d273d8.zip
Add float.compare_with_tolerance
Diffstat (limited to 'src')
-rw-r--r--src/gleam/float.gleam20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam
index 3f5399e..2e24f0b 100644
--- a/src/gleam/float.gleam
+++ b/src/gleam/float.gleam
@@ -69,6 +69,26 @@ pub fn compare(a: Float, with b: Float) -> Order {
}
}
+/// Compares two `Float`s within a tolerance.
+/// Keep in mind that as this are floats the tolerance won't be exact
+/// e.g. 5.3 - 5.0 is not exactly 0.3 in a float
+///
+/// ## Examples
+/// > loosely_compare(5.0, with: 5.3, tolerating: 0.5)
+/// Eq
+///
+pub fn loosely_compare(
+ a: Float,
+ with b: Float,
+ tolerating tolerance: Float,
+) -> Order {
+ let diff = absolute_value(a -. b)
+ case diff <=. tolerance {
+ True -> order.Eq
+ False -> compare(a, b)
+ }
+}
+
/// Compares two `Float`s, returning the smaller of the two.
///
/// ## Examples