Skip to content

Commit 63d1ef2

Browse files
committed
float:ceiling, float:floor
1 parent 077aaf3 commit 63d1ef2

File tree

5 files changed

+51
-3
lines changed

5 files changed

+51
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- `str` module gains `append` function and loses `from_int`, `parse_int`,
1010
`from_float`, `parse_float`, and `base_from_int`.
1111
- `int` module created with `parse`, `to_string`, and `to_base_string`.
12-
- `float` module created with `parse`, and `to_string`.
12+
- `float` module created with `ceiling`, `floor`, `parse`, and `to_string`.
1313

1414
## v0.1.1 - 2019-04-17
1515

gen/src/float.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
-module(float).
22
-compile(no_auto_import).
33

4-
-export([parse/1, to_string/1]).
4+
-export([parse/1, to_string/1, ceiling/1, floor/1]).
55

66
parse(A) ->
77
gleam__stdlib:parse_float(A).
88

99
to_string(F) ->
1010
iodata:to_string(iodata:from_float(F)).
11+
12+
ceiling(A) ->
13+
math:ceil(A).
14+
15+
floor(A) ->
16+
math:floor(A).

gen/test/float_test.erl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(float_test).
22
-compile(no_auto_import).
33

4-
-export([parse_test/0, to_string_test/0]).
4+
-export([parse_test/0, to_string_test/0, ceiling_test/0, floor_test/0]).
55

66
parse_test() ->
77
expect:equal(float:parse(<<"1.23">>), {ok, 1.23}),
@@ -14,3 +14,13 @@ parse_test() ->
1414
to_string_test() ->
1515
expect:equal(float:to_string(123.0), <<"123.0">>),
1616
expect:equal(float:to_string(-8.1), <<"-8.1">>).
17+
18+
ceiling_test() ->
19+
expect:equal(float:ceiling(8.1), 9.0),
20+
expect:equal(float:ceiling(-8.1), -8.0),
21+
expect:equal(float:ceiling(-8.0), -8.0).
22+
23+
floor_test() ->
24+
expect:equal(float:floor(8.1), 8.0),
25+
expect:equal(float:floor(-8.1), -9.0),
26+
expect:equal(float:floor(-8.0), -8.0).

src/float.gleam

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ pub fn to_string(f) {
1111
|> iodata:from_float
1212
|> iodata:to_string
1313
}
14+
15+
pub external fn ceiling(Float) -> Float = "math" "ceil";
16+
17+
pub external fn floor(Float) -> Float = "math" "floor";

test/float_test.gleam

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,31 @@ pub fn to_string_test() {
3636
|> float:to_string
3737
|> expect:equal(_, "-8.1")
3838
}
39+
40+
pub fn ceiling_test() {
41+
8.1
42+
|> float:ceiling
43+
|> expect:equal(_, 9.0)
44+
45+
-8.1
46+
|> float:ceiling
47+
|> expect:equal(_, -8.0)
48+
49+
-8.0
50+
|> float:ceiling
51+
|> expect:equal(_, -8.0)
52+
}
53+
54+
pub fn floor_test() {
55+
8.1
56+
|> float:floor
57+
|> expect:equal(_, 8.0)
58+
59+
-8.1
60+
|> float:floor
61+
|> expect:equal(_, -9.0)
62+
63+
-8.0
64+
|> float:floor
65+
|> expect:equal(_, -8.0)
66+
}

0 commit comments

Comments
 (0)