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

ruby-changes:26777

From: usa <ko1@a...>
Date: Tue, 15 Jan 2013 16:23:01 +0900 (JST)
Subject: [ruby-changes:26777] usa:r38829 (ruby_1_9_3): merge revision(s) 38801,38809,38810: [Backport #7404]

usa	2013-01-15 16:22:50 +0900 (Tue, 15 Jan 2013)

  New Revision: 38829

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=38829

  Log:
    merge revision(s) 38801,38809,38810: [Backport #7404]
    
    * ext/bigdecimal/bigdecimal.c (BigDecimal_sub):
      need to specify precision for converting Rational and Float.
      [ruby-dev:46544] [Bug #7404]
    
    * ext/bigdecimal/bigdecimal.c (BigDecimal_mult): ditto.
    
    * ext/bigdecimal/bigdecimal.c (BigDecimal_divide): ditto.
    
    * ext/bigdecimal/bigdecimal.c (BigDecimal_DoDivmod): ditto.
    
    * ext/bigdecimal/bigdecimal.c (BigDecimal_divremain): ditto.
    
    * test/bigdecimal/test_bigdecimal.rb: add tests for the above fixes.
    
    * ext/bigdecimal/bigdecimal.c: use `RB_TYPE_P(x, t)` instead of
      `TYPE(x) == t`.
    
    * ext/bigdecimal/bigdecimal.c (BigDecimal_to_s): use CRuby style.

  Modified directories:
    branches/ruby_1_9_3/
  Modified files:
    branches/ruby_1_9_3/ChangeLog
    branches/ruby_1_9_3/ext/bigdecimal/bigdecimal.c
    branches/ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb
    branches/ruby_1_9_3/version.h

Index: ruby_1_9_3/ChangeLog
===================================================================
--- ruby_1_9_3/ChangeLog	(revision 38828)
+++ ruby_1_9_3/ChangeLog	(revision 38829)
@@ -1,3 +1,28 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ChangeLog#L1
+Tue Jan 15 16:13:47 2013  Kenta Murata  <mrkn@m...>
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_to_s): use CRuby style.
+
+Tue Jan 15 16:13:47 2013  Kenta Murata  <mrkn@m...>
+
+	* ext/bigdecimal/bigdecimal.c: use `RB_TYPE_P(x, t)` instead of
+	  `TYPE(x) == t`.
+
+Tue Jan 15 16:13:47 2013  Kenta Murata  <mrkn@m...>
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_sub):
+	  need to specify precision for converting Rational and Float.
+	  [ruby-dev:46544] [Bug #7404]
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_mult): ditto.
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_divide): ditto.
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_DoDivmod): ditto.
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_divremain): ditto.
+
+	* test/bigdecimal/test_bigdecimal.rb: add tests for the above fixes.
+
 Tue Jan 15 16:03:30 2013  Nobuyoshi Nakada  <nobu@r...>
 
 	* tool/mkconfig.rb: use configured libdir value to fix
Index: ruby_1_9_3/ext/bigdecimal/bigdecimal.c
===================================================================
--- ruby_1_9_3/ext/bigdecimal/bigdecimal.c	(revision 38828)
+++ ruby_1_9_3/ext/bigdecimal/bigdecimal.c	(revision 38829)
@@ -660,7 +660,7 @@ BigDecimal_to_i(VALUE self) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L660
 	    ret = rb_funcall(numerator, '*', 1,
 			     rb_funcall(INT2FIX(10), rb_intern("**"), 1,
 					INT2FIX(dpower)));
-	if (TYPE(ret) == T_FLOAT)
+	if (RB_TYPE_P(ret, T_FLOAT))
 	    rb_raise(rb_eFloatDomainError, "Infinity");
 	return ret;
     }
@@ -768,11 +768,11 @@ BigDecimal_coerce(VALUE self, VALUE othe https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L768
     VALUE obj;
     Real *b;
 
-    if (TYPE(other) == T_FLOAT) {
+    if (RB_TYPE_P(other, T_FLOAT)) {
 	obj = rb_assoc_new(other, BigDecimal_to_f(self));
     }
     else {
-	if (TYPE(other) == T_RATIONAL) {
+	if (RB_TYPE_P(other, T_RATIONAL)) {
 	    Real* pv = DATA_PTR(self);
 	    GUARD_OBJ(b, GetVpValueWithPrec(other, pv->Prec*VpBaseFig(), 1));
 	}
@@ -810,10 +810,10 @@ BigDecimal_add(VALUE self, VALUE r) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L810
     size_t mx;
 
     GUARD_OBJ(a, GetVpValue(self, 1));
-    if (TYPE(r) == T_FLOAT) {
+    if (RB_TYPE_P(r, T_FLOAT)) {
 	b = GetVpValueWithPrec(r, DBL_DIG+1, 1);
     }
-    else if (TYPE(r) == T_RATIONAL) {
+    else if (RB_TYPE_P(r, T_RATIONAL)) {
 	b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1);
     }
     else {
@@ -862,7 +862,16 @@ BigDecimal_sub(VALUE self, VALUE r) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L862
     size_t mx;
 
     GUARD_OBJ(a,GetVpValue(self,1));
-    b = GetVpValue(r,0);
+    if (RB_TYPE_P(r, T_FLOAT)) {
+	b = GetVpValueWithPrec(r, DBL_DIG+1, 1);
+    }
+    else if (RB_TYPE_P(r, T_RATIONAL)) {
+	b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1);
+    }
+    else {
+	b = GetVpValue(r,0);
+    }
+
     if(!b) return DoSomeOne(self,r,'-');
     SAVE(b);
 
@@ -1095,7 +1104,16 @@ BigDecimal_mult(VALUE self, VALUE r) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1104
     size_t mx;
 
     GUARD_OBJ(a,GetVpValue(self,1));
-    b = GetVpValue(r,0);
+    if (RB_TYPE_P(r, T_FLOAT)) {
+	b = GetVpValueWithPrec(r, DBL_DIG+1, 1);
+    }
+    else if (RB_TYPE_P(r, T_RATIONAL)) {
+	b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1);
+    }
+    else {
+	b = GetVpValue(r,0);
+    }
+
     if(!b) return DoSomeOne(self,r,'*');
     SAVE(b);
 
@@ -1114,9 +1132,19 @@ BigDecimal_divide(Real **c, Real **res, https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1132
     size_t mx;
 
     GUARD_OBJ(a,GetVpValue(self,1));
-    b = GetVpValue(r,0);
+    if (RB_TYPE_P(r, T_FLOAT)) {
+	b = GetVpValueWithPrec(r, DBL_DIG+1, 1);
+    }
+    else if (RB_TYPE_P(r, T_RATIONAL)) {
+	b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1);
+    }
+    else {
+	b = GetVpValue(r,0);
+    }
+
     if(!b) return DoSomeOne(self,r,'/');
     SAVE(b);
+
     *div = b;
     mx = a->Prec + vabs(a->exponent);
     if(mx<b->Prec + vabs(b->exponent)) mx = b->Prec + vabs(b->exponent);
@@ -1177,7 +1205,16 @@ BigDecimal_DoDivmod(VALUE self, VALUE r, https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1205
     size_t mx;
 
     GUARD_OBJ(a,GetVpValue(self,1));
-    b = GetVpValue(r,0);
+    if (RB_TYPE_P(r, T_FLOAT)) {
+	b = GetVpValueWithPrec(r, DBL_DIG+1, 1);
+    }
+    else if (RB_TYPE_P(r, T_RATIONAL)) {
+	b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1);
+    }
+    else {
+	b = GetVpValue(r,0);
+    }
+
     if(!b) return Qfalse;
     SAVE(b);
 
@@ -1267,7 +1304,16 @@ BigDecimal_divremain(VALUE self, VALUE r https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1304
     Real *f=NULL;
 
     GUARD_OBJ(a,GetVpValue(self,1));
-    b = GetVpValue(r,0);
+    if (RB_TYPE_P(r, T_FLOAT)) {
+	b = GetVpValueWithPrec(r, DBL_DIG+1, 1);
+    }
+    else if (RB_TYPE_P(r, T_RATIONAL)) {
+	b = GetVpValueWithPrec(r, a->Prec*VpBaseFig(), 1);
+    }
+    else {
+	b = GetVpValue(r,0);
+    }
+
     if(!b) return DoSomeOne(self,r,rb_intern("remainder"));
     SAVE(b);
 
@@ -1741,8 +1787,8 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1787
 BigDecimal_to_s(int argc, VALUE *argv, VALUE self)
 {
     ENTER(5);
-    int   fmt=0;   /* 0:E format */
-    int   fPlus=0; /* =0:default,=1: set ' ' before digits ,set '+' before digits. */
+    int   fmt = 0;   /* 0:E format */
+    int   fPlus = 0; /* =0:default,=1: set ' ' before digits ,set '+' before digits. */
     Real  *vp;
     volatile VALUE str;
     char  *psz;
@@ -1752,42 +1798,53 @@ BigDecimal_to_s(int argc, VALUE *argv, V https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1798
 
     GUARD_OBJ(vp,GetVpValue(self,1));
 
-    if(rb_scan_args(argc,argv,"01",&f)==1) {
-        if(TYPE(f)==T_STRING) {
-            SafeStringValue(f);
-            psz = RSTRING_PTR(f);
-            if(*psz==' ') {
-                fPlus = 1; psz++;
-            } else if(*psz=='+') {
-                fPlus = 2; psz++;
-            }
-            while((ch=*psz++)!=0) {
-                if(ISSPACE(ch)) continue;
-                if(!ISDIGIT(ch)) {
-                    if(ch=='F' || ch=='f') fmt = 1; /* F format */
-                    break;
-                }
-                mc = mc * 10 + ch - '0';
-            }
-        }
+    if (rb_scan_args(argc,argv,"01",&f)==1) {
+	if (RB_TYPE_P(f, T_STRING)) {
+	    SafeStringValue(f);
+	    psz = RSTRING_PTR(f);
+	    if (*psz == ' ') {
+		fPlus = 1;
+		psz++;
+	    }
+	    else if (*psz == '+') {
+		fPlus = 2;
+		psz++;
+	    }
+	    while ((ch = *psz++) != 0) {
+		if (ISSPACE(ch)) {
+		    continue;
+		}
+		if (!ISDIGIT(ch)) {
+		    if (ch == 'F' || ch == 'f') {
+			fmt = 1; /* F format */
+		    }
+		    break;
+		}
+		mc = mc * 10 + ch - '0';
+	    }
+	}
 	else {
-            mc = (size_t)GetPositiveInt(f);
-        }
+	    mc = (size_t)GetPositiveInt(f);
+	}
     }
-    if(fmt) {
-        nc = VpNumOfChars(vp,"F");
-    } else {
-        nc = VpNumOfChars(vp,"E");
+    if (fmt) {
+	nc = VpNumOfChars(vp, "F");
+    }
+    else {
+	nc = VpNumOfChars(vp, "E");
+    }
+    if (mc > 0) {
+	nc += (nc + mc - 1) / mc + 1;
     }
-    if(mc>0) nc += (nc + mc - 1) / mc + 1;
 
     str = rb_str_new(0, nc);
     psz = RSTRING_PTR(str);
 
-    if(fmt) {
-        VpToFString(vp, psz, mc, fPlus);
-    } else {
-        VpToString (vp, psz, mc, fPlus);
+    if (fmt) {
+	VpToFString(vp, psz, mc, fPlus);
+    }
+    else {
+	VpToString (vp, psz, mc, fPlus);
     }
     rb_str_resize(str, strlen(psz));
     return str;
@@ -1904,7 +1961,7 @@ static VALUE BigMath_s_log(VALUE, VALUE, https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1961
 inline static int
 is_integer(VALUE x)
 {
-    return (TYPE(x) == T_FIXNUM || TYPE(x) == T_BIGNUM);
+    return (RB_TYPE_P(x, T_FIXNUM) || RB_TYPE_P(x, T_BIGNUM));
 }
 
 inline static int
@@ -1913,10 +1970,10 @@ is_negative(VALUE x) https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L1970
     if (FIXNUM_P(x)) {
 	return FIX2LONG(x) < 0;
     }
-    else if (TYPE(x) == T_BIGNUM) {
+    else if (RB_TYPE_P(x, T_BIGNUM)) {
 	return RBIGNUM_NEGATIVE_P(x);
     }
-    else if (TYPE(x) == T_FLOAT) {
+    else if (RB_TYPE_P(x, T_FLOAT)) {
 	return RFLOAT_VALUE(x) < 0.0;
     }
     return RTEST(rb_funcall(x, '<', 1, INT2FIX(0)));
@@ -2175,7 +2232,7 @@ retry: https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L2232
     if (exp != NULL) {
 	return rmpd_power_by_big_decimal(x, exp, n);
     }
-    else if (TYPE(vexp) == T_BIGNUM) {
+    else if (RB_TYPE_P(vexp, T_BIGNUM)) {
 	VALUE abs_value = BigDecimal_abs(self);
 	if (is_one(abs_value)) {
 	    return ToValue(VpCreateRbObject(n, "1"));
@@ -2571,7 +2628,7 @@ BigMath_s_log(VALUE klass, VALUE x, VALU https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/ext/bigdecimal/bigdecimal.c#L2628
     double flo;
     long fix;
 
-    if (TYPE(vprec) != T_FIXNUM && TYPE(vprec) != T_BIGNUM) {
+    if (!is_integer(vprec)) {
 	rb_raise(rb_eArgError, "precision must be an Integer");
     }
 
Index: ruby_1_9_3/version.h
===================================================================
--- ruby_1_9_3/version.h	(revision 38828)
+++ ruby_1_9_3/version.h	(revision 38829)
@@ -1,5 +1,5 @@ https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/version.h#L1
 #define RUBY_VERSION "1.9.3"
-#define RUBY_PATCHLEVEL 369
+#define RUBY_PATCHLEVEL 370
 
 #define RUBY_RELEASE_DATE "2013-01-15"
 #define RUBY_RELEASE_YEAR 2013
Index: ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb
===================================================================
--- ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb	(revision 38828)
+++ ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb	(revision 38829)
@@ -597,6 +597,14 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb#L597
     assert_equal(BigDecimal.new((2**100-1).to_s), x - 1)
   end
 
+  def test_sub_with_float
+    assert_kind_of(BigDecimal, BigDecimal.new("3") - 1.0)
+  end
+
+  def test_sub_with_rational
+    assert_kind_of(BigDecimal, BigDecimal.new("3") - 1.quo(3))
+  end
+
   def test_mult
     x = BigDecimal.new((2**100).to_s)
     assert_equal(BigDecimal.new((2**100 * 3).to_s), (x * 3).to_i)
@@ -605,6 +613,14 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb#L613
     assert_equal(BigDecimal.new((2**200).to_s), (x * x).to_i)
   end
 
+  def test_mult_with_float
+    assert_kind_of(BigDecimal, BigDecimal.new("3") * 1.5)
+  end
+
+  def test_mult_with_rational
+    assert_kind_of(BigDecimal, BigDecimal.new("3") * 1.quo(3))
+  end
+
   def test_div
     x = BigDecimal.new((2**100).to_s)
     assert_equal(BigDecimal.new((2**100 / 3).to_s), (x / 3).to_i)
@@ -614,6 +630,14 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb#L630
     assert_equal(-2, BigDecimal.new("2") / -1)
   end
 
+  def test_div_with_float
+    assert_kind_of(BigDecimal, BigDecimal.new("3") / 1.5)
+  end
+
+  def test_div_with_rational
+    assert_kind_of(BigDecimal, BigDecimal.new("3") / 1.quo(3))
+  end
+
   def test_mod
     x = BigDecimal.new((2**100).to_s)
     assert_equal(1, x % 3)
@@ -622,6 +646,14 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb#L646
     assert_equal(-1, (-x) % -3)
   end
 
+  def test_mod_with_float
+    assert_kind_of(BigDecimal, BigDecimal.new("3") % 1.5)
+  end
+
+  def test_mod_with_rational
+    assert_kind_of(BigDecimal, BigDecimal.new("3") % 1.quo(3))
+  end
+
   def test_remainder
     x = BigDecimal.new((2**100).to_s)
     assert_equal(1, x.remainder(3))
@@ -630,6 +662,14 @@ class TestBigDecimal < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/ruby_1_9_3/test/bigdecimal/test_bigdecimal.rb#L662
     assert_equal(-1, (-x).remainder(-3))
   end
 
+  def test_remainder_with_float
+    assert_kind_of(BigDecimal, BigDecimal.new("3").remainder(1.5))
+  end
+
+  def test_remainder_with_rational
+    assert_kind_of(BigDecimal, BigDecimal.new("3").remainder(1.quo(3)))
+  end
+
   def test_divmod
     x = BigDecimal.new((2**100).to_s)
     assert_equal([(x / 3).floor, 1], x.divmod(3))

Property changes on: ruby_1_9_3
___________________________________________________________________
Modified: svn:mergeinfo
   Merged /trunk:r38801,38809-38810


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

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