1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

numeric.c: flo_ceil

* numeric.c (flo_ceil): add an optional parameter, digits, as
  well as Float#round.  [Feature #12245]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54564 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-04-13 06:56:36 +00:00
parent d56b277684
commit c258535186
3 changed files with 64 additions and 8 deletions

View file

@ -1,4 +1,7 @@
Wed Apr 13 15:54:36 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
Wed Apr 13 15:56:35 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* numeric.c (flo_ceil): add an optional parameter, digits, as
well as Float#round. [Feature #12245]
* numeric.c (flo_floor): add an optional parameter, digits, as
well as Integer#floor. [Feature #12245]

View file

@ -1786,21 +1786,53 @@ flo_floor(int argc, VALUE *argv, VALUE num)
/*
* call-seq:
* float.ceil -> integer
* float.ceil([ndigits]) -> integer or float
*
* Returns the smallest Integer greater than or equal to +float+.
* Returns the smallest number greater than or equal to +float+ in decimal
* digits (default 0 digits).
*
* Precision may be negative. Returns a floating point number when +ndigits+
* is positive, +self+ for zero, and ceil up for negative.
*
* 1.2.ceil #=> 2
* 2.0.ceil #=> 2
* (-1.2).ceil #=> -1
* (-2.0).ceil #=> -2
* 1.234567.ceil(2) #=> 1.24
* 1.234567.ceil(3) #=> 1.235
* 1.234567.ceil(4) #=> 1.2346
* 1.234567.ceil(5) #=> 1.23457
*
* 34567.89.ceil(-5) #=> 100000
* 34567.89.ceil(-4) #=> 40000
* 34567.89.ceil(-3) #=> 35000
* 34567.89.ceil(-2) #=> 34600
* 34567.89.ceil(-1) #=> 34570
* 34567.89.ceil(0) #=> 34568
* 34567.89.ceil(1) #=> 34567.9
* 34567.89.ceil(2) #=> 34567.89
* 34567.89.ceil(3) #=> 34567.89
*/
static VALUE
flo_ceil(VALUE num)
flo_ceil(int argc, VALUE *argv, VALUE num)
{
double f = ceil(RFLOAT_VALUE(num));
return dbl2ival(f);
double number, f;
int ndigits = 0;
if (rb_check_arity(argc, 0, 1)) {
ndigits = NUM2INT(argv[0]);
}
number = RFLOAT_VALUE(num);
if (ndigits < 0) {
return rb_int_ceil(dbl2ival(ceil(number)), ndigits);
}
if (ndigits == 0) {
return dbl2ival(ceil(number));
}
if (float_invariant_round(number, ndigits, &num)) return num;
f = pow(10, ndigits);
return DBL2NUM(ceil(number * f) / f);
}
static int
@ -2085,7 +2117,7 @@ num_floor(VALUE num)
static VALUE
num_ceil(VALUE num)
{
return flo_ceil(rb_Float(num));
return flo_ceil(0, 0, rb_Float(num));
}
/*
@ -4658,7 +4690,7 @@ Init_Numeric(void)
rb_define_method(rb_cFloat, "to_i", flo_truncate, 0);
rb_define_method(rb_cFloat, "to_int", flo_truncate, 0);
rb_define_method(rb_cFloat, "floor", flo_floor, -1);
rb_define_method(rb_cFloat, "ceil", flo_ceil, 0);
rb_define_method(rb_cFloat, "ceil", flo_ceil, -1);
rb_define_method(rb_cFloat, "round", flo_round, -1);
rb_define_method(rb_cFloat, "truncate", flo_truncate, 0);

View file

@ -466,6 +466,27 @@ class TestFloat < Test::Unit::TestCase
assert_equal(0.99, 0.998.floor(prec))
end
def test_ceil_with_precision
assert_equal(1.200, 1.111.ceil(1))
assert_equal(1.120, 1.111.ceil(2))
assert_equal(11120, 11111.1.ceil(-1))
assert_equal(11200, 11111.1.ceil(-2))
assert_equal(100000, 11111.1.ceil(-5))
assert_equal(2*10**300, 1.1e300.ceil(-300))
assert_equal(-10**300, -1.1e300.ceil(-300))
assert_equal(2.0e-300, 1.1e-300.ceil(300))
assert_equal(-1.0e-300, -1.1e-300.ceil(300))
assert_equal(42.0, 42.0.ceil(308))
assert_equal(1.0e307, 1.0e307.ceil(2))
assert_raise(TypeError) {1.0.ceil("4")}
assert_raise(TypeError) {1.0.ceil(nil)}
def (prec = Object.new).to_int; 2; end
assert_equal(0.99, 0.981.ceil(prec))
end
VS = [
18446744073709551617.0,
18446744073709551616.0,