diff options
author | Connor Schembor <cschembor96@gmail.com> | 2020-08-26 19:38:50 -0400 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-08-27 13:55:55 +0100 |
commit | 66653cae1a57435140b591683f3de429b3e87c3e (patch) | |
tree | dcfedeb9dc4f8893811ebc4064a2e9ce1468da7f /src | |
parent | 41d2256cb18cfb657ab6a0eaccb4f315e390b618 (diff) | |
download | gleam_stdlib-66653cae1a57435140b591683f3de429b3e87c3e.tar.gz gleam_stdlib-66653cae1a57435140b591683f3de429b3e87c3e.zip |
Add square_root function for floats
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 3767113..448424a 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -142,3 +142,20 @@ pub external fn absolute_value(Float) -> Float = /// pub external fn power(base: Float, exponent: Float) -> Float = "math" "pow" + +/// Returns the square root of the input as a float. +/// +/// ## Examples +/// +/// > square_root(4.0) +/// 2.0 +/// +/// > square_root(-16.0) +/// Error(Nil) +/// +pub fn square_root(number: Float) -> Result(Float, Nil) { + case number <. 0.0 { + True -> Error(Nil) + False -> Ok(power(number, 0.5)) + } +} |