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

ruby-changes:8656

From: matz <ko1@a...>
Date: Tue, 11 Nov 2008 19:45:45 +0900 (JST)
Subject: [ruby-changes:8656] Ruby:r20191 (trunk): * ext/bigdecimal/bigdecimal.c (BigDecimal_to_r): moved from

matz	2008-11-11 19:45:21 +0900 (Tue, 11 Nov 2008)

  New Revision: 20191

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

  Log:
    * ext/bigdecimal/bigdecimal.c (BigDecimal_to_r): moved from
      bigdecimal/util, converted into C.  [ruby-dev:36805]

  Modified files:
    trunk/ChangeLog
    trunk/ext/bigdecimal/bigdecimal.c
    trunk/ext/bigdecimal/lib/bigdecimal/util.rb

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 20190)
+++ ChangeLog	(revision 20191)
@@ -22,6 +22,11 @@
 	* test/ruby/test_transcode.rb: added tests for the above
 	  (from Yoshihiro Kambayashi)
 
+Tue Nov 11 13:20:23 2008  Yukihiro Matsumoto  <matz@r...>
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_to_r): moved from
+	  bigdecimal/util, converted into C.  [ruby-dev:36805]
+
 Tue Nov 11 07:55:57 2008  Tadayoshi Funaba  <tadf@d...>
 
 	* lib/date.rb (inspect): changed the format.
Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 20190)
+++ ext/bigdecimal/bigdecimal.c	(revision 20191)
@@ -606,6 +606,41 @@
     return rb_float_new(d);
 }
 
+
+static VALUE BigDecimal_split(VALUE self);
+
+/* Converts a BigDecimal to a Rational.
+ */
+static VALUE
+BigDecimal_to_r(VALUE self)
+{
+    Real *p;
+    S_LONG sign, power, denomi_power;
+    VALUE a, digits, numerator;
+
+    p = GetVpValue(self,1);
+    sign = VpGetSign(p);
+    power = VpExponent10(p);
+    a = BigDecimal_split(self);
+    digits = RARRAY_PTR(a)[1];
+    denomi_power = power - RSTRING_LEN(digits);
+    numerator = rb_funcall(digits, rb_intern("to_i"), 0);
+    
+    if (sign < 0) {
+	numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1));
+    }
+    if (denomi_power < 0) {
+	return rb_Rational(numerator,
+			   rb_funcall(INT2FIX(10), rb_intern("**"), 1,
+				      INT2FIX(-denomi_power)));
+    }
+    else {
+        return rb_Rational1(rb_funcall(numerator, '*', 1,
+				       rb_funcall(INT2FIX(10), rb_intern("**"), 1,
+						  INT2FIX(denomi_power))));
+    }
+}
+
 /* The coerce method provides support for Ruby type coercion. It is not
  * enabled by default.
  * 
@@ -1918,6 +1953,7 @@
     rb_define_method(rb_cBigDecimal, "to_s", BigDecimal_to_s, -1);
     rb_define_method(rb_cBigDecimal, "to_i", BigDecimal_to_i, 0);
     rb_define_method(rb_cBigDecimal, "to_int", BigDecimal_to_i, 0);
+    rb_define_method(rb_cBigDecimal, "to_r", BigDecimal_to_r, 0);
     rb_define_method(rb_cBigDecimal, "split", BigDecimal_split, 0);
     rb_define_method(rb_cBigDecimal, "+", BigDecimal_add, 1);
     rb_define_method(rb_cBigDecimal, "-", BigDecimal_sub, 1);
Index: ext/bigdecimal/lib/bigdecimal/util.rb
===================================================================
--- ext/bigdecimal/lib/bigdecimal/util.rb	(revision 20190)
+++ ext/bigdecimal/lib/bigdecimal/util.rb	(revision 20191)
@@ -39,18 +39,6 @@
        i + "." + ("0"*(-z)) + f
      end
   end
-
-  # Converts a BigDecimal to a Rational.
-  def to_r 
-     sign,digits,base,power = self.split
-     numerator = sign*digits.to_i
-     denomi_power = power - digits.size # base is always 10
-     if denomi_power < 0
-        Rational(numerator,base ** (-denomi_power))
-     else
-        Rational(numerator * (base ** denomi_power),1)
-     end
-  end
 end
 
 class Rational < Numeric

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

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