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 | |
parent | dd8a7bacc5db670bb084e8c77b02c0c1c44f605d (diff) | |
download | gleam_stdlib-7ca5f50aff6cb19c43ac39170e829cce1a48fdb9.tar.gz gleam_stdlib-7ca5f50aff6cb19c43ac39170e829cce1a48fdb9.zip |
JS float functions
-rw-r--r-- | src/gleam/float.gleam | 96 | ||||
-rw-r--r-- | src/gleam_stdlib.js | 12 | ||||
-rw-r--r-- | test/gleam/float_test.gleam | 135 |
3 files changed, 146 insertions, 97 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); +} diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam index 9eff3a3..a449f79 100644 --- a/test/gleam/float_test.gleam +++ b/test/gleam/float_test.gleam @@ -1,9 +1,6 @@ import gleam/should import gleam/float - -if erlang { - import gleam/order -} +import gleam/order pub fn parse_test() { "1.23" @@ -49,92 +46,92 @@ pub fn to_string_test() { |> should.equal("-8.1") } -if erlang { - pub fn clamp_test() { - float.clamp(1.4, min: 1.3, max: 1.5) - |> should.equal(1.4) +pub fn clamp_test() { + float.clamp(1.4, min: 1.3, max: 1.5) + |> should.equal(1.4) - float.clamp(1.2, min: 1.3, max: 1.5) - |> should.equal(1.3) + float.clamp(1.2, min: 1.3, max: 1.5) + |> should.equal(1.3) - float.clamp(1.6, min: 1.3, max: 1.5) - |> should.equal(1.5) - } + float.clamp(1.6, min: 1.3, max: 1.5) + |> should.equal(1.5) +} - pub fn compare_test() { - float.compare(0., 0.) - |> should.equal(order.Eq) +pub fn compare_test() { + float.compare(0., 0.) + |> should.equal(order.Eq) - float.compare(0.1, 0.1) - |> should.equal(order.Eq) + float.compare(0.1, 0.1) + |> should.equal(order.Eq) - float.compare(0., 0.1) - |> should.equal(order.Lt) + float.compare(0., 0.1) + |> should.equal(order.Lt) - float.compare(-2., -1.9) - |> should.equal(order.Lt) + float.compare(-2., -1.9) + |> should.equal(order.Lt) - float.compare(2., 1.9) - |> should.equal(order.Gt) + float.compare(2., 1.9) + |> should.equal(order.Gt) - float.compare(-1.9, -2.) - |> should.equal(order.Gt) - } + float.compare(-1.9, -2.) + |> should.equal(order.Gt) +} - pub fn ceiling_test() { - 8.1 - |> float.ceiling - |> should.equal(9.0) +pub fn ceiling_test() { + 8.1 + |> float.ceiling + |> should.equal(9.0) - -8.1 - |> float.ceiling - |> should.equal(-8.0) + -8.1 + |> float.ceiling + |> should.equal(-8.0) - -8.0 - |> float.ceiling - |> should.equal(-8.0) - } + -8.0 + |> float.ceiling + |> should.equal(-8.0) +} - pub fn floor_test() { - 8.1 - |> float.floor - |> should.equal(8.0) +pub fn floor_test() { + 8.1 + |> float.floor + |> should.equal(8.0) - -8.1 - |> float.floor - |> should.equal(-9.0) + -8.1 + |> float.floor + |> should.equal(-9.0) - -8.0 - |> float.floor - |> should.equal(-8.0) - } + -8.0 + |> float.floor + |> should.equal(-8.0) +} - pub fn round_test() { - 8.1 - |> float.round - |> should.equal(8) +pub fn round_test() { + 8.1 + |> float.round + |> should.equal(8) - 8.4 - |> float.round - |> should.equal(8) + 8.4 + |> float.round + |> should.equal(8) - 8.499 - |> float.round - |> should.equal(8) + 8.499 + |> float.round + |> should.equal(8) - 8.5 - |> float.round - |> should.equal(9) + 8.5 + |> float.round + |> should.equal(9) - -8.1 - |> float.round - |> should.equal(-8) + -8.1 + |> float.round + |> should.equal(-8) - -7.5 - |> float.round - |> should.equal(-8) - } + -7.5 + |> float.round + |> should.equal(-8) +} +if erlang { pub fn truncate_test() { 8.1 |> float.truncate |