diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/gleam/float.gleam | 11 | ||||
-rw-r--r-- | src/gleam/int.gleam | 11 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 11 | ||||
-rw-r--r-- | test/gleam/int_test.gleam | 11 |
5 files changed, 45 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e567952..e920741 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - The `list` module gains the `each`, and `partition` functions. +- The `int` and `float` modules gain the `negate` function. ## v0.11.0 - 2020-08-22 diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index ecb203c..24f0330 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -160,3 +160,14 @@ pub fn square_root(number: Float) -> Result(Float, Nil) { False -> Ok(power(number, 0.5)) } } + +/// Returns the negative of the value provided +/// +/// ## Examples +/// +/// > negate(1.) +/// -1. +/// +pub fn negate(x: Float) -> Float { + -1. *. x +} diff --git a/src/gleam/int.gleam b/src/gleam/int.gleam index 151bfa3..2dc4f46 100644 --- a/src/gleam/int.gleam +++ b/src/gleam/int.gleam @@ -121,3 +121,14 @@ pub fn is_even(x: Int) -> Bool { pub fn is_odd(x: Int) -> Bool { x % 2 != 0 } + +/// Returns the negative of the value provided +/// +/// ## Examples +/// +/// > negate(1) +/// -1 +/// +pub fn negate(x: Int) -> Int { + -1 * x +} diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 79bece2..be1f22d 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -231,3 +231,14 @@ pub fn square_root_test() { float.square_root(-4.0) |> should.equal(Error(Nil)) } + +pub fn negate_test() { + float.negate(-1.) + |> should.equal(1.) + + float.negate(2.) + |> should.equal(-2.) + + float.negate(0.) + |> should.equal(0.) +} diff --git a/test/gleam/int_test.gleam b/test/gleam/int_test.gleam index bb4648e..4a01073 100644 --- a/test/gleam/int_test.gleam +++ b/test/gleam/int_test.gleam @@ -157,3 +157,14 @@ pub fn is_odd_test() { int.is_odd(10005) |> should.be_true } + +pub fn negate_test() { + int.negate(-1) + |> should.equal(1) + + int.negate(2) + |> should.equal(-2) + + int.negate(0) + |> should.equal(0) +} |