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

ruby-changes:14616

From: matz <ko1@a...>
Date: Fri, 29 Jan 2010 01:35:09 +0900 (JST)
Subject: [ruby-changes:14616] Ruby:r26461 (trunk): * ext/bigdecimal/bigdecimal.c (BigDecimal_to_i): update RDoc to

matz	2010-01-29 01:34:11 +0900 (Fri, 29 Jan 2010)

  New Revision: 26461

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

  Log:
    * ext/bigdecimal/bigdecimal.c (BigDecimal_to_i): update RDoc to
      denote that #to_i raises FloatDomainError for Inf and NaN.
    
    * ext/bigdecimal/bigdecimal.c (BigDecimal_to_i): fast #to_i using
      BigDecimal_split(). 
    
    * bignum.c (conv_digit): use faster ISDIGIT() assuming ASCII.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/ext/bigdecimal/bigdecimal.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 26460)
+++ ChangeLog	(revision 26461)
@@ -1,3 +1,13 @@
+Fri Jan 29 01:26:53 2010  Yukihiro Matsumoto  <matz@r...>
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_to_i): update RDoc to
+	  denote that #to_i raises FloatDomainError for Inf and NaN.
+
+	* ext/bigdecimal/bigdecimal.c (BigDecimal_to_i): fast #to_i using
+	  BigDecimal_split(). 
+
+	* bignum.c (conv_digit): use faster ISDIGIT() assuming ASCII.
+
 Fri Jan 29 00:18:54 2010  Yusuke Endoh  <mame@t...>
 
 	* lib/cgi.rb: set autoload to CGI::HtmlExtension.  [ruby-dev:40194]
Index: ext/bigdecimal/bigdecimal.c
===================================================================
--- ext/bigdecimal/bigdecimal.c	(revision 26460)
+++ ext/bigdecimal/bigdecimal.c	(revision 26461)
@@ -473,9 +473,11 @@
     }
 }
 
+static VALUE BigDecimal_split(VALUE self);
+
 /* Returns the value as an integer (Fixnum or Bignum).
  *
- * If the BigNumber is infinity or NaN, returns nil.
+ * If the BigNumber is infinity or NaN, raises FloatDomainError.
  */
 static VALUE
 BigDecimal_to_i(VALUE self)
@@ -497,31 +499,24 @@
         e = VpGetSign(p)*p->frac[0];
         return INT2FIX(e);
     }
-    str = rb_str_new(0, e+nf+2);
-    psz = RSTRING_PTR(str);
+    else {
+	VALUE a = BigDecimal_split(self);
+	VALUE digits = RARRAY_PTR(a)[1];
+	VALUE numerator = rb_funcall(digits, rb_intern("to_i"), 0);
+	int dpower = e - RSTRING_LEN(digits);
 
-    n = (e+nf-1)/nf;
-    pch = psz;
-    if(VpGetSign(p)<0) *pch++ = '-';
-    for(i=0;i<n;++i) {
-        b = VpBaseVal()/10;
-        if(i>=(int)p->Prec) {
-            while(b) {
-                *pch++ = '0';
-                b /= 10;
-            }
-            continue;
-        }
-        v = p->frac[i];
-        while(b) {
-            j = v/b;
-            *pch++ = (char)(j + '0');
-            v -= j*b;
-            b /= 10;
-        }
+	if (VpGetSign(p) < 0) {
+	    numerator = rb_funcall(numerator, '*', 1, INT2FIX(-1));
+	}
+	if (dpower < 0) {
+	    return rb_funcall(numerator, rb_intern("div"), 1,
+			      rb_funcall(INT2FIX(10), rb_intern("**"), 1,
+					 INT2FIX(-dpower)));
+	}
+        return rb_funcall(numerator, '*', 1,
+			  rb_funcall(INT2FIX(10), rb_intern("**"), 1,
+				     INT2FIX(dpower)));
     }
-    *pch++ = 0;
-    return rb_cstr2inum(psz,10);
 }
 
 /* Returns a new Float object having approximately the same value as the
@@ -556,8 +551,6 @@
 }
 
 
-static VALUE BigDecimal_split(VALUE self);
-
 /* Converts a BigDecimal to a Rational.
  */
 static VALUE
Index: bignum.c
===================================================================
--- bignum.c	(revision 26460)
+++ bignum.c	(revision 26461)
@@ -437,6 +437,8 @@
     VALUE z;
     BDIGIT *zds;
 
+#undef ISDIGIT
+#define ISDIGIT(c) ('0' <= (c) && (c) <= '9')
 #define conv_digit(c) \
     (!ISASCII(c) ? -1 : \
      ISDIGIT(c) ? ((c) - '0') : \

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

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