aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJez Ng <me@jezng.com>2013-06-06 10:27:17 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-06-07 10:09:12 -0700
commit32dc04d8d17fb7cc4122d692c8fa1ba661a33803 (patch)
treec464228c745912654e3ce24ebacd74a6641bfd81
parent5f2ccad0a399276de4e8426fcd1449e256499815 (diff)
Make lrint more correct. Closes #1265.
-rw-r--r--src/library.js3
-rwxr-xr-xtests/runner.py33
2 files changed, 35 insertions, 1 deletions
diff --git a/src/library.js b/src/library.js
index 5779327f..2578fda4 100644
--- a/src/library.js
+++ b/src/library.js
@@ -5732,7 +5732,8 @@ LibraryManager.library = {
llround: 'round',
llroundf: 'round',
rint: function(x) {
- return (x > 0) ? -Math.round(-x) : Math.round(x);
+ if (Math.abs(x % 1) !== 0.5) return Math.round(x);
+ return x + x % 2 + ((x < 0) ? 1 : -1);
},
rintf: 'rint',
lrint: 'rint',
diff --git a/tests/runner.py b/tests/runner.py
index 53eb56fe..5e8cbeea 100755
--- a/tests/runner.py
+++ b/tests/runner.py
@@ -1957,6 +1957,39 @@ Succeeded!
1.000000=1.000000*2^0
-1.000000=-1.000000*2^0''')
+ def test_rounding(self):
+ src = '''
+ #include <stdio.h>
+ #include <math.h>
+
+ int main()
+ {
+ printf("%.1f ", round(1.4));
+ printf("%.1f ", round(1.6));
+ printf("%.1f ", round(-1.4));
+ printf("%.1f ", round(-1.6));
+
+ printf("%.1f ", round(1.5));
+ printf("%.1f ", round(2.5));
+ printf("%.1f ", round(-1.5));
+ printf("%.1f ", round(-2.5));
+
+ printf("%ld ", lrint(1.4));
+ printf("%ld ", lrint(1.6));
+ printf("%ld ", lrint(-1.4));
+ printf("%ld ", lrint(-1.6));
+
+ printf("%ld ", lrint(1.5));
+ printf("%ld ", lrint(2.5));
+ printf("%ld ", lrint(-1.5));
+ printf("%ld ", lrint(-2.5));
+
+ return 0;
+ }
+ '''
+ self.do_run(src, "1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0 "
+ "1 2 -1 -2 2 2 -2 -2")
+
def test_getgep(self):
# Generated code includes getelementptr (getelementptr, 0, 1), i.e., GEP as the first param to GEP
src = '''