numeric.c: round to nearest even

* numeric.c (flo_round, int_round): support round-to-nearest-even
  semantics of IEEE 754 to match sprintf behavior, and add `half:`
  optional keyword argument for the old behavior.
  [ruby-core:76273] [Bug #12548]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56590 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-11-05 09:49:39 +00:00
parent 76977611dd
commit dfe91fcd08
10 changed files with 322 additions and 39 deletions

View file

@ -597,17 +597,20 @@ class Rational_Test < Test::Unit::TestCase
end
def test_trunc
[[Rational(13, 5), [ 2, 3, 2, 3]], # 2.6
[Rational(5, 2), [ 2, 3, 2, 3]], # 2.5
[Rational(12, 5), [ 2, 3, 2, 2]], # 2.4
[Rational(-12,5), [-3, -2, -2, -2]], # -2.4
[Rational(-5, 2), [-3, -2, -2, -3]], # -2.5
[Rational(-13, 5), [-3, -2, -2, -3]], # -2.6
[[Rational(13, 5), [ 2, 3, 2, 3, 3, 3]], # 2.6
[Rational(5, 2), [ 2, 3, 2, 2, 2, 3]], # 2.5
[Rational(12, 5), [ 2, 3, 2, 2, 2, 2]], # 2.4
[Rational(-12,5), [-3, -2, -2, -2, -2, -2]], # -2.4
[Rational(-5, 2), [-3, -2, -2, -2, -2, -3]], # -2.5
[Rational(-13, 5), [-3, -2, -2, -3, -3, -3]], # -2.6
].each do |i, a|
assert_equal(a[0], i.floor)
assert_equal(a[1], i.ceil)
assert_equal(a[2], i.truncate)
assert_equal(a[3], i.round)
s = proc {i.inspect}
assert_equal(a[0], i.floor, s)
assert_equal(a[1], i.ceil, s)
assert_equal(a[2], i.truncate, s)
assert_equal(a[3], i.round, s)
assert_equal(a[4], i.round(half: :even), s)
assert_equal(a[5], i.round(half: :up), s)
end
end