aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2019-04-22 22:45:06 +0000
committerLouis Pilfold <louis@lpil.uk>2019-04-22 22:45:06 +0000
commit63d1ef2e449262becd654a13d1cb1462590e203f (patch)
treeae25e58718e1ff54a6454fd181af405eff36849d
parent077aaf3468c640b84ff53fc92171292ddae55bf4 (diff)
downloadgleam_stdlib-63d1ef2e449262becd654a13d1cb1462590e203f.tar.gz
gleam_stdlib-63d1ef2e449262becd654a13d1cb1462590e203f.zip
float:ceiling, float:floor
-rw-r--r--CHANGELOG.md2
-rw-r--r--gen/src/float.erl8
-rw-r--r--gen/test/float_test.erl12
-rw-r--r--src/float.gleam4
-rw-r--r--test/float_test.gleam28
5 files changed, 51 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b3b5924..2f5b603 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,7 +9,7 @@
- `str` module gains `append` function and loses `from_int`, `parse_int`,
`from_float`, `parse_float`, and `base_from_int`.
- `int` module created with `parse`, `to_string`, and `to_base_string`.
-- `float` module created with `parse`, and `to_string`.
+- `float` module created with `ceiling`, `floor`, `parse`, and `to_string`.
## v0.1.1 - 2019-04-17
diff --git a/gen/src/float.erl b/gen/src/float.erl
index e5f843b..c290e97 100644
--- a/gen/src/float.erl
+++ b/gen/src/float.erl
@@ -1,10 +1,16 @@
-module(float).
-compile(no_auto_import).
--export([parse/1, to_string/1]).
+-export([parse/1, to_string/1, ceiling/1, floor/1]).
parse(A) ->
gleam__stdlib:parse_float(A).
to_string(F) ->
iodata:to_string(iodata:from_float(F)).
+
+ceiling(A) ->
+ math:ceil(A).
+
+floor(A) ->
+ math:floor(A).
diff --git a/gen/test/float_test.erl b/gen/test/float_test.erl
index 642e5ed..8e9da5e 100644
--- a/gen/test/float_test.erl
+++ b/gen/test/float_test.erl
@@ -1,7 +1,7 @@
-module(float_test).
-compile(no_auto_import).
--export([parse_test/0, to_string_test/0]).
+-export([parse_test/0, to_string_test/0, ceiling_test/0, floor_test/0]).
parse_test() ->
expect:equal(float:parse(<<"1.23">>), {ok, 1.23}),
@@ -14,3 +14,13 @@ parse_test() ->
to_string_test() ->
expect:equal(float:to_string(123.0), <<"123.0">>),
expect:equal(float:to_string(-8.1), <<"-8.1">>).
+
+ceiling_test() ->
+ expect:equal(float:ceiling(8.1), 9.0),
+ expect:equal(float:ceiling(-8.1), -8.0),
+ expect:equal(float:ceiling(-8.0), -8.0).
+
+floor_test() ->
+ expect:equal(float:floor(8.1), 8.0),
+ expect:equal(float:floor(-8.1), -9.0),
+ expect:equal(float:floor(-8.0), -8.0).
diff --git a/src/float.gleam b/src/float.gleam
index 2d3ca3c..cd1d43e 100644
--- a/src/float.gleam
+++ b/src/float.gleam
@@ -11,3 +11,7 @@ pub fn to_string(f) {
|> iodata:from_float
|> iodata:to_string
}
+
+pub external fn ceiling(Float) -> Float = "math" "ceil";
+
+pub external fn floor(Float) -> Float = "math" "floor";
diff --git a/test/float_test.gleam b/test/float_test.gleam
index 161f6df..54f1463 100644
--- a/test/float_test.gleam
+++ b/test/float_test.gleam
@@ -36,3 +36,31 @@ pub fn to_string_test() {
|> float:to_string
|> expect:equal(_, "-8.1")
}
+
+pub fn ceiling_test() {
+ 8.1
+ |> float:ceiling
+ |> expect:equal(_, 9.0)
+
+ -8.1
+ |> float:ceiling
+ |> expect:equal(_, -8.0)
+
+ -8.0
+ |> float:ceiling
+ |> expect:equal(_, -8.0)
+}
+
+pub fn floor_test() {
+ 8.1
+ |> float:floor
+ |> expect:equal(_, 8.0)
+
+ -8.1
+ |> float:floor
+ |> expect:equal(_, -9.0)
+
+ -8.0
+ |> float:floor
+ |> expect:equal(_, -8.0)
+}