Skip to content

Commit 32dc04d

Browse files
int3kripken
authored andcommitted
Make lrint more correct. Closes #1265.
1 parent 5f2ccad commit 32dc04d

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/library.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -5732,7 +5732,8 @@ LibraryManager.library = {
57325732
llround: 'round',
57335733
llroundf: 'round',
57345734
rint: function(x) {
5735-
return (x > 0) ? -Math.round(-x) : Math.round(x);
5735+
if (Math.abs(x % 1) !== 0.5) return Math.round(x);
5736+
return x + x % 2 + ((x < 0) ? 1 : -1);
57365737
},
57375738
rintf: 'rint',
57385739
lrint: 'rint',

tests/runner.py

+33
Original file line numberDiff line numberDiff line change
@@ -1957,6 +1957,39 @@ def test_frexp(self):
19571957
1.000000=1.000000*2^0
19581958
-1.000000=-1.000000*2^0''')
19591959

1960+
def test_rounding(self):
1961+
src = '''
1962+
#include <stdio.h>
1963+
#include <math.h>
1964+
1965+
int main()
1966+
{
1967+
printf("%.1f ", round(1.4));
1968+
printf("%.1f ", round(1.6));
1969+
printf("%.1f ", round(-1.4));
1970+
printf("%.1f ", round(-1.6));
1971+
1972+
printf("%.1f ", round(1.5));
1973+
printf("%.1f ", round(2.5));
1974+
printf("%.1f ", round(-1.5));
1975+
printf("%.1f ", round(-2.5));
1976+
1977+
printf("%ld ", lrint(1.4));
1978+
printf("%ld ", lrint(1.6));
1979+
printf("%ld ", lrint(-1.4));
1980+
printf("%ld ", lrint(-1.6));
1981+
1982+
printf("%ld ", lrint(1.5));
1983+
printf("%ld ", lrint(2.5));
1984+
printf("%ld ", lrint(-1.5));
1985+
printf("%ld ", lrint(-2.5));
1986+
1987+
return 0;
1988+
}
1989+
'''
1990+
self.do_run(src, "1.0 2.0 -1.0 -2.0 2.0 3.0 -2.0 -3.0 "
1991+
"1 2 -1 -2 2 2 -2 -2")
1992+
19601993
def test_getgep(self):
19611994
# Generated code includes getelementptr (getelementptr, 0, 1), i.e., GEP as the first param to GEP
19621995
src = '''

0 commit comments

Comments
 (0)