summaryrefslogtreecommitdiff
path: root/numeric.c
diff options
context:
space:
mode:
authorstomar <stomar@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-06 19:58:12 (GMT)
committerstomar <stomar@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-05-06 19:58:12 (GMT)
commitf495fe4fc024c0d398a6291bfdecc7634198a75a (patch)
tree0af5154ef65f8856a609662986684f09cea72c69 /numeric.c
parent7c0dcecb394d84e37b26e8272e7a22ec33836046 (diff)
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 some more. [ruby-core:80645] [Bug #13420] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r--numeric.c40
1 files changed, 12 insertions, 28 deletions
diff --git a/numeric.c b/numeric.c
index cb9a780..69bb573 100644
--- a/numeric.c
+++ b/numeric.c
@@ -4985,11 +4985,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
*
@@ -5017,10 +5016,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);
@@ -5037,11 +5033,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
*/
@@ -5053,10 +5048,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);
@@ -5073,11 +5065,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
*/
@@ -5089,10 +5080,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);
@@ -5109,11 +5097,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
*/
@@ -5125,10 +5112,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);