[前][次][番号順一覧][スレッド一覧]

ruby-changes:45615

From: nobu <ko1@a...>
Date: Thu, 23 Feb 2017 08:28:32 +0900 (JST)
Subject: [ruby-changes:45615] nobu:r57688 (trunk): rational.c: infinity in power

nobu	2017-02-23 08:28:26 +0900 (Thu, 23 Feb 2017)

  New Revision: 57688

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=57688

  Log:
    rational.c: infinity in power
    
    * rational.c (nurat_expt): return Infinity due to overflow.
      [ruby-core:79686] [Bug #13242]:

  Modified files:
    trunk/numeric.c
    trunk/rational.c
    trunk/test/ruby/test_rational.rb
Index: numeric.c
===================================================================
--- numeric.c	(revision 57687)
+++ numeric.c	(revision 57688)
@@ -3913,6 +3913,8 @@ int_pow(long x, unsigned long y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L3913
 		VALUE v;
 	      bignum:
 		v = rb_big_pow(rb_int2big(x), LONG2NUM(y));
+		if (RB_FLOAT_TYPE_P(v)) /* infinity due to overflow */
+		    return v;
 		if (z != 1) v = rb_big_mul(rb_int2big(neg ? -z : z), v);
 		return v;
 	    }
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 57687)
+++ test/ruby/test_rational.rb	(revision 57688)
@@ -955,6 +955,12 @@ class Rational_Test < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L955
     assert_raise(ZeroDivisionError, bug5713) { Rational(0, 1) ** Rational(-2,3) }
   end
 
+  def test_power_overflow
+    bug = '[ruby-core:79686] [Bug #13242]: Infinity due to overflow'
+    x = EnvUtil.suppress_warning {4r**40000000}
+    assert_predicate x, :infinite?, bug
+  end
+
   def test_positive_p
     assert_predicate(1/2r, :positive?)
     assert_not_predicate(-1/2r, :positive?)
Index: rational.c
===================================================================
--- rational.c	(revision 57687)
+++ rational.c	(revision 57688)
@@ -1042,6 +1042,10 @@ nurat_expt(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/rational.c#L1042
 		num = ONE;
 		den = ONE;
 	    }
+	    if (RB_FLOAT_TYPE_P(num)) { /* infinity due to overflow */
+		if (RB_FLOAT_TYPE_P(den)) return DBL2NUM(NAN);
+		return num;
+	    }
 	    return f_rational_new2(CLASS_OF(self), num, den);
 	}
     }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]