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

ruby-changes:19817

From: mrkn <ko1@a...>
Date: Tue, 31 May 2011 22:45:38 +0900 (JST)
Subject: [ruby-changes:19817] mrkn:r31863 (trunk): * ext/bigdecimal/bigdecimal.c (BigDecimal_new): support instantiation a

mrkn	2011-05-31 22:45:31 +0900 (Tue, 31 May 2011)

  New Revision: 31863

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

  Log:
    * ext/bigdecimal/bigdecimal.c (BigDecimal_new): support instantiation a
      BigDecimal object from an Integer.
    * test/bigdecimal/test_bigdecimal.rb (test_new_with_integer):
      add for testing the above change.
    * ext/bigdecimal/bigdecimal.c (BigDecimal_global_new): replace its body
      with a BigDecimal_new call.
    * test/bigdecimal/test_bigdecimal.rb (test_global_new_with_integer):
      add for testing the above change.

  Modified files:
    trunk/ChangeLog
    trunk/ext/bigdecimal/bigdecimal.c
    trunk/test/bigdecimal/test_bigdecimal.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 31862)
+++ ChangeLog	(revision 31863)
@@ -1,3 +1,17 @@
+Tue May 31 22:44:00 2011  Kenta Murata  <mrkn@m...>
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_new): support instantiation a
+	  BigDecimal object from an Integer.
+
+	* test/bigdecimal/test_bigdecimal.rb (test_new_with_integer):
+	  add for testing the above change.
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_global_new): replace its body
+	  with a BigDecimal_new call.
+
+	* test/bigdecimal/test_bigdecimal.rb (test_global_new_with_integer):
+	  add for testing the above change.
+
 Tue May 31 22:24:39 2011  Tadayoshi Funaba  <tadf@d...>
 
 	* ext/date/date_core.c: use simple/complex mode instead of light/right mode.
Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 31862)
+++ ext/bigdecimal/bigdecimal.c	(revision 31863)
@@ -1738,8 +1738,23 @@
     return ToValue(y);
 }
 
+/* call-seq:
+ *   new(initial, digits)
+ *
+ * Create a new BigDecimal object.
+ *
+ * initial:: The initial value, as a String. Spaces are ignored, unrecognized
+ *           characters terminate the value.
+ *
+ * digits:: The number of significant digits, as a Fixnum. If omitted or 0,
+ *          the number of significant digits is determined from the initial
+ *          value.
+ *
+ * The actual number of significant digits used in computation is usually
+ * larger than the specified number.
+ */
 static VALUE
-BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
+BigDecimal_new(int argc, VALUE *argv, VALUE self)
 {
     ENTER(5);
     Real *pv;
@@ -1747,45 +1762,34 @@
     VALUE  nFig;
     VALUE  iniValue;
 
-    if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
-	mf = 0;
-    } else {
-	mf = GetPositiveInt(nFig);
+    if (rb_scan_args(argc, argv, "11", &iniValue, &nFig) == 1) {
+        mf = 0;
     }
+    else {
+        mf = GetPositiveInt(nFig);
+    }
+
+    switch (TYPE(iniValue)) {
+      case T_FIXNUM:
+	/* fall through */
+      case T_BIGNUM:
+	return ToValue(GetVpValue(iniValue, 1));
+
+      case T_STRING:
+	/* fall through */
+      default:
+	break;
+    }
     SafeStringValue(iniValue);
-    GUARD_OBJ(pv,VpCreateRbObject(mf, RSTRING_PTR(iniValue)));
+    GUARD_OBJ(pv, VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
+
     return ToValue(pv);
 }
 
- /* call-seq:
-  * new(initial, digits)
-  *
-  * Create a new BigDecimal object.
-  *
-  * initial:: The initial value, as a String. Spaces are ignored, unrecognized characters terminate the value.
-  *
-  * digits:: The number of significant digits, as a Fixnum. If omitted or 0, the number of significant digits is determined from the initial value.
-  *
-  * The actual number of significant digits used in computation is usually
-  * larger than the specified number.
-  */
 static VALUE
-BigDecimal_new(int argc, VALUE *argv, VALUE self)
+BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
 {
-    ENTER(5);
-    Real *pv;
-    size_t mf;
-    VALUE  nFig;
-    VALUE  iniValue;
-
-    if(rb_scan_args(argc,argv,"11",&iniValue,&nFig)==1) {
-        mf = 0;
-    } else {
-        mf = GetPositiveInt(nFig);
-    }
-    SafeStringValue(iniValue);
-    GUARD_OBJ(pv,VpNewRbClass(mf, RSTRING_PTR(iniValue),self));
-    return ToValue(pv);
+    return BigDecimal_new(argc, argv, rb_cBigDecimal);
 }
 
  /* call-seq:
Index: test/bigdecimal/test_bigdecimal.rb
===================================================================
--- test/bigdecimal/test_bigdecimal.rb	(revision 31862)
+++ test/bigdecimal/test_bigdecimal.rb	(revision 31863)
@@ -29,6 +29,13 @@
     assert_raise(ArgumentError) { BigDecimal("1", -1) }
   end
 
+  def test_global_new_with_integer
+    assert_equal(BigDecimal("1"), BigDecimal(1))
+    assert_equal(BigDecimal("-1"), BigDecimal(-1))
+    assert_equal(BigDecimal((2**100).to_s), BigDecimal(2**100))
+    assert_equal(BigDecimal((-2**100).to_s), BigDecimal(-2**100))
+  end
+
   def test_new
     assert_equal(1, BigDecimal.new("1"))
     assert_equal(1, BigDecimal.new("1", 1))
@@ -44,6 +51,13 @@
     assert_equal( 1, BigDecimal.new("1E1111111111111111111").infinite?)
   end
 
+  def test_new_with_integer
+    assert_equal(BigDecimal("1"), BigDecimal.new(1))
+    assert_equal(BigDecimal("-1"), BigDecimal.new(-1))
+    assert_equal(BigDecimal((2**100).to_s), BigDecimal.new(2**100))
+    assert_equal(BigDecimal((-2**100).to_s), BigDecimal.new(-2**100))
+  end
+
   def _test_mode(type)
     BigDecimal.mode(type, true)
     assert_raise(FloatDomainError) { yield }

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

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