util.c: round to even

* util.c (ruby_dtoa): round to even, instead of rounding to
  nearest.  [ruby-core:77864] [Bug #12889]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56557 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-11-04 07:31:47 +00:00
parent 25728a1963
commit 6ed8c79ddb
3 changed files with 23 additions and 8 deletions

View File

@ -1,3 +1,8 @@
Fri Nov 4 16:31:45 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* util.c (ruby_dtoa): round to even, instead of rounding to
nearest. [ruby-core:77864] [Bug #12889]
Fri Nov 4 15:31:00 2016 NARUSE, Yui <naruse@ruby-lang.org>
* configure.in: Add compiler version message into rbconfig

View File

@ -283,7 +283,16 @@ class TestSprintf < Test::Unit::TestCase
end
def test_float_prec
assert_equal("5.03", sprintf("%.2f",5.025))
assert_equal("5.00", sprintf("%.2f",5.005))
assert_equal("5.02", sprintf("%.2f",5.015))
assert_equal("5.02", sprintf("%.2f",5.025))
assert_equal("5.04", sprintf("%.2f",5.035))
bug12889 = '[ruby-core:77864] [Bug #12889]'
assert_equal("1234567892", sprintf("%.0f", 1234567891.99999))
assert_equal("1234567892", sprintf("%.0f", 1234567892.49999))
assert_equal("1234567892", sprintf("%.0f", 1234567892.50000))
assert_equal("1234567894", sprintf("%.0f", 1234567893.50000))
assert_equal("1234567892", sprintf("%.0f", 1234567892.00000), bug12889)
end
BSIZ = 120

15
util.c
View File

@ -3165,7 +3165,7 @@ ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
spec_case, try_quick;
spec_case, try_quick, half = 0;
Long L;
#ifndef Sudden_Underflow
int denorm;
@ -3452,17 +3452,17 @@ ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
ilim = i;
*s++ = '0' + (int)L;
if (i == ilim) {
double x;
if (dval(d) > 0.5 + dval(eps))
goto bump_up;
else if (!isinf(x = d_ * tens[ilim-1] + 0.5) &&
dval(d) > modf(x, &x))
goto bump_up;
else if (dval(d) < 0.5 - dval(eps)) {
while (*--s == '0') ;
s++;
goto ret1;
}
half = 1;
if ((*(s-1) - '0') & 1) {
goto bump_up;
}
break;
}
}
@ -3770,12 +3770,13 @@ keep_dig:
*s++ = '1';
goto ret;
}
++*s++;
if (!half || (*s - '0') & 1)
++*s;
}
else {
while (*--s == '0') ;
s++;
}
s++;
ret:
Bfree(S);
if (mhi) {