diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-08 18:54:25 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-08 18:54:25 +0100 |
commit | d8407c576cc04ae2c104fb4ff5999cb5307981e1 (patch) | |
tree | 87175db5699b82b47d8f57b0a0e0d3516ea26530 /src | |
parent | 7ca5f50aff6cb19c43ac39170e829cce1a48fdb9 (diff) | |
download | gleam_stdlib-d8407c576cc04ae2c104fb4ff5999cb5307981e1.tar.gz gleam_stdlib-d8407c576cc04ae2c104fb4ff5999cb5307981e1.zip |
Remaining float functions for JS
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 120 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 8 |
2 files changed, 80 insertions, 48 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 1191ac1..0ad788c 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -170,59 +170,83 @@ if javascript { "../gleam_stdlib.js" "round" } +/// Returns the value as an int, truncating all decimal digits. +/// +/// ## Examples +/// +/// > truncate(2.4343434847383438) +/// 2 +/// +pub fn truncate(float: Float) -> Int { + do_truncate(float) +} + if erlang { - /// Returns the value as an int, truncating all decimal digits. - /// - /// ## Examples - /// - /// > truncate(2.4343434847383438) - /// 2 - /// - pub external fn truncate(Float) -> Int = - "erlang" "trunc" + external fn do_truncate(Float) -> Int = + "math" "trunc" +} - /// Returns the absolute value of the input as a float. - /// - /// ## Examples - /// - /// > absolute_value(-12.5) - /// 12.5 - /// - /// > absolute_value(10.2) - /// 10.2 - /// - pub external fn absolute_value(Float) -> Float = - "erlang" "abs" +if javascript { + external fn do_truncate(Float) -> Int = + "../gleam_stdlib.js" "truncate" +} - /// 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 = +/// Returns the absolute value of the input as a float. +/// +/// ## Examples +/// +/// > absolute_value(-12.5) +/// 12.5 +/// +/// > absolute_value(10.2) +/// 10.2 +/// +pub fn absolute_value(float: Float) -> Float { + case float >=. 0. { + True -> float + _ -> 0. -. float + } +} + +/// 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 fn power(base: Float, exponent: Float) -> Float { + do_power(base, exponent) +} + +if erlang { + external fn do_power(Float, Float) -> Float = "math" "pow" +} - /// Returns the square root of the input as a float. - /// - /// ## Examples - /// - /// > square_root(4.0) - /// Ok(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)) - } +if javascript { + external fn do_power(Float, Float) -> Float = + "../gleam_stdlib.js" "power" +} + +/// Returns the square root of the input as a float. +/// +/// ## Examples +/// +/// > square_root(4.0) +/// Ok(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)) } } diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 8b2f131..479b8a9 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -236,3 +236,11 @@ export function floor(float) { export function round(float) { return Math.round(float); } + +export function truncate(float) { + return Math.trunc(float); +} + +export function power(base, exponent) { + return Math.pow(base, exponent); +} |