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

ruby-changes:16405

From: akr <ko1@a...>
Date: Tue, 22 Jun 2010 23:13:34 +0900 (JST)
Subject: [ruby-changes:16405] Ruby:r28389 (trunk, ruby_1_9_2): * time.c (num_exact): fix for mathn.

akr	2010-06-22 23:13:09 +0900 (Tue, 22 Jun 2010)

  New Revision: 28389

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

  Log:
    * time.c (num_exact): fix for mathn.  [ruby-dev:41599]

  Modified files:
    branches/ruby_1_9_2/ChangeLog
    branches/ruby_1_9_2/time.c
    trunk/ChangeLog
    trunk/time.c

Index: time.c
===================================================================
--- time.c	(revision 28388)
+++ time.c	(revision 28389)
@@ -656,35 +656,50 @@
 static VALUE
 num_exact(VALUE v)
 {
-    switch (TYPE(v)) {
+    VALUE tmp;
+    int t;
+
+    t = TYPE(v);
+    switch (t) {
       case T_FIXNUM:
       case T_BIGNUM:
+        return v;
+
       case T_RATIONAL:
         break;
 
-      case T_FLOAT:
-        v = rb_convert_type(v, T_RATIONAL, "Rational", "to_r");
-        break;
-
       case T_STRING:
       case T_NIL:
         goto typeerror;
 
-      default: {
-        VALUE tmp;
-        if (!NIL_P(tmp = rb_check_convert_type(v, T_RATIONAL, "Rational", "to_r"))) {
-	    if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
+      default:
+        if ((tmp = rb_check_funcall(v, rb_intern("to_r"), 0, NULL)) != Qundef) {
+            if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
             v = tmp;
-	}
-        else if (!NIL_P(tmp = rb_check_to_integer(v, "to_int")))
+            break;
+        }
+        if (!NIL_P(tmp = rb_check_to_integer(v, "to_int"))) {
             v = tmp;
-        else {
-          typeerror:
-            rb_raise(rb_eTypeError, "can't convert %s into an exact number",
-		                    NIL_P(v) ? "nil" : rb_obj_classname(v));
+            break;
         }
+        goto typeerror;
+    }
+
+    t = TYPE(v);
+    switch (t) {
+      case T_FIXNUM:
+      case T_BIGNUM:
+        return v;
+
+      case T_RATIONAL:
+        if (RRATIONAL(v)->den == INT2FIX(1))
+            v = RRATIONAL(v)->num;
         break;
-      }
+
+      default:
+      typeerror:
+        rb_raise(rb_eTypeError, "can't convert %s into an exact number",
+                                NIL_P(v) ? "nil" : rb_obj_classname(v));
     }
     return v;
 }
@@ -3140,7 +3155,7 @@
     GetTimeval(time, tobj);
     v = w2v(rb_time_unmagnify(tobj->timew));
     if (TYPE(v) != T_RATIONAL) {
-        v = rb_convert_type(v, T_RATIONAL, "Rational", "to_r");
+        v = rb_Rational1(v);
     }
     return v;
 }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 28388)
+++ ChangeLog	(revision 28389)
@@ -1,3 +1,7 @@
+Tue Jun 22 23:10:10 2010  Tanaka Akira  <akr@f...>
+
+	* time.c (num_exact): fix for mathn.  [ruby-dev:41599]
+
 Tue Jun 22 22:00:06 2010  Tanaka Akira  <akr@f...>
 
 	* ext/socket/lib/socket.rb: suppress warnings.
Index: ruby_1_9_2/time.c
===================================================================
--- ruby_1_9_2/time.c	(revision 28388)
+++ ruby_1_9_2/time.c	(revision 28389)
@@ -656,35 +656,50 @@
 static VALUE
 num_exact(VALUE v)
 {
-    switch (TYPE(v)) {
+    VALUE tmp;
+    int t;
+
+    t = TYPE(v);
+    switch (t) {
       case T_FIXNUM:
       case T_BIGNUM:
+        return v;
+
       case T_RATIONAL:
         break;
 
-      case T_FLOAT:
-        v = rb_convert_type(v, T_RATIONAL, "Rational", "to_r");
-        break;
-
       case T_STRING:
       case T_NIL:
         goto typeerror;
 
-      default: {
-        VALUE tmp;
-        if (!NIL_P(tmp = rb_check_convert_type(v, T_RATIONAL, "Rational", "to_r"))) {
-	    if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
+      default:
+        if ((tmp = rb_check_funcall(v, rb_intern("to_r"), 0, NULL)) != Qundef) {
+            if (rb_respond_to(v, rb_intern("to_str"))) goto typeerror;
             v = tmp;
-	}
-        else if (!NIL_P(tmp = rb_check_to_integer(v, "to_int")))
+            break;
+        }
+        if (!NIL_P(tmp = rb_check_to_integer(v, "to_int"))) {
             v = tmp;
-        else {
-          typeerror:
-            rb_raise(rb_eTypeError, "can't convert %s into an exact number",
-		                    NIL_P(v) ? "nil" : rb_obj_classname(v));
+            break;
         }
+        goto typeerror;
+    }
+
+    t = TYPE(v);
+    switch (t) {
+      case T_FIXNUM:
+      case T_BIGNUM:
+        return v;
+
+      case T_RATIONAL:
+        if (RRATIONAL(v)->den == INT2FIX(1))
+            v = RRATIONAL(v)->num;
         break;
-      }
+
+      default:
+      typeerror:
+        rb_raise(rb_eTypeError, "can't convert %s into an exact number",
+                                NIL_P(v) ? "nil" : rb_obj_classname(v));
     }
     return v;
 }
@@ -3140,7 +3155,7 @@
     GetTimeval(time, tobj);
     v = w2v(rb_time_unmagnify(tobj->timew));
     if (TYPE(v) != T_RATIONAL) {
-        v = rb_convert_type(v, T_RATIONAL, "Rational", "to_r");
+        v = rb_Rational1(v);
     }
     return v;
 }
Index: ruby_1_9_2/ChangeLog
===================================================================
--- ruby_1_9_2/ChangeLog	(revision 28388)
+++ ruby_1_9_2/ChangeLog	(revision 28389)
@@ -1,3 +1,8 @@
+Tue Jun 22 23:10:10 2010  Tanaka Akira  <akr@f...>
+
+	* time.c (num_exact): fix for mathn.  [ruby-dev:41599]
+
+
 Tue Jun 22 22:00:06 2010  Tanaka Akira  <akr@f...>
 
 	* ext/socket/lib/socket.rb: suppress warnings.

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

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