Skip to content

Commit

Permalink
wasm: fix rounding mode
Browse files Browse the repository at this point in the history
Same as in topdown, we should have this be true in wasm:

    round(1.5) == 2
    round(2.5) == 3

The default rounding mode for libmpdec's maxcontext was MPD_ROUND_HALF_EVEN,
would would round 2.5 to 2.

See https://www.bytereef.org/mpdecimal/doc/libmpdec/context.html#rounding

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus authored and tsandall committed Dec 14, 2020
1 parent 9e52194 commit f465b22
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 81 deletions.
84 changes: 3 additions & 81 deletions test/cases/testdata/arithmetic/test-arithmetic-0813.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,95 +5,17 @@ cases:
- 2
- 3
- 4
b:
v1: hello
v2: goodbye
c:
- x:
- true
- false
- foo
"y":
- null
- 3.14159
z:
p: true
q: false
d:
e:
- bar
- baz
f:
- xs:
- 1
ys:
- 2
- xs:
- 2
ys:
- 3
g:
a:
- 1
- 0
- 0
- 0
b:
- 0
- 2
- 0
- 0
c:
- 0
- 0
- 0
- 4
h:
- - 1
- 2
- 3
- - 2
- 3
- 4
l:
- a: bob
b: -1
c:
- 1
- 2
- 3
- 4
- a: alice
b: 1
c:
- 2
- 3
- 4
- 5
d: null
m: []
numbers:
- "1"
- "2"
- "3"
- "4"
strings:
bar: 2
baz: 3
foo: 1
three: 3
modules:
- |
package generated
package test
p[z] {
data.a[i] = x
__local0__ = i / x
y = __local0__
y = i / x
round(y, z)
}
note: arithmetic/divide+round
query: data.generated.p = x
query: data.test.p = x
sort_bindings: true
want_result:
- x:
Expand Down
6 changes: 6 additions & 0 deletions test/wasm/assets/018_builtins.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ cases:
- note: round built-in
query: round(1.4,x)
want_result: [{'x': 1}]
- note: round built-in ("halfs up")
query: round(1.5,x)
want_result: [{'x': 2}]
- note: round built-in ("halfs up not to even")
query: round(2.5,x)
want_result: [{'x': 3}]
- note: plus built-in
query: plus(1,1,x)
want_result: [{'x': 2}]
Expand Down
1 change: 1 addition & 0 deletions wasm/src/mpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ static void init(void)

mpd_maxcontext(&max_ctx);
max_ctx.traps = 0;
max_ctx.round = MPD_ROUND_HALF_UP; // .5 always rounded up

one = mpd_qnew();

Expand Down
2 changes: 2 additions & 0 deletions wasm/tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,8 @@ void test_arithmetic(void)
test("round -1.4 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(-1.4)))) == -1);
test("round 1.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(1.5)))) == 2);
test("round -1.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(-1.5)))) == -2);
test("round 2.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(2.5)))) == 3);
test("round -2.5 (float)", opa_number_as_float(opa_cast_number(opa_arith_round(opa_number_float(-2.5)))) == -3);
test("plus 1+2", opa_number_as_float(opa_cast_number(opa_arith_plus(opa_number_float(1), opa_number_float(2)))) == 3);
test("minus 3-2", opa_number_as_float(opa_cast_number(opa_arith_minus(opa_number_float(3), opa_number_float(2)))) == 1);

Expand Down

0 comments on commit f465b22

Please sign in to comment.