aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-08-08 18:54:25 +0100
committerLouis Pilfold <louis@lpil.uk>2021-08-08 18:54:25 +0100
commitd8407c576cc04ae2c104fb4ff5999cb5307981e1 (patch)
tree87175db5699b82b47d8f57b0a0e0d3516ea26530
parent7ca5f50aff6cb19c43ac39170e829cce1a48fdb9 (diff)
downloadgleam_stdlib-d8407c576cc04ae2c104fb4ff5999cb5307981e1.tar.gz
gleam_stdlib-d8407c576cc04ae2c104fb4ff5999cb5307981e1.zip
Remaining float functions for JS
-rw-r--r--src/gleam/float.gleam120
-rw-r--r--src/gleam_stdlib.js8
-rw-r--r--test/gleam/float_test.gleam216
3 files changed, 187 insertions, 157 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);
+}
diff --git a/test/gleam/float_test.gleam b/test/gleam/float_test.gleam
index a449f79..c7107d9 100644
--- a/test/gleam/float_test.gleam
+++ b/test/gleam/float_test.gleam
@@ -131,154 +131,152 @@ pub fn round_test() {
|> should.equal(-8)
}
-if erlang {
- pub fn truncate_test() {
- 8.1
- |> float.truncate
- |> should.equal(8)
+pub fn truncate_test() {
+ 8.1
+ |> float.truncate
+ |> should.equal(8)
- 8.4
- |> float.truncate
- |> should.equal(8)
+ 8.4
+ |> float.truncate
+ |> should.equal(8)
- 8.499
- |> float.truncate
- |> should.equal(8)
+ 8.499
+ |> float.truncate
+ |> should.equal(8)
- 8.5
- |> float.truncate
- |> should.equal(8)
+ 8.5
+ |> float.truncate
+ |> should.equal(8)
- -8.1
- |> float.truncate
- |> should.equal(-8)
+ -8.1
+ |> float.truncate
+ |> should.equal(-8)
- -7.5
- |> float.truncate
- |> should.equal(-7)
- }
+ -7.5
+ |> float.truncate
+ |> should.equal(-7)
+}
- pub fn min_test() {
- float.min(0., 0.)
- |> should.equal(0.)
+pub fn min_test() {
+ float.min(0., 0.)
+ |> should.equal(0.)
- float.min(0.3, 1.5)
- |> should.equal(0.3)
+ float.min(0.3, 1.5)
+ |> should.equal(0.3)
- float.min(1., 0.)
- |> should.equal(0.)
+ float.min(1., 0.)
+ |> should.equal(0.)
- float.min(-1.7, 2.5)
- |> should.equal(-1.7)
+ float.min(-1.7, 2.5)
+ |> should.equal(-1.7)
- float.min(-2.2, -2.2)
- |> should.equal(-2.2)
+ float.min(-2.2, -2.2)
+ |> should.equal(-2.2)
- float.min(-1., -1.)
- |> should.equal(-1.)
+ float.min(-1., -1.)
+ |> should.equal(-1.)
- float.min(-1.1, -1.)
- |> should.equal(-1.1)
- }
+ float.min(-1.1, -1.)
+ |> should.equal(-1.1)
+}
- pub fn max_test() {
- float.max(0., 0.)
- |> should.equal(0.)
+pub fn max_test() {
+ float.max(0., 0.)
+ |> should.equal(0.)
- float.max(0.3, 1.5)
- |> should.equal(1.5)
+ float.max(0.3, 1.5)
+ |> should.equal(1.5)
- float.max(1., 0.)
- |> should.equal(1.)
+ float.max(1., 0.)
+ |> should.equal(1.)
- float.max(-1.7, 2.5)
- |> should.equal(2.5)
+ float.max(-1.7, 2.5)
+ |> should.equal(2.5)
- float.max(-2.2, -2.2)
- |> should.equal(-2.2)
+ float.max(-2.2, -2.2)
+ |> should.equal(-2.2)
- float.max(-1., -1.)
- |> should.equal(-1.)
+ float.max(-1., -1.)
+ |> should.equal(-1.)
- float.max(-1.1, -1.)
- |> should.equal(-1.)
- }
+ float.max(-1.1, -1.)
+ |> should.equal(-1.)
+}
- pub fn absolute_value_test() {
- float.absolute_value(-1.0)
- |> should.equal(1.0)
+pub fn absolute_value_test() {
+ float.absolute_value(-1.0)
+ |> should.equal(1.0)
- float.absolute_value(-20.6)
- |> should.equal(20.6)
+ float.absolute_value(-20.6)
+ |> should.equal(20.6)
- float.absolute_value(0.0)
- |> should.equal(0.0)
+ float.absolute_value(0.0)
+ |> should.equal(0.0)
- float.absolute_value(1.0)
- |> should.equal(1.0)
+ float.absolute_value(1.0)
+ |> should.equal(1.0)
- float.absolute_value(25.2)
- |> should.equal(25.2)
- }
+ float.absolute_value(25.2)
+ |> should.equal(25.2)
+}
- pub fn power_test() {
- float.power(2.0, 2.0)
- |> should.equal(4.0)
+pub fn power_test() {
+ float.power(2.0, 2.0)
+ |> should.equal(4.0)
- float.power(-5.0, 3.0)
- |> should.equal(-125.0)
+ float.power(-5.0, 3.0)
+ |> should.equal(-125.0)
- float.power(10.5, 0.0)
- |> should.equal(1.0)
+ float.power(10.5, 0.0)
+ |> should.equal(1.0)
- float.power(16.0, 0.5)
- |> should.equal(4.0)
+ float.power(16.0, 0.5)
+ |> should.equal(4.0)
- float.power(2.0, -1.0)
- |> should.equal(0.5)
- }
+ float.power(2.0, -1.0)
+ |> should.equal(0.5)
+}
- pub fn square_root_test() {
- float.square_root(4.0)
- |> should.equal(Ok(2.0))
+pub fn square_root_test() {
+ float.square_root(4.0)
+ |> should.equal(Ok(2.0))
- float.square_root(16.0)
- |> should.equal(Ok(4.0))
+ float.square_root(16.0)
+ |> should.equal(Ok(4.0))
- float.square_root(0.0)
- |> should.equal(Ok(0.0))
+ float.square_root(0.0)
+ |> should.equal(Ok(0.0))
- float.square_root(-4.0)
- |> should.equal(Error(Nil))
- }
+ float.square_root(-4.0)
+ |> should.equal(Error(Nil))
+}
- pub fn negate_test() {
- float.negate(-1.)
- |> should.equal(1.)
+pub fn negate_test() {
+ float.negate(-1.)
+ |> should.equal(1.)
- float.negate(2.)
- |> should.equal(-2.)
+ float.negate(2.)
+ |> should.equal(-2.)
- float.negate(0.)
- |> should.equal(0.)
- }
+ float.negate(0.)
+ |> should.equal(0.)
+}
- pub fn sum_test() {
- float.sum([])
- |> should.equal(0.0)
+pub fn sum_test() {
+ float.sum([])
+ |> should.equal(0.0)
- float.sum([1.0, 2.2, 3.3])
- |> should.equal(6.5)
- }
+ float.sum([1.0, 2.2, 3.3])
+ |> should.equal(6.5)
+}
- pub fn product_test() {
- float.product([])
- |> should.equal(0.)
+pub fn product_test() {
+ float.product([])
+ |> should.equal(0.)
- float.product([4.])
- |> should.equal(4.)
+ float.product([4.])
+ |> should.equal(4.)
- float.product([2.5, 3.2, 4.2])
- |> should.equal(33.6)
- }
+ float.product([2.5, 3.2, 4.2])
+ |> should.equal(33.6)
}