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

ruby-changes:44679

From: mrkn <ko1@a...>
Date: Sat, 12 Nov 2016 21:14:16 +0900 (JST)
Subject: [ruby-changes:44679] mrkn:r56752 (trunk): rational.c: optimize Float#rationalize

mrkn	2016-11-12 21:14:10 +0900 (Sat, 12 Nov 2016)

  New Revision: 56752

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=56752

  Log:
    rational.c: optimize Float#rationalize
    
    * rational.c (rb_flt_rationalize{,_with_prec},float_rationalize):
      optimize Float#rationalize.
      Author: Tadashi Saito <tad.a.digger@g...>

  Modified files:
    trunk/rational.c
Index: rational.c
===================================================================
--- rational.c	(revision 56751)
+++ rational.c	(revision 56752)
@@ -2093,7 +2093,7 @@ rb_flt_rationalize_with_prec(VALUE flt, https://github.com/ruby/ruby/blob/trunk/rational.c#L2093
     b = f_add(flt, e);
 
     if (f_eqeq_p(a, b))
-        return f_to_r(flt);
+        return float_to_r(flt);
 
     nurat_rationalize_internal(a, b, &p, &q);
     return rb_rational_new2(p, q);
@@ -2105,33 +2105,33 @@ rb_flt_rationalize(VALUE flt) https://github.com/ruby/ruby/blob/trunk/rational.c#L2105
     VALUE a, b, f, n, p, q;
 
     float_decode_internal(flt, &f, &n);
-    if (f_zero_p(f) || f_positive_p(n))
-        return rb_rational_new1(f_lshift(f, n));
+    if (INT_ZERO_P(f) || FIX2INT(n) >= 0)
+        return rb_rational_new1(rb_int_lshift(f, n));
 
 #if FLT_RADIX == 2
     {
         VALUE two_times_f, den;
 
-        two_times_f = f_mul(TWO, f);
-        den = f_lshift(ONE, f_sub(ONE, n));
+        two_times_f = rb_int_mul(TWO, f);
+        den = rb_int_lshift(ONE, rb_int_minus(ONE, n));
 
-        a = rb_rational_new2(f_sub(two_times_f, ONE), den);
-        b = rb_rational_new2(f_add(two_times_f, ONE), den);
+        a = rb_rational_new2(rb_int_minus(two_times_f, ONE), den);
+        b = rb_rational_new2(rb_int_plus(two_times_f, ONE), den);
     }
 #else
     {
         VALUE radix_times_f, den;
 
-        radix_times_f = f_mul(INT2FIX(FLT_RADIX), f);
-        den = f_expt(INT2FIX(FLT_RADIX), f_sub(ONE, n));
+        radix_times_f = rb_int_mul(INT2FIX(FLT_RADIX), f);
+        den = rb_int_pow(INT2FIX(FLT_RADIX), rb_int_minus(ONE, n));
 
-        a = rb_rational_new2(f_sub(radix_times_f, INT2FIX(FLT_RADIX - 1)), den);
-        b = rb_rational_new2(f_add(radix_times_f, INT2FIX(FLT_RADIX - 1)), den);
+        a = rb_rational_new2(rb_int_minus(radix_times_f, INT2FIX(FLT_RADIX - 1)), den);
+        b = rb_rational_new2(rb_int_plus(radix_times_f, INT2FIX(FLT_RADIX - 1)), den);
     }
 #endif
 
-    if (f_eqeq_p(a, b))
-        return f_to_r(flt);
+    if (nurat_eqeq_p(a, b))
+        return float_to_r(flt);
 
     nurat_rationalize_internal(a, b, &p, &q);
     return rb_rational_new2(p, q);
@@ -2155,9 +2155,10 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/rational.c#L2155
 float_rationalize(int argc, VALUE *argv, VALUE self)
 {
     VALUE e;
+    double d = RFLOAT_VALUE(self);
 
-    if (f_negative_p(self))
-        return f_negate(float_rationalize(argc, argv, f_abs(self)));
+    if (d < 0.0)
+        return nurat_negate(float_rationalize(argc, argv, DBL2NUM(-d)));
 
     rb_scan_args(argc, argv, "01", &e);
 

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

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