From 66653cae1a57435140b591683f3de429b3e87c3e Mon Sep 17 00:00:00 2001 From: Connor Schembor Date: Wed, 26 Aug 2020 19:38:50 -0400 Subject: Add square_root function for floats --- src/gleam/float.gleam | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src') 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)) + } +} -- cgit v1.2.3