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

ruby-changes:19822

From: mrkn <ko1@a...>
Date: Wed, 1 Jun 2011 00:09:45 +0900 (JST)
Subject: [ruby-changes:19822] mrkn:r31868 (trunk): * ext/bigdecimal/bigdecimal.c (BigDecimal_coerce): support coerce with a

mrkn	2011-06-01 00:09:38 +0900 (Wed, 01 Jun 2011)

  New Revision: 31868

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

  Log:
    * ext/bigdecimal/bigdecimal.c (BigDecimal_coerce): support coerce with a
      Rational.  The precision used for instantiate a BigDecimal from the
      given Rational is obtained from the receiver BigDecimal.
    * test/bigdecimal/test_bigdecimal.rb (test_coerce): add a test for the
      above change.

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

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 31867)
+++ ChangeLog	(revision 31868)
@@ -1,3 +1,12 @@
+Tue Jun  1 00:07:00 2011  Kenta Murata  <mrkn@m...>
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_coerce): support coerce with a
+	  Rational.  The precision used for instantiate a BigDecimal from the
+	  given Rational is obtained from the receiver BigDecimal.
+
+	* test/bigdecimal/test_bigdecimal.rb (test_coerce): add a test for the
+	  above change.
+
 Tue May 31 23:49:08 2011  Tadayoshi Funaba  <tadf@d...>
 
 	* ext/date/date_core.c (offset_to_sec): fixed invalid validation.
Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 31867)
+++ ext/bigdecimal/bigdecimal.c	(revision 31868)
@@ -675,12 +675,21 @@
     ENTER(2);
     VALUE obj;
     Real *b;
+
     if (TYPE(other) == T_FLOAT) {
 	obj = rb_assoc_new(other, BigDecimal_to_f(self));
-    } else {
-	GUARD_OBJ(b,GetVpValue(other,1));
+    }
+    else {
+	if (TYPE(other) == T_RATIONAL) {
+	    Real* pv = DATA_PTR(self);
+	    GUARD_OBJ(b, GetVpValueWithPrec(other, pv->Prec*VpBaseFig(), 1));
+	}
+	else {
+	    GUARD_OBJ(b, GetVpValue(other, 1));
+	}
 	obj = rb_assoc_new(b->obj, self);
     }
+
     return obj;
 }
 
Index: test/bigdecimal/test_bigdecimal.rb
===================================================================
--- test/bigdecimal/test_bigdecimal.rb	(revision 31867)
+++ test/bigdecimal/test_bigdecimal.rb	(revision 31868)
@@ -485,6 +485,12 @@
     assert_instance_of(Float, a)
     assert_instance_of(Float, b)
     assert_equal(2, 1 + BigDecimal.new("1"), '[ruby-core:25697]')
+
+    a, b = BigDecimal("1").coerce(1.quo(10))
+    assert_equal(BigDecimal("0.1"), a, '[ruby-core:34318]')
+
+    a, b = BigDecimal("0.11111").coerce(1.quo(3))
+    assert_equal(BigDecimal("0." + "3"*a.precs[0]), a)
   end
 
   def test_uplus

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

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