ruby-changes:42488
From: nobu <ko1@a...>
Date: Wed, 13 Apr 2016 14:53:46 +0900 (JST)
Subject: [ruby-changes:42488] nobu:r54562 (trunk): numeric.c: int_ceil
nobu 2016-04-13 15:50:24 +0900 (Wed, 13 Apr 2016) New Revision: 54562 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54562 Log: numeric.c: int_ceil * numeric.c (int_ceil): add an optional parameter, digits, as well as Integer#round. [Feature #12245] Modified files: trunk/ChangeLog trunk/numeric.c trunk/test/ruby/test_integer.rb Index: test/ruby/test_integer.rb =================================================================== --- test/ruby/test_integer.rb (revision 54561) +++ test/ruby/test_integer.rb (revision 54562) @@ -224,6 +224,31 @@ class TestInteger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_integer.rb#L224 assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1120, (-1111_1111_1111_1111_1111_1111_1111_1111).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(11110, 11110.ceil(-1)) + assert_int_equal(11120, 11119.ceil(-1)) + assert_int_equal(11200, 11101.ceil(-2)) + assert_int_equal(11200, 11200.ceil(-2)) + assert_int_equal(100000, 11111.ceil(-5)) + assert_int_equal(300, 299.ceil(-2)) + assert_int_equal(300, 300.ceil(-2)) + assert_int_equal(-200, -299.ceil(-2)) + assert_int_equal(-300, -300.ceil(-2)) + assert_int_equal(+30 * 10**70, (+25 * 10**70).ceil(-71)) + assert_int_equal(-20 * 10**70, (-25 * 10**70).ceil(-71)) + assert_int_equal(+30 * 10**70, (+25 * 10**70 - 1).ceil(-71)) + assert_int_equal(-20 * 10**70, (-25 * 10**70 + 1).ceil(-71)) + + 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)) + end + def test_bitwise_and_with_integer_mimic_object def (obj = Object.new).to_int 10 Index: numeric.c =================================================================== --- numeric.c (revision 54561) +++ numeric.c (revision 54562) @@ -111,6 +111,7 @@ static VALUE int_pow(long x, unsigned lo https://github.com/ruby/ruby/blob/trunk/numeric.c#L111 static VALUE int_cmp(VALUE x, VALUE y); static int int_round_zero_p(VALUE num, int ndigits); VALUE rb_int_floor(VALUE num, int ndigits); +VALUE rb_int_ceil(VALUE num, int ndigits); static VALUE flo_truncate(VALUE num); static int float_invariant_round(double number, int ndigits, VALUE *num); @@ -1844,6 +1845,30 @@ rb_int_floor(VALUE num, int ndigits) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1845 return rb_int_minus(num, rb_int_modulo(num, f)); } +VALUE +rb_int_ceil(VALUE num, int ndigits) +{ + VALUE f; + + if (int_round_zero_p(num, ndigits)) + return INT2FIX(0); + f = int_pow(10, -ndigits); + if (FIXNUM_P(num) && FIXNUM_P(f)) { + SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f); + int neg = x < 0; + if (neg) x = -x; + else x += y - 1; + x = (x / y) * y; + if (neg) x = -x; + return LONG2NUM(x); + } + if (RB_TYPE_P(f, T_FLOAT)) { + /* then int_pow overflow */ + return INT2FIX(0); + } + return rb_int_plus(num, rb_int_minus(f, rb_int_modulo(num, f))); +} + /* * call-seq: * float.round([ndigits]) -> integer or float @@ -4225,6 +4250,37 @@ int_floor(int argc, VALUE* argv, VALUE n https://github.com/ruby/ruby/blob/trunk/numeric.c#L4250 } /* + * call-seq: + * int.ceil([ndigits]) -> integer or float + * + * Returns the smallest number than or equal to +int+ 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.ceil #=> 1 + * 1.ceil(2) #=> 1.0 + * 15.ceil(-1) #=> 20 + */ + +static VALUE +int_ceil(int argc, VALUE* argv, VALUE num) +{ + int ndigits; + + if (!rb_check_arity(argc, 0, 1)) return num; + ndigits = NUM2INT(argv[0]); + if (ndigits > 0) { + return rb_Float(num); + } + if (ndigits == 0) { + return num; + } + return rb_int_ceil(num, ndigits); +} + +/* * Document-class: ZeroDivisionError * * Raised when attempting to divide an integer by 0. @@ -4395,7 +4451,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4451 rb_define_method(rb_cInteger, "to_int", int_to_i, 0); rb_define_method(rb_cInteger, "to_f", int_to_f, 0); rb_define_method(rb_cInteger, "floor", int_floor, -1); - rb_define_method(rb_cInteger, "ceil", int_to_i, 0); + rb_define_method(rb_cInteger, "ceil", int_ceil, -1); rb_define_method(rb_cInteger, "truncate", int_to_i, 0); rb_define_method(rb_cInteger, "round", int_round, -1); rb_define_method(rb_cInteger, "<=>", int_cmp, 1); Index: ChangeLog =================================================================== --- ChangeLog (revision 54561) +++ ChangeLog (revision 54562) @@ -1,7 +1,10 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Wed Apr 13 15:47:53 2016 Nobuyoshi Nakada <nobu@r...> +Wed Apr 13 15:50:22 2016 Nobuyoshi Nakada <nobu@r...> + + * numeric.c (int_ceil): add an optional parameter, digits, as + well as Integer#round. [Feature #12245] * numeric.c (int_floor): add an optional parameter, digits, as - well as Integer#round. + well as Integer#round. [Feature #12245] Wed Apr 13 14:47:47 2016 Nobuyoshi Nakada <nobu@r...> -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/