Actions
Bug #13138
closedRounding bug
Description
31.175.round(2) = 31.18
32.175.round(2) = 32.17 (I expect 32.18)
Files
Updated by nobu (Nobuyoshi Nakada) over 7 years ago
Fixed by r55604 in the trunk.
diff --git i/numeric.c w/numeric.c
index 46f112349f..9ff68f25c9 100644
--- i/numeric.c
+++ w/numeric.c
@@ -1786,7 +1786,7 @@ static VALUE
flo_round(int argc, VALUE *argv, VALUE num)
{
VALUE nd;
- double number, f;
+ double number, f, x;
int ndigits = 0;
int binexp;
enum {float_dig = DBL_DIG+2};
@@ -1828,7 +1828,14 @@ flo_round(int argc, VALUE *argv, VALUE num)
return DBL2NUM(0);
}
f = pow(10, ndigits);
- return DBL2NUM(round(number * f) / f);
+ x = round(number * f);
+ if (x > 0) {
+ if ((x + 0.5) / f <= number) x += 1;
+ }
+ else {
+ if ((x - 0.5) / f >= number) x -= 1;
+ }
+ return DBL2NUM(x / f);
}
/*
diff --git i/test/ruby/test_float.rb w/test/ruby/test_float.rb
index 3afd26d0b4..7d979a824f 100644
--- i/test/ruby/test_float.rb
+++ w/test/ruby/test_float.rb
@@ -442,6 +442,11 @@
assert_raise(TypeError) {1.0.round(nil)}
def (prec = Object.new).to_int; 2; end
assert_equal(1.0, 0.998.round(prec))
+
+ assert_equal(+5.02, +5.015.round(2))
+ assert_equal(-5.02, -5.015.round(2))
+ assert_equal(+1.26, +1.255.round(2))
+ assert_equal(-1.26, -1.255.round(2))
end
VS = [
Updated by nobu (Nobuyoshi Nakada) over 7 years ago
- Status changed from Open to Closed
- Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN to 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONTNEED
Updated by usa (Usaku NAKAMURA) about 7 years ago
- Backport changed from 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: DONTNEED to 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONTNEED
Updated by usa (Usaku NAKAMURA) almost 7 years ago
- Backport changed from 2.2: REQUIRED, 2.3: REQUIRED, 2.4: DONTNEED to 2.2: REQUIRED, 2.3: DONE, 2.4: DONTNEED
ruby_2_3 r58513 merged revision(s) 55604,55612.
Actions
Like0
Like0Like0Like0Like0