diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-05 09:49:39 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-11-05 09:49:39 +0000 |
commit | 73b3a8497c97cb6a9ed76cc52733a98f7791c9b0 (patch) | |
tree | d209f0b546e45cb8d8fa7fc2bbbb5044e53e2c8f /test | |
parent | 5710defbe621766796d973b9ef6451a94d6d7eff (diff) | |
download | ruby-73b3a8497c97cb6a9ed76cc52733a98f7791c9b0.tar.gz |
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
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_float.rb | 42 | ||||
-rw-r--r-- | test/ruby/test_integer.rb | 44 | ||||
-rw-r--r-- | test/ruby/test_rational.rb | 23 | ||||
-rw-r--r-- | test/test_mathn.rb | 50 |
4 files changed, 141 insertions, 18 deletions
diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index b31c0415f2..f2989e4f5b 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -659,6 +659,48 @@ class TestFloat < Test::Unit::TestCase } end + def test_round_half_even + assert_equal(12.0, 12.5.round(half: :even)) + assert_equal(14.0, 13.5.round(half: :even)) + + assert_equal(2.2, 2.15.round(1, half: :even)) + assert_equal(2.2, 2.25.round(1, half: :even)) + assert_equal(2.4, 2.35.round(1, half: :even)) + + assert_equal(-2.2, -2.15.round(1, half: :even)) + assert_equal(-2.2, -2.25.round(1, half: :even)) + assert_equal(-2.4, -2.35.round(1, half: :even)) + + assert_equal(7.1364, 7.13645.round(4, half: :even)) + assert_equal(7.1365, 7.1364501.round(4, half: :even)) + assert_equal(7.1364, 7.1364499.round(4, half: :even)) + + assert_equal(-7.1364, -7.13645.round(4, half: :even)) + assert_equal(-7.1365, -7.1364501.round(4, half: :even)) + assert_equal(-7.1364, -7.1364499.round(4, half: :even)) + end + + def test_round_half_up + assert_equal(13.0, 12.5.round(half: :up)) + assert_equal(14.0, 13.5.round(half: :up)) + + assert_equal(2.2, 2.15.round(1, half: :up)) + assert_equal(2.3, 2.25.round(1, half: :up)) + assert_equal(2.4, 2.35.round(1, half: :up)) + + assert_equal(-2.2, -2.15.round(1, half: :up)) + assert_equal(-2.3, -2.25.round(1, half: :up)) + assert_equal(-2.4, -2.35.round(1, half: :up)) + + assert_equal(7.1365, 7.13645.round(4, half: :up)) + assert_equal(7.1365, 7.1364501.round(4, half: :up)) + assert_equal(7.1364, 7.1364499.round(4, half: :up)) + + assert_equal(-7.1365, -7.13645.round(4, half: :up)) + assert_equal(-7.1365, -7.1364501.round(4, half: :up)) + assert_equal(-7.1364, -7.1364499.round(4, half: :up)) + end + def test_Float assert_in_delta(0.125, Float("0.1_2_5"), 0.00001) assert_in_delta(0.125, "0.1_2_5__".to_f, 0.00001) diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 6e505ec003..d9dd754ca6 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -187,13 +187,49 @@ class TestInteger < Test::Unit::TestCase assert_int_equal(11110, 11111.round(-1)) assert_int_equal(11100, 11111.round(-2)) assert_int_equal(+200, +249.round(-2)) - assert_int_equal(+300, +250.round(-2)) + assert_int_equal(+200, +250.round(-2)) assert_int_equal(-200, -249.round(-2)) - assert_int_equal(-300, -250.round(-2)) - assert_int_equal(+30 * 10**70, (+25 * 10**70).round(-71)) - assert_int_equal(-30 * 10**70, (-25 * 10**70).round(-71)) + assert_int_equal(+200, +249.round(-2, half: :even)) + assert_int_equal(+200, +250.round(-2, half: :even)) + assert_int_equal(+300, +349.round(-2, half: :even)) + assert_int_equal(+400, +350.round(-2, half: :even)) + assert_int_equal(+200, +249.round(-2, half: :up)) + assert_int_equal(+300, +250.round(-2, half: :up)) + assert_int_equal(+300, +349.round(-2, half: :up)) + assert_int_equal(+400, +350.round(-2, half: :up)) + assert_int_equal(-200, -250.round(-2)) + assert_int_equal(-200, -249.round(-2, half: :even)) + assert_int_equal(-200, -250.round(-2, half: :even)) + assert_int_equal(-300, -349.round(-2, half: :even)) + assert_int_equal(-400, -350.round(-2, half: :even)) + assert_int_equal(-200, -249.round(-2, half: :up)) + assert_int_equal(-300, -250.round(-2, half: :up)) + assert_int_equal(-300, -349.round(-2, half: :up)) + assert_int_equal(-400, -350.round(-2, half: :up)) + assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71)) + assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71)) assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71)) assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71)) + assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71)) + assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71)) + assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71)) + assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71)) + assert_int_equal(+20 * 10**70, (+25 * 10**70).round(-71, half: :even)) + assert_int_equal(-20 * 10**70, (-25 * 10**70).round(-71, half: :even)) + assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :even)) + assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :even)) + assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71, half: :even)) + assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71, half: :even)) + assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :even)) + assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :even)) + assert_int_equal(+30 * 10**70, (+25 * 10**70).round(-71, half: :up)) + assert_int_equal(-30 * 10**70, (-25 * 10**70).round(-71, half: :up)) + assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).round(-71, half: :up)) + assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).round(-71, half: :up)) + assert_int_equal(+40 * 10**70, (+35 * 10**70).round(-71, half: :up)) + assert_int_equal(-40 * 10**70, (-35 * 10**70).round(-71, half: :up)) + assert_int_equal(+30 * 10**70, (+35 * 10**70 - 1).round(-71, half: :up)) + assert_int_equal(-30 * 10**70, (-35 * 10**70 + 1).round(-71, half: :up)) assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1)) assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1)) diff --git a/test/ruby/test_rational.rb b/test/ruby/test_rational.rb index 387f4121cf..d65c292970 100644 --- a/test/ruby/test_rational.rb +++ b/test/ruby/test_rational.rb @@ -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 diff --git a/test/test_mathn.rb b/test/test_mathn.rb index aaf132ba88..2ea049502c 100644 --- a/test/test_mathn.rb +++ b/test/test_mathn.rb @@ -96,17 +96,17 @@ class TestMathn < Test::Unit::TestCase def test_round assert_separately(%w[-rmathn], <<-EOS, ignore_stderr: true) assert_equal( 3, ( 13/5).round) - assert_equal( 3, ( 5/2).round) + assert_equal( 2, ( 5/2).round) assert_equal( 2, ( 12/5).round) assert_equal(-2, (-12/5).round) - assert_equal(-3, ( -5/2).round) + assert_equal(-2, ( -5/2).round) assert_equal(-3, (-13/5).round) assert_equal( 3, ( 13/5).round(0)) - assert_equal( 3, ( 5/2).round(0)) + assert_equal( 2, ( 5/2).round(0)) assert_equal( 2, ( 12/5).round(0)) assert_equal(-2, (-12/5).round(0)) - assert_equal(-3, ( -5/2).round(0)) + assert_equal(-2, ( -5/2).round(0)) assert_equal(-3, (-13/5).round(0)) assert_equal(( 13/5), ( 13/5).round(2)) @@ -115,6 +115,48 @@ class TestMathn < Test::Unit::TestCase assert_equal((-12/5), (-12/5).round(2)) assert_equal(( -5/2), ( -5/2).round(2)) assert_equal((-13/5), (-13/5).round(2)) + + assert_equal( 3, ( 13/5).round(half: :even)) + assert_equal( 2, ( 5/2).round(half: :even)) + assert_equal( 2, ( 12/5).round(half: :even)) + assert_equal(-2, (-12/5).round(half: :even)) + assert_equal(-2, ( -5/2).round(half: :even)) + assert_equal(-3, (-13/5).round(half: :even)) + + assert_equal( 3, ( 13/5).round(0, half: :even)) + assert_equal( 2, ( 5/2).round(0, half: :even)) + assert_equal( 2, ( 12/5).round(0, half: :even)) + assert_equal(-2, (-12/5).round(0, half: :even)) + assert_equal(-2, ( -5/2).round(0, half: :even)) + assert_equal(-3, (-13/5).round(0, half: :even)) + + assert_equal(( 13/5), ( 13/5).round(2, half: :even)) + assert_equal(( 5/2), ( 5/2).round(2, half: :even)) + assert_equal(( 12/5), ( 12/5).round(2, half: :even)) + assert_equal((-12/5), (-12/5).round(2, half: :even)) + assert_equal(( -5/2), ( -5/2).round(2, half: :even)) + assert_equal((-13/5), (-13/5).round(2, half: :even)) + + assert_equal( 3, ( 13/5).round(half: :up)) + assert_equal( 3, ( 5/2).round(half: :up)) + assert_equal( 2, ( 12/5).round(half: :up)) + assert_equal(-2, (-12/5).round(half: :up)) + assert_equal(-3, ( -5/2).round(half: :up)) + assert_equal(-3, (-13/5).round(half: :up)) + + assert_equal( 3, ( 13/5).round(0, half: :up)) + assert_equal( 3, ( 5/2).round(0, half: :up)) + assert_equal( 2, ( 12/5).round(0, half: :up)) + assert_equal(-2, (-12/5).round(0, half: :up)) + assert_equal(-3, ( -5/2).round(0, half: :up)) + assert_equal(-3, (-13/5).round(0, half: :up)) + + assert_equal(( 13/5), ( 13/5).round(2, half: :up)) + assert_equal(( 5/2), ( 5/2).round(2, half: :up)) + assert_equal(( 12/5), ( 12/5).round(2, half: :up)) + assert_equal((-12/5), (-12/5).round(2, half: :up)) + assert_equal(( -5/2), ( -5/2).round(2, half: :up)) + assert_equal((-13/5), (-13/5).round(2, half: :up)) EOS end end |