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

ruby-changes:45917

From: nobu <ko1@a...>
Date: Thu, 16 Mar 2017 12:35:33 +0900 (JST)
Subject: [ruby-changes:45917] nobu:r57990 (trunk): rational.c: float denom

nobu	2017-03-16 12:35:29 +0900 (Thu, 16 Mar 2017)

  New Revision: 57990

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

  Log:
    rational.c: float denom
    
    * rational.c (parse_rat): allow float as a denominator as well as
      a numerator.  [ruby-core:79104] [Bug #13134]

  Modified files:
    trunk/rational.c
    trunk/test/ruby/test_rational.rb
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 57989)
+++ test/ruby/test_rational.rb	(revision 57990)
@@ -747,6 +747,19 @@ class Rational_Test < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L747
     ng[ 5, 1, '5e_1']
     ng[50, 1, '5e1_']
 
+    ok[ 50, 33, '5/3.3']
+    ok[  5,  3, '5/3e0']
+    ok[  5, 30, '5/3e1']
+    ng[  5,  3, '5/3._3']
+    ng[ 50, 33, '5/3.3_']
+    ok[500,333, '5/3.3_3']
+    ng[  5,  3, '5/3e']
+    ng[  5,  3, '5/3_e']
+    ng[  5,  3, '5/3e_']
+    ng[  5,  3, '5/3e_1']
+    ng[  5, 30, '5/3e1_']
+    ok[  5, 300000000000, '5/3e1_1']
+
     ng[0, 1, '']
     ng[0, 1, ' ']
     ng[5, 1, "\f\n\r\t\v5\0"]
@@ -756,7 +769,6 @@ class Rational_Test < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L769
     ng[5, 1, '5x']
     ng[5, 1, '5/_3']
     ng[5, 3, '5/3_']
-    ng[5, 3, '5/3.3']
     ng[5, 3, '5/3x']
   end
 
Index: rational.c
===================================================================
--- rational.c	(revision 57989)
+++ rational.c	(revision 57990)
@@ -2356,7 +2356,7 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/rational.c#L2356
 parse_rat(const char *s, const char *const e, int strict)
 {
     int sign;
-    VALUE num, den, ndiv;
+    VALUE num, den, ndiv, ddiv;
 
     s = skip_ws(s, e);
     sign = read_sign(&s, e);
@@ -2368,12 +2368,8 @@ parse_rat(const char *s, const char *con https://github.com/ruby/ruby/blob/trunk/rational.c#L2368
     nurat_reduce(&num, &ndiv);
     den = ndiv;
     if (s < e && *s == '/') {
-	char *t;
 	s++;
-	den = rb_int_parse_cstr(s, e-s, &t, NULL,
-				10, RB_INT_PARSE_UNDERSCORE);
-	s = t;
-	if (NIL_P(den)) {
+	if (!read_num(&s, e, &den, &ddiv)) {
 	    if (strict) return Qnil;
 	    den = ndiv;
 	}
@@ -2384,8 +2380,11 @@ parse_rat(const char *s, const char *con https://github.com/ruby/ruby/blob/trunk/rational.c#L2380
 	    return Qnil;
 	}
 	else {
+	    nurat_reduce(&den, &ddiv);
 	    nurat_reduce(&num, &den);
-	    den = rb_int_mul(den, ndiv);
+	    nurat_reduce(&ndiv, &ddiv);
+	    if (ndiv != ONE) den = rb_int_mul(den, ndiv);
+	    if (ddiv != ONE) num = rb_int_mul(num, ddiv);
 	}
     }
     else if (strict && skip_ws(s, e) != e) {

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

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