diff options
author | Louis Pilfold <louis@lpil.uk> | 2021-08-08 18:46:14 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2021-08-08 18:46:14 +0100 |
commit | 7ca5f50aff6cb19c43ac39170e829cce1a48fdb9 (patch) | |
tree | b794a97a7d839f87532fb7ece92f627840d91a9b /src | |
parent | dd8a7bacc5db670bb084e8c77b02c0c1c44f605d (diff) | |
download | gleam_stdlib-7ca5f50aff6cb19c43ac39170e829cce1a48fdb9.tar.gz gleam_stdlib-7ca5f50aff6cb19c43ac39170e829cce1a48fdb9.zip |
JS float functions
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/float.gleam | 96 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 12 |
2 files changed, 80 insertions, 28 deletions
diff --git a/src/gleam/float.gleam b/src/gleam/float.gleam index 8fdb79e..1191ac1 100644 --- a/src/gleam/float.gleam +++ b/src/gleam/float.gleam @@ -97,40 +97,80 @@ pub fn max(a: Float, b: Float) -> Float { } } +/// Rounds the value to the next highest whole number as a float. +/// +/// ## Examples +/// +/// > ceiling(2.3) +/// 3.0 +/// +pub fn ceiling(float: Float) -> Float { + do_ceiling(float) +} + if erlang { - /// Rounds the value to the next highest whole number as a float. - /// - /// ## Examples - /// - /// > ceiling(2.3) - /// 3.0 - /// - pub external fn ceiling(Float) -> Float = + external fn do_ceiling(Float) -> Float = "math" "ceil" +} - /// Rounds the value to the next lowest whole number as a float. - /// - /// ## Examples - /// - /// > floor(2.3) - /// 2.0 - /// - pub external fn floor(Float) -> Float = +if javascript { + external fn do_ceiling(Float) -> Float = + "../gleam_stdlib.js" "ceiling" +} + +/// Rounds the value to the next lowest whole number as a float. +/// +/// ## Examples +/// +/// > floor(2.3) +/// 2.0 +/// +pub fn floor(float: Float) -> Float { + do_floor(float) +} + +if erlang { + external fn do_floor(Float) -> Float = "math" "floor" +} - /// Rounds the value to the nearest whole number as an int. - /// - /// ## Examples - /// - /// > round(2.3) - /// 2 - /// - /// > round(2.5) - /// 3 - /// - pub external fn round(Float) -> Int = - "erlang" "round" +if javascript { + external fn do_floor(Float) -> Float = + "../gleam_stdlib.js" "floor" +} + +/// Rounds the value to the nearest whole number as an int. +/// +/// ## Examples +/// +/// > round(2.3) +/// 2 +/// +/// > round(2.5) +/// 3 +/// +pub fn round(float: Float) -> Int { + do_round(float) +} +if erlang { + external fn do_round(Float) -> Int = + "math" "round" +} + +if javascript { + fn do_round(float: Float) -> Int { + case float >=. 0.0 { + True -> js_round(float) + _ -> 0 - js_round(negate(float)) + } + } + + external fn js_round(Float) -> Int = + "../gleam_stdlib.js" "round" +} + +if erlang { /// Returns the value as an int, truncating all decimal digits. /// /// ## Examples diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 95fb526..8b2f131 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -224,3 +224,15 @@ export function print(string) { console.log(string); // We're in a browser. Newlines are mandated } } + +export function ceiling(float) { + return Math.ceil(float); +} + +export function floor(float) { + return Math.floor(float); +} + +export function round(float) { + return Math.round(float); +} |