ruby-changes:52665
From: nobu <ko1@a...>
Date: Sat, 29 Sep 2018 09:45:46 +0900 (JST)
Subject: [ruby-changes:52665] nobu:r64877 (trunk): rational.c: may be float
nobu 2018-09-29 09:45:41 +0900 (Sat, 29 Sep 2018) New Revision: 64877 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=64877 Log: rational.c: may be float * rational.c (f_muldiv): Integer#** can return Rational with Float right now. [ruby-core:89212] [Bug #15175] Modified files: trunk/numeric.c trunk/rational.c trunk/test/ruby/test_integer.rb Index: rational.c =================================================================== --- rational.c (revision 64876) +++ rational.c (revision 64877) @@ -805,6 +805,16 @@ f_muldiv(VALUE self, VALUE anum, VALUE a https://github.com/ruby/ruby/blob/trunk/rational.c#L805 VALUE num, den; assert(RB_TYPE_P(self, T_RATIONAL)); + + /* Integer#** can return Rational with Float right now */ + if (RB_FLOAT_TYPE_P(anum) || RB_FLOAT_TYPE_P(aden) || + RB_FLOAT_TYPE_P(bnum) || RB_FLOAT_TYPE_P(bden)) { + double an = NUM2DBL(anum), ad = NUM2DBL(aden); + double bn = NUM2DBL(bnum), bd = NUM2DBL(bden); + double x = (an * bn) / (ad * bd); + return DBL2NUM(x); + } + assert(RB_INTEGER_TYPE_P(anum)); assert(RB_INTEGER_TYPE_P(aden)); assert(RB_INTEGER_TYPE_P(bnum)); Index: numeric.c =================================================================== --- numeric.c (revision 64876) +++ numeric.c (revision 64877) @@ -4034,7 +4034,13 @@ fix_pow(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4034 } if (BIGNUM_NEGATIVE_P(y)) { if (a == 0) rb_num_zerodiv(); - return rb_rational_raw(INT2FIX(1), rb_int_pow(x, rb_big_uminus(y))); + y = rb_int_pow(x, rb_big_uminus(y)); + if (0 && RB_FLOAT_TYPE_P(y)) { + /* Maybe should return a Float */ + double d = pow((double)a, RFLOAT_VALUE(y)); + return DBL2NUM(d); + } + return rb_rational_raw(INT2FIX(1), y); } if (a == 0) return INT2FIX(0); x = rb_int2big(FIX2LONG(x)); Index: test/ruby/test_integer.rb =================================================================== --- test/ruby/test_integer.rb (revision 64876) +++ test/ruby/test_integer.rb (revision 64877) @@ -24,6 +24,9 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L24 rescue nil end, "[ruby-dev:32084] [ruby-dev:34547]") + + x = EnvUtil.suppress_warning {2 ** -0x4000000000000000} + assert_in_delta(0.0, (x / 2), Float::EPSILON) end def test_lshift -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/