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

ruby-changes:46420

From: nobu <ko1@a...>
Date: Mon, 1 May 2017 09:33:02 +0900 (JST)
Subject: [ruby-changes:46420] nobu:r58523 (trunk): rational.c: canonicalization case

nobu	2017-05-01 09:32:56 +0900 (Mon, 01 May 2017)

  New Revision: 58523

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

  Log:
    rational.c: canonicalization case
    
    * rational.c (float_numerator, float_denominator): fix for
      canonicalization case where `Float#to_r` could return an Integer
      not a Rational.  although mathn.rb has been removed in the
      trunk, fix for the backport purpose.
      [ruby-core:80942] [Bug #13528]

  Modified files:
    trunk/rational.c
Index: rational.c
===================================================================
--- rational.c	(revision 58522)
+++ rational.c	(revision 58523)
@@ -2073,9 +2073,14 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/rational.c#L2073
 float_numerator(VALUE self)
 {
     double d = RFLOAT_VALUE(self);
+    VALUE r;
     if (isinf(d) || isnan(d))
 	return self;
-    return nurat_numerator(float_to_r(self));
+    r = float_to_r(self);
+    if (canonicalization && k_integer_p(r)) {
+	return r;
+    }
+    return nurat_numerator(r);
 }
 
 /*
@@ -2091,9 +2096,14 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/rational.c#L2096
 float_denominator(VALUE self)
 {
     double d = RFLOAT_VALUE(self);
+    VALUE r;
     if (isinf(d) || isnan(d))
 	return INT2FIX(1);
-    return nurat_denominator(float_to_r(self));
+    r = float_to_r(self);
+    if (canonicalization && k_integer_p(r)) {
+	return ONE;
+    }
+    return nurat_denominator(r);
 }
 
 /*

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

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