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

ruby-changes:37737

From: gogotanaka <ko1@a...>
Date: Tue, 3 Mar 2015 14:59:43 +0900 (JST)
Subject: [ruby-changes:37737] gogotanaka:r49818 (trunk): * math.c (num2dbl_with_to_f): direct casting from Rational to double.

gogotanaka	2015-03-03 14:59:28 +0900 (Tue, 03 Mar 2015)

  New Revision: 49818

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

  Log:
    * math.c (num2dbl_with_to_f): direct casting from Rational to double.
      [Feature #10909]

  Modified files:
    trunk/ChangeLog
    trunk/math.c
Index: math.c
===================================================================
--- math.c	(revision 49817)
+++ math.c	(revision 49818)
@@ -32,13 +32,20 @@ basic_to_f_p(VALUE klass) https://github.com/ruby/ruby/blob/trunk/math.c#L32
     return rb_method_basic_definition_p(klass, id_to_f);
 }
 
+#define fix2dbl_without_to_f(x) (double)FIX2LONG(x)
+#define big2dbl_without_to_f(x) rb_big2dbl(x)
+#define int2dbl_without_to_f(x) (FIXNUM_P(x) ? fix2dbl_without_to_f(x) : big2dbl_without_to_f(x))
+#define rat2dbl_without_to_f(x) \
+    (int2dbl_without_to_f(rb_rational_num(x)) / \
+     int2dbl_without_to_f(rb_rational_den(x)))
+
 static inline double
 num2dbl_with_to_f(VALUE num)
 {
     if (SPECIAL_CONST_P(num)) {
 	if (FIXNUM_P(num)) {
 	    if (basic_to_f_p(rb_cFixnum))
-		return (double)FIX2LONG(num);
+		return fix2dbl_without_to_f(num);
 	}
 	else if (FLONUM_P(num)) {
 	    return RFLOAT_VALUE(num);
@@ -50,7 +57,11 @@ num2dbl_with_to_f(VALUE num) https://github.com/ruby/ruby/blob/trunk/math.c#L57
 	    return RFLOAT_VALUE(num);
 	  case T_BIGNUM:
 	    if (basic_to_f_p(rb_cBignum))
-		return rb_big2dbl(num);
+		return big2dbl_without_to_f(num);
+	    break;
+	  case T_RATIONAL:
+	    if (basic_to_f_p(rb_cRational))
+		return rat2dbl_without_to_f(num);
 	    break;
 	}
     }
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 49817)
+++ ChangeLog	(revision 49818)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Mar  3 14:47:30 2015  Kazuki Tanaka  <gogotanaka@r...>
+
+	* math.c (num2dbl_with_to_f): direct casting from Rational to double.
+	  [Feature #10909]
+
 Tue Mar  3 07:52:20 2015  Rei Odaira  <Rei.Odaira@g...>
 
 	* test/ruby/test_symbol.rb: avoid a false positive in AIX.

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

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