Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions asteroid/modules/math.ast
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,21 @@ __retval__ = ('integer', math.floor(val_x[1]))
"
end

------------------------------------------------------------------
function round
------------------------------------------------------------------
-- Return x rounded to the nearest integer. If two integers are equally close,
-- x is rounded to the nearest even integer.
with (x:%real) do return escape
"
global __retval__

val_x = state.symbol_table.lookup_sym('x')

__retval__ = ('integer', round(val_x[1]))
"
end

------------------------------------------------------------------
function gcd
------------------------------------------------------------------
Expand Down
29 changes: 29 additions & 0 deletions asteroid/test-suites/regression-tests/test147.ast
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
load system math.

let test_cases = [
(3.14, 3),
(3.508, 4),
(12.99999, 13),
(13.00000, 13),
(13.00001, 13),
(13.49999, 13),
(13.50000, 14),
(13.50001, 14),
(13.99999, 14),
(14.00000, 14),
(14.00001, 14),
(14.49999, 14),
(14.50000, 14),
(12.5, 12),
(13.5, 14),
(14.5, 14),
(15.5, 16),
(0.0, 0),
(-0.0, 0),
(-53.246, -53),
(-53.5, -54)
].

for (input, expected_output) in test_cases do
assert (math @round input == expected_output).
end
4 changes: 4 additions & 0 deletions docs/Reference Guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,10 @@ math **@ceil** x:%real
math **@floor** x:%real
Returns the floor of x: the largest integer less than or equal to x.

math **@round** x:%real
Returns x rounded to the nearest integer. If two integers are equally
close, x is rounded to the nearest even integer.

math **@gcd** (a:%integer, b:%integer)
Returns the greatest common denominator that both integers share.

Expand Down
4 changes: 4 additions & 0 deletions docs/Reference Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,10 @@ math **@ceil** x:%real
math **@floor** x:%real
Returns the floor of x: the largest integer less than or equal to x.

math **@round** x:%real
Returns x rounded to the nearest integer. If two integers are equally
close, x is rounded to the nearest even integer.

math **@gcd** (a:%integer, b:%integer)
Returns the greatest common denominator that both integers share.

Expand Down