diff options
author | Connor Schembor <cschembor96@gmail.com> | 2020-08-25 20:29:57 -0400 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-08-27 13:55:55 +0100 |
commit | 41d2256cb18cfb657ab6a0eaccb4f315e390b618 (patch) | |
tree | f11eb8ad3a932f03168557a97ff56d56e5eae3e7 | |
parent | e9817a0f6dd9b77c048b0a702e6b897dd95bebf8 (diff) | |
download | gleam_stdlib-41d2256cb18cfb657ab6a0eaccb4f315e390b618.tar.gz gleam_stdlib-41d2256cb18cfb657ab6a0eaccb4f315e390b618.zip |
Add power function for floats
This adds a function for evaluating exponents for floats. Calls the erlang math module function
-rw-r--r-- | src/gleam/float.gleam | 14 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 17 |
2 files changed, 31 insertions, 0 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index c85903f..3767113 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -128,3 +128,17 @@ pub external fn truncate(Float) -> Int = /// pub external fn absolute_value(Float) -> Float = "erlang" "abs" + +/// Returns the results of the base being raised to the power of the +/// exponent, as a float. +/// +/// ## Examples +/// +/// > power(2.0, 2.0) +/// 4.0 +/// +/// > power(8.0, 1.5) +/// 64.0 +/// +pub external fn power(base: Float, exponent: Float) -> Float = + "math" "pow" diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 5508a76..234df59 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -200,3 +200,20 @@ pub fn absolute_value_test() { float.absolute_value(25.2) |> should.equal(25.2) } + +pub fn power_test() { + float.power(2.0, 2.0) + |> should.equal(4.0) + + float.power(-5.0, 3.0) + |> should.equal(-125.0) + + float.power(10.5, 0.0) + |> should.equal(1.0) + + float.power(16.0, 0.5) + |> should.equal(4.0) + + float.power(2.0, -1.0) + |> should.equal(0.5) +} |