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

ruby-changes:65459

From: Jeremy <ko1@a...>
Date: Sat, 13 Mar 2021 00:35:38 +0900 (JST)
Subject: [ruby-changes:65459] aaab3b1de9 (master): Fix integer/float remainder with infinity argument of opposite sign

https://git.ruby-lang.org/ruby.git/commit/?id=aaab3b1de9

From aaab3b1de943c3317e115d623ffc7908b4c96578 Mon Sep 17 00:00:00 2001
From: Jeremy Evans <code@j...>
Date: Wed, 10 Mar 2021 13:15:50 -0800
Subject: Fix integer/float remainder with infinity argument of opposite sign

Previously, the result was incorrect:

4.remainder(-Float::INFINITY)
Before: => NaN
After: => 4

4.2.remainder(-Float::INFINITY)
Before: => NaN
After: => 4.2

Fixes [Bug #6120]
---
 numeric.c                 | 11 ++++++++---
 test/ruby/test_numeric.rb | 12 ++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/numeric.c b/numeric.c
index 167eed6..caf7b86 100644
--- a/numeric.c
+++ b/numeric.c
@@ -656,6 +656,11 @@ num_remainder(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L656
 	  rb_num_positive_int_p(y)) ||
 	 (rb_num_positive_int_p(x) &&
 	  rb_num_negative_int_p(y)))) {
+        if (RB_TYPE_P(y, T_FLOAT)) {
+            if (isinf(RFLOAT_VALUE(y))) {
+                return x;
+            }
+        }
 	return rb_funcall(z, '-', 1, y);
     }
     return z;
@@ -1151,11 +1156,11 @@ flodivmod(double x, double y, double *divp, double *modp) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1156
 	div = x;
     else {
 	div = (x - mod) / y;
-	if (modp && divp) div = round(div);
+        if (modp && divp) div = round(div);
     }
     if (y*mod < 0) {
-	mod += y;
-	div -= 1.0;
+        mod += y;
+        div -= 1.0;
     }
     if (modp) *modp = mod;
     if (divp) *divp = div;
diff --git a/test/ruby/test_numeric.rb b/test/ruby/test_numeric.rb
index c8751fb..066afc8 100644
--- a/test/ruby/test_numeric.rb
+++ b/test/ruby/test_numeric.rb
@@ -384,6 +384,18 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L384
     end;
   end
 
+  def test_remainder_infinity
+    assert_equal(4, 4.remainder(Float::INFINITY))
+    assert_equal(4, 4.remainder(-Float::INFINITY))
+    assert_equal(-4, -4.remainder(Float::INFINITY))
+    assert_equal(-4, -4.remainder(-Float::INFINITY))
+
+    assert_equal(4.2, 4.2.remainder(Float::INFINITY))
+    assert_equal(4.2, 4.2.remainder(-Float::INFINITY))
+    assert_equal(-4.2, -4.2.remainder(Float::INFINITY))
+    assert_equal(-4.2, -4.2.remainder(-Float::INFINITY))
+  end
+
   def test_comparison_comparable
     bug12864 = '[ruby-core:77713] [Bug #12864]'
 
-- 
cgit v1.1


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

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