diff --git a/src/javascript/lib/core/erlang_compat/erlang.js b/src/javascript/lib/core/erlang_compat/erlang.js index ddece09d..a72d3805 100644 --- a/src/javascript/lib/core/erlang_compat/erlang.js +++ b/src/javascript/lib/core/erlang_compat/erlang.js @@ -164,7 +164,7 @@ function equals(left, right) { } function div(left, right) { - return left / right; + return Math.trunc(left / right); } function not(x) { @@ -357,7 +357,7 @@ function min(first, second) { } function round(number) { - return Math.round(number); + return Math.sign(number) * Math.round(Math.abs(number)); } function tl(list) { @@ -463,7 +463,6 @@ function nodes(arg = []) { for (const nodeType of nodeTypes) { if (nodeType === Symbol.for('this')) { nodesFound.push(Symbol.for('nonode@nohost')); - console.log(nodesFound); } } return nodesFound; diff --git a/src/javascript/tests/core/erlang_compat/erlang_spec.js b/src/javascript/tests/core/erlang_compat/erlang_spec.js index 6e41e6ea..bc2c243d 100644 --- a/src/javascript/tests/core/erlang_compat/erlang_spec.js +++ b/src/javascript/tests/core/erlang_compat/erlang_spec.js @@ -83,6 +83,36 @@ test('list_subtraction', (t) => { t.deepEqual(Core.erlang.list_subtraction([1, 2, 3], [1, 2]), [3]); }); +test('div', (t) => { + t.is(Core.erlang.div(6, 3), 2); + t.is(Core.erlang.div(7, 2), 3); + t.is(Core.erlang.div(-7, 2), -3); + t.is(Core.erlang.div(7, -2), -3); + t.is(Core.erlang.div(-7, -2), 3); +}); + +test('rem', (t) => { + t.is(Core.erlang.rem(7, 2), 1); + t.is(Core.erlang.rem(-7, 2), -1); + t.is(Core.erlang.rem(7, -2), 1); + t.is(Core.erlang.rem(-7, -2), -1); +}); + +test('round', (t) => { + t.is(Core.erlang.round(1.4), 1); + t.is(Core.erlang.round(2.5), 3); + t.is(Core.erlang.round(-2.5), -3); + t.is(Core.erlang.round(0.5), 1); + t.is(Core.erlang.round(-0.5), -1); + t.is(Core.erlang.round(4), 4); + t.is(Core.erlang.round(-4), -4); +}); + +test('trunc', (t) => { + t.is(Core.erlang.trunc(2.7), 2); + t.is(Core.erlang.trunc(-2.7), -2); +}); + test('node', (t) => { t.deepEqual(Core.erlang.node(), Symbol.for('nonode@nohost')); });