ruby-changes:42489
From: nobu <ko1@a...>
Date: Wed, 13 Apr 2016 14:58:01 +0900 (JST)
Subject: [ruby-changes:42489] nobu:r54563 (trunk): numeric.c: flo_floor
nobu 2016-04-13 15:54:38 +0900 (Wed, 13 Apr 2016) New Revision: 54563 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54563 Log: numeric.c: flo_floor * numeric.c (flo_floor): add an optional parameter, digits, as well as Integer#floor. [Feature #12245] Modified files: trunk/ChangeLog trunk/numeric.c trunk/test/ruby/test_float.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 54562) +++ ChangeLog (revision 54563) @@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Wed Apr 13 15:50:22 2016 Nobuyoshi Nakada <nobu@r...> +Wed Apr 13 15:54:36 2016 Nobuyoshi Nakada <nobu@r...> + + * numeric.c (flo_floor): add an optional parameter, digits, as + well as Integer#floor. [Feature #12245] * numeric.c (int_ceil): add an optional parameter, digits, as well as Integer#round. [Feature #12245] Index: test/ruby/test_float.rb =================================================================== --- test/ruby/test_float.rb (revision 54562) +++ test/ruby/test_float.rb (revision 54563) @@ -444,6 +444,28 @@ class TestFloat < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_float.rb#L444 assert_equal(1.0, 0.998.round(prec)) end + def test_floor_with_precision + assert_equal(1.100, 1.111.floor(1)) + assert_equal(1.110, 1.111.floor(2)) + assert_equal(11110, 11119.9.floor(-1)) + assert_equal(11100, 11100.0.floor(-2)) + assert_equal(11100, 11199.9.floor(-2)) + assert_equal(0, 11111.1.floor(-5)) + + assert_equal(10**300, 1.1e300.floor(-300)) + assert_equal(-2*10**300, -1.1e300.floor(-300)) + assert_equal(1.0e-300, 1.1e-300.floor(300)) + assert_equal(-2.0e-300, -1.1e-300.floor(300)) + + assert_equal(42.0, 42.0.floor(308)) + assert_equal(1.0e307, 1.0e307.floor(2)) + + assert_raise(TypeError) {1.0.floor("4")} + assert_raise(TypeError) {1.0.floor(nil)} + def (prec = Object.new).to_int; 2; end + assert_equal(0.99, 0.998.floor(prec)) + end + VS = [ 18446744073709551617.0, 18446744073709551616.0, Index: numeric.c =================================================================== --- numeric.c (revision 54562) +++ numeric.c (revision 54563) @@ -1727,22 +1727,56 @@ flo_prev_float(VALUE vx) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1727 /* * call-seq: - * float.floor -> integer + * float.floor([ndigits]) -> integer or float * - * Returns the largest integer less than or equal to +float+. + * Returns the largest number less 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 floor down for negative. * * 1.2.floor #=> 1 * 2.0.floor #=> 2 * (-1.2).floor #=> -2 * (-2.0).floor #=> -2 + * + * 1.234567.floor(2) #=> 1.23 + * 1.234567.floor(3) #=> 1.234 + * 1.234567.floor(4) #=> 1.2345 + * 1.234567.floor(5) #=> 1.23456 + * + * 34567.89.floor(-5) #=> 0 + * 34567.89.floor(-4) #=> 30000 + * 34567.89.floor(-3) #=> 34000 + * 34567.89.floor(-2) #=> 34500 + * 34567.89.floor(-1) #=> 34560 + * 34567.89.floor(0) #=> 34567 + * 34567.89.floor(1) #=> 34567.8 + * 34567.89.floor(2) #=> 34567.89 + * 34567.89.floor(3) #=> 34567.89 */ static VALUE -flo_floor(VALUE num) +flo_floor(int argc, VALUE *argv, VALUE num) { - double f = floor(RFLOAT_VALUE(num)); + double number, f; long val; + int ndigits = 0; + if (rb_check_arity(argc, 0, 1)) { + ndigits = NUM2INT(argv[0]); + } + if (ndigits < 0) { + return rb_int_floor(flo_truncate(num), ndigits); + } + number = RFLOAT_VALUE(num); + if (ndigits > 0) { + if (float_invariant_round(number, ndigits, &num)) return num; + f = pow(10, ndigits); + f = floor(number * f) / f; + return DBL2NUM(f); + } + f = floor(number); if (!FIXABLE(f)) { return rb_dbl2big(f); } @@ -2028,7 +2062,7 @@ flo_negative_p(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2062 static VALUE num_floor(VALUE num) { - return flo_floor(rb_Float(num)); + return flo_floor(0, 0, rb_Float(num)); } @@ -4623,7 +4657,7 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4657 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, 0); + rb_define_method(rb_cFloat, "floor", flo_floor, -1); rb_define_method(rb_cFloat, "ceil", flo_ceil, 0); rb_define_method(rb_cFloat, "round", flo_round, -1); rb_define_method(rb_cFloat, "truncate", flo_truncate, 0); -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/