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

ruby-changes:24347

From: akr <ko1@a...>
Date: Mon, 16 Jul 2012 15:04:05 +0900 (JST)
Subject: [ruby-changes:24347] akr:r36398 (trunk): * internal.h (rb_big_float_cmp): declared.

akr	2012-07-16 15:02:21 +0900 (Mon, 16 Jul 2012)

  New Revision: 36398

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

  Log:
    * internal.h (rb_big_float_cmp): declared.
    
    * bignum.c (rb_big_float_cmp): extracted from rb_big_cmp and big_op.
      (rb_big_cmp): use rb_big_float_cmp.
      (big_op): ditto.
    
    * numeric.c (flo_cmp): use rb_big_float_cmp.
      (flo_gt): ditto.
      (flo_ge): ditto.
      (flo_lt): ditto.
      (flo_le): ditto.

  Modified files:
    trunk/ChangeLog
    trunk/bignum.c
    trunk/internal.h
    trunk/numeric.c

Index: ChangeLog
===================================================================
--- ChangeLog	(revision 36397)
+++ ChangeLog	(revision 36398)
@@ -1,3 +1,17 @@
+Mon Jul 16 15:00:45 2012  Tanaka Akira  <akr@f...>
+
+	* internal.h (rb_big_float_cmp): declared.
+
+	* bignum.c (rb_big_float_cmp): extracted from rb_big_cmp and big_op.
+	  (rb_big_cmp): use rb_big_float_cmp.
+	  (big_op): ditto.
+
+	* numeric.c (flo_cmp): use rb_big_float_cmp.
+	  (flo_gt): ditto.
+	  (flo_ge): ditto.
+	  (flo_lt): ditto.
+	  (flo_le): ditto.
+
 Mon Jul 16 14:14:21 2012  Tanaka Akira  <akr@f...>
 
 	* bignum.c (enum big_op_t): new type.
Index: numeric.c
===================================================================
--- numeric.c	(revision 36397)
+++ numeric.c	(revision 36398)
@@ -1128,12 +1128,12 @@
 	break;
 
       case T_BIGNUM:
-	if (isinf(a)) {
-	    if (a > 0.0) return INT2FIX(1);
-	    else return INT2FIX(-1);
-	}
-	b = rb_big2dbl(y);
-	break;
+      {
+        VALUE rel = rb_big_float_cmp(y, x);
+        if (FIXNUM_P(rel))
+            return INT2FIX(-FIX2INT(rel));
+        return rel;
+      }
 
       case T_FLOAT:
 	b = RFLOAT_VALUE(y);
@@ -1173,8 +1173,12 @@
 	break;
 
       case T_BIGNUM:
-	b = rb_big2dbl(y);
-	break;
+      {
+        VALUE rel = rb_big_float_cmp(y, x);
+        if (FIXNUM_P(rel))
+            return -FIX2INT(rel) > 0 ? Qtrue : Qfalse;
+        return Qfalse;
+      }
 
       case T_FLOAT:
 	b = RFLOAT_VALUE(y);
@@ -1212,8 +1216,12 @@
 	break;
 
       case T_BIGNUM:
-	b = rb_big2dbl(y);
-	break;
+      {
+        VALUE rel = rb_big_float_cmp(y, x);
+        if (FIXNUM_P(rel))
+            return -FIX2INT(rel) >= 0 ? Qtrue : Qfalse;
+        return Qfalse;
+      }
 
       case T_FLOAT:
 	b = RFLOAT_VALUE(y);
@@ -1250,8 +1258,12 @@
 	break;
 
       case T_BIGNUM:
-	b = rb_big2dbl(y);
-	break;
+      {
+        VALUE rel = rb_big_float_cmp(y, x);
+        if (FIXNUM_P(rel))
+            return -FIX2INT(rel) < 0 ? Qtrue : Qfalse;
+        return Qfalse;
+      }
 
       case T_FLOAT:
 	b = RFLOAT_VALUE(y);
@@ -1289,8 +1301,12 @@
 	break;
 
       case T_BIGNUM:
-	b = rb_big2dbl(y);
-	break;
+      {
+        VALUE rel = rb_big_float_cmp(y, x);
+        if (FIXNUM_P(rel))
+            return -FIX2INT(rel) <= 0 ? Qtrue : Qfalse;
+        return Qfalse;
+      }
 
       case T_FLOAT:
 	b = RFLOAT_VALUE(y);
Index: internal.h
===================================================================
--- internal.h	(revision 36397)
+++ internal.h	(revision 36398)
@@ -49,6 +49,7 @@
 /* bignum.c */
 VALUE rb_big_fdiv(VALUE x, VALUE y);
 VALUE rb_big_uminus(VALUE x);
+VALUE rb_big_float_cmp(VALUE x, VALUE y);
 
 /* class.c */
 VALUE rb_obj_methods(int argc, VALUE *argv, VALUE obj);
Index: bignum.c
===================================================================
--- bignum.c	(revision 36397)
+++ bignum.c	(revision 36398)
@@ -1431,6 +1431,18 @@
     return DBL2NUM(rb_big2dbl(x));
 }
 
+VALUE
+rb_big_float_cmp(VALUE x, VALUE y)
+{
+    double a = RFLOAT_VALUE(y);
+
+    if (isinf(a)) {
+        if (a > 0.0) return INT2FIX(-1);
+        else return INT2FIX(1);
+    }
+    return rb_dbl_cmp(rb_big2dbl(x), a);
+}
+
 /*
  *  call-seq:
  *     big <=> numeric   -> -1, 0, +1 or nil
@@ -1456,16 +1468,8 @@
 	break;
 
       case T_FLOAT:
-	{
-	    double a = RFLOAT_VALUE(y);
+        return rb_big_float_cmp(x, y);
 
-	    if (isinf(a)) {
-		if (a > 0.0) return INT2FIX(-1);
-		else return INT2FIX(1);
-	    }
-	    return rb_dbl_cmp(rb_big2dbl(x), a);
-	}
-
       default:
 	return rb_num_coerce_cmp(x, y, rb_intern("<=>"));
     }
@@ -1507,18 +1511,9 @@
 	break;
 
       case T_FLOAT:
-	{
-	    double a = RFLOAT_VALUE(y);
+        rel = rb_big_float_cmp(x, y);
+        break;
 
-	    if (isinf(a)) {
-		if (a > 0.0) rel = INT2FIX(-1);
-		else rel = INT2FIX(1);
-		break;
-	    }
-	    rel = rb_dbl_cmp(rb_big2dbl(x), a);
-	    break;
-	}
-
       default:
 	{
 	    ID id = 0;

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

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