Skip to content

Commit

Permalink
util.c: round to even
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
nobu committed Nov 4, 2016
1 parent 25728a1 commit 6ed8c79
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 10 additions & 1 deletion test/ruby/test_sprintf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,16 @@ def test_float_hex
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
Expand Down
15 changes: 8 additions & 7 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -3770,12 +3770,13 @@ ruby_dtoa(double d_, int mode, int ndigits, int *decpt, int *sign, char **rve)
*s++ = '1';
goto ret;
}
++*s++;
if (!half || (*s - '0') & 1)
++*s;
}
else {
while (*--s == '0') ;
s++;
}
s++;
ret:
Bfree(S);
if (mhi) {
Expand Down

0 comments on commit 6ed8c79

Please sign in to comment.