From febc42d05c6b8a401fff2361bf03fe338eb4d1b0 Mon Sep 17 00:00:00 2001 From: mrkn Date: Thu, 6 Jun 2013 01:04:11 +0000 Subject: [PATCH] * numeric.c (num_quo): should return a Float for a Float argument. [ruby-dev:44710] [Bug #5515] * test/ruby/test_fixnum.rb: Add an assertion for the above change. * test/ruby/test_bignum.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41099 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 9 +++++++++ numeric.c | 33 +++++++++++++++++++-------------- test/ruby/test_bignum.rb | 4 +++- test/ruby/test_fixnum.rb | 1 + 4 files changed, 32 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3afb96c997..d54787449d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +Thu Jun 6 09:41:00 2013 Kenta Murata + + * numeric.c (num_quo): should return a Float for a Float argument. + [ruby-dev:44710] [Bug #5515] + + * test/ruby/test_fixnum.rb: Add an assertion for the above change. + + * test/ruby/test_bignum.rb: ditto. + Thu Jun 6 00:59:44 2013 Masaya Tarui * gc.c (gc_mark): get rid of pushing useless objests. diff --git a/numeric.c b/numeric.c index 89544dc745..e8d2f9df25 100644 --- a/numeric.c +++ b/numeric.c @@ -369,20 +369,6 @@ num_uminus(VALUE num) return rb_funcall(zero, '-', 1, num); } -/* - * call-seq: - * num.quo(numeric) -> real - * - * Returns most exact division (rational for integers, float for floats). - */ - -static VALUE -num_quo(VALUE x, VALUE y) -{ - return rb_funcall(rb_rational_raw1(x), '/', 1, y); -} - - /* * call-seq: * num.fdiv(numeric) -> float @@ -397,6 +383,25 @@ num_fdiv(VALUE x, VALUE y) } +/* + * call-seq: + * num.quo(int_or_rat) -> rat + * num.quo(flo) -> flo + * + * Returns most exact division (rational for integers, float for floats). + */ + +static VALUE +num_quo(VALUE x, VALUE y) +{ + if (RB_TYPE_P(y, T_FLOAT)) { + return num_fdiv(x, y); + } + + return rb_funcall(rb_rational_raw1(x), '/', 1, y); +} + + /* * call-seq: * num.div(numeric) -> integer diff --git a/test/ruby/test_bignum.rb b/test/ruby/test_bignum.rb index a3cba398e3..9f709daa8a 100644 --- a/test/ruby/test_bignum.rb +++ b/test/ruby/test_bignum.rb @@ -413,6 +413,8 @@ class TestBignum < Test::Unit::TestCase end def test_quo + assert_kind_of(Float, T32.quo(1.0)) + assert_equal(T32.to_f, T32.quo(1)) assert_equal(T32.to_f, T32.quo(1.0)) assert_equal(T32.to_f, T32.quo(T_ONE)) @@ -420,7 +422,7 @@ class TestBignum < Test::Unit::TestCase assert_raise(TypeError) { T32.quo("foo") } assert_equal(1024**1024, (1024**1024).quo(1)) - assert_equal(1024**1024, (1024**1024).quo(1.0)) + assert_equal(Float::INFINITY, (1024**1024).quo(1.0)) assert_equal(1024**1024*2, (1024**1024*2).quo(1)) inf = 1 / 0.0; nan = inf / inf diff --git a/test/ruby/test_fixnum.rb b/test/ruby/test_fixnum.rb index 85c8bdfa7b..c016b2f4e0 100644 --- a/test/ruby/test_fixnum.rb +++ b/test/ruby/test_fixnum.rb @@ -157,6 +157,7 @@ class TestFixnum < Test::Unit::TestCase assert_equal(0, 1 / (2**32)) assert_equal(0, 1.div(2**32)) + assert_kind_of(Float, 1.quo(2.0)) assert_equal(0.5, 1.quo(2.0)) assert_equal(0.5, 1 / 2.0) assert_equal(0, 1.div(2.0))