From 26b0ba2fca105b09bf659ca288c8e362c8e88089 Mon Sep 17 00:00:00 2001
From: Marcus Stollsteimer <sto.mar@web.de>
Date: Mon, 10 Apr 2017 20:22:59 +0200
Subject: make Integer#{round,floor,ceil,truncate} always return integer

* numeric.c (int_round): return integer (self) instead of float
  for Integer#round with positive ndigits argument, because
  conversion to float introduces errors for large integers.
* numeric.c (int_floor): ditto for Integer#floor.
* numeric.c (int_ceil): ditto for Integer#ceil.
* numeric.c (int_truncate): ditto for Integer#truncate.

* test/ruby/test_integer.rb: adjust test cases and add more.
---
 numeric.c                 | 40 ++++++++++++----------------------------
 test/ruby/test_integer.rb | 28 ++++++++++++++++++++--------
 2 files changed, 32 insertions(+), 36 deletions(-)

diff --git a/numeric.c b/numeric.c
index f8f0766..589f557 100644
--- a/numeric.c
+++ b/numeric.c
@@ -5053,11 +5053,10 @@ int_dotimes(VALUE num)
  *  When the precision is negative, the returned value is an integer
  *  with at least <code>ndigits.abs</code> trailing zeros.
  *
- *  Returns a floating point number when +ndigits+ is positive,
- *  +self+ for zero, and an integer for negative.
+ *  Returns +self+ when +ndigits+ is zero or positive.
  *
  *     1.round           #=> 1
- *     1.round(2)        #=> 1.0
+ *     1.round(2)        #=> 1
  *     15.round(-1)      #=> 20
  *     (-15).round(-1)   #=> -20
  *
@@ -5085,10 +5084,7 @@ int_round(int argc, VALUE* argv, VALUE num)
     if (!rb_scan_args(argc, argv, "01:", &nd, &opt)) return num;
     ndigits = NUM2INT(nd);
     mode = rb_num_get_rounding_option(opt);
-    if (ndigits > 0) {
-	return rb_Float(num);
-    }
-    if (ndigits == 0) {
+    if (ndigits >= 0) {
 	return num;
     }
     return rb_int_round(num, ndigits, mode);
@@ -5105,11 +5101,10 @@ int_round(int argc, VALUE* argv, VALUE num)
  *  When the precision is negative, the returned value is an integer
  *  with at least <code>ndigits.abs</code> trailing zeros.
  *
- *  Returns a floating point number when +ndigits+ is positive,
- *  +self+ for zero, and an integer for negative.
+ *  Returns +self+ when +ndigits+ is zero or positive.
  *
  *     1.floor           #=> 1
- *     1.floor(2)        #=> 1.0
+ *     1.floor(2)        #=> 1
  *     18.floor(-1)      #=> 10
  *     (-18).floor(-1)   #=> -20
  */
@@ -5121,10 +5116,7 @@ int_floor(int argc, VALUE* argv, VALUE num)
 
     if (!rb_check_arity(argc, 0, 1)) return num;
     ndigits = NUM2INT(argv[0]);
-    if (ndigits > 0) {
-	return rb_Float(num);
-    }
-    if (ndigits == 0) {
+    if (ndigits >= 0) {
 	return num;
     }
     return rb_int_floor(num, ndigits);
@@ -5141,11 +5133,10 @@ int_floor(int argc, VALUE* argv, VALUE num)
  *  When the precision is negative, the returned value is an integer
  *  with at least <code>ndigits.abs</code> trailing zeros.
  *
- *  Returns a floating point number when +ndigits+ is positive,
- *  +self+ for zero, and an integer for negative.
+ *  Returns +self+ when +ndigits+ is zero or positive.
  *
  *     1.ceil           #=> 1
- *     1.ceil(2)        #=> 1.0
+ *     1.ceil(2)        #=> 1
  *     18.ceil(-1)      #=> 20
  *     (-18).ceil(-1)   #=> -10
  */
@@ -5157,10 +5148,7 @@ int_ceil(int argc, VALUE* argv, VALUE num)
 
     if (!rb_check_arity(argc, 0, 1)) return num;
     ndigits = NUM2INT(argv[0]);
-    if (ndigits > 0) {
-	return rb_Float(num);
-    }
-    if (ndigits == 0) {
+    if (ndigits >= 0) {
 	return num;
     }
     return rb_int_ceil(num, ndigits);
@@ -5177,11 +5165,10 @@ int_ceil(int argc, VALUE* argv, VALUE num)
  *  When the precision is negative, the returned value is an integer
  *  with at least <code>ndigits.abs</code> trailing zeros.
  *
- *  Returns a floating point number when +ndigits+ is positive,
- *  +self+ for zero, and an integer for negative.
+ *  Returns +self+ when +ndigits+ is zero or positive.
  *
  *     1.truncate           #=> 1
- *     1.truncate(2)        #=> 1.0
+ *     1.truncate(2)        #=> 1
  *     18.truncate(-1)      #=> 10
  *     (-18).truncate(-1)   #=> -10
  */
@@ -5193,10 +5180,7 @@ int_truncate(int argc, VALUE* argv, VALUE num)
 
     if (!rb_check_arity(argc, 0, 1)) return num;
     ndigits = NUM2INT(argv[0]);
-    if (ndigits > 0) {
-	return rb_Float(num);
-    }
-    if (ndigits == 0) {
+    if (ndigits >= 0) {
 	return num;
     }
     return rb_int_truncate(num, ndigits);
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb
index e0927c9..7b64c44 100644
--- a/test/ruby/test_integer.rb
+++ b/test/ruby/test_integer.rb
@@ -181,8 +181,8 @@ def test_round
     assert_int_equal(11111, 11111.round)
     assert_int_equal(11111, 11111.round(0))
 
-    assert_float_equal(11111.0, 11111.round(1))
-    assert_float_equal(11111.0, 11111.round(2))
+    assert_int_equal(11111, 11111.round(1))
+    assert_int_equal(11111, 11111.round(2))
 
     assert_int_equal(11110, 11111.round(-1))
     assert_int_equal(11100, 11111.round(-2))
@@ -249,14 +249,17 @@ def test_round
 
     assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.round(-1))
     assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1))
+
+    assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.round(1))
+    assert_int_equal(10**400, (10**400).round(1))
   end
 
   def test_floor
     assert_int_equal(11111, 11111.floor)
     assert_int_equal(11111, 11111.floor(0))
 
-    assert_float_equal(11111.0, 11111.floor(1))
-    assert_float_equal(11111.0, 11111.floor(2))
+    assert_int_equal(11111, 11111.floor(1))
+    assert_int_equal(11111, 11111.floor(2))
 
     assert_int_equal(11110, 11110.floor(-1))
     assert_int_equal(11110, 11119.floor(-1))
@@ -274,14 +277,17 @@ def test_floor
 
     assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.floor(-1))
     assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1120, (-1111_1111_1111_1111_1111_1111_1111_1111).floor(-1))
+
+    assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.floor(1))
+    assert_int_equal(10**400, (10**400).floor(1))
   end
 
   def test_ceil
     assert_int_equal(11111, 11111.ceil)
     assert_int_equal(11111, 11111.ceil(0))
 
-    assert_float_equal(11111.0, 11111.ceil(1))
-    assert_float_equal(11111.0, 11111.ceil(2))
+    assert_int_equal(11111, 11111.ceil(1))
+    assert_int_equal(11111, 11111.ceil(2))
 
     assert_int_equal(11110, 11110.ceil(-1))
     assert_int_equal(11120, 11119.ceil(-1))
@@ -299,14 +305,17 @@ def test_ceil
 
     assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1120, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(-1))
     assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).ceil(-1))
+
+    assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.ceil(1))
+    assert_int_equal(10**400, (10**400).ceil(1))
   end
 
   def test_truncate
     assert_int_equal(11111, 11111.truncate)
     assert_int_equal(11111, 11111.truncate(0))
 
-    assert_float_equal(11111.0, 11111.truncate(1))
-    assert_float_equal(11111.0, 11111.truncate(2))
+    assert_int_equal(11111, 11111.truncate(1))
+    assert_int_equal(11111, 11111.truncate(2))
 
     assert_int_equal(11110, 11110.truncate(-1))
     assert_int_equal(11110, 11119.truncate(-1))
@@ -324,6 +333,9 @@ def test_truncate
 
     assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(-1))
     assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).truncate(-1))
+
+    assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1111, 1111_1111_1111_1111_1111_1111_1111_1111.truncate(1))
+    assert_int_equal(10**400, (10**400).truncate(1))
   end
 
   MimicInteger = Struct.new(:to_int)
-- 
1.9.1

