ruby-changes:42551
From: nobu <ko1@a...>
Date: Mon, 18 Apr 2016 11:59:56 +0900 (JST)
Subject: [ruby-changes:42551] nobu:r54625 (trunk): numeric.c: flo_truncate
nobu 2016-04-18 12:56:33 +0900 (Mon, 18 Apr 2016) New Revision: 54625 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=54625 Log: numeric.c: flo_truncate * numeric.c (flo_truncate): add an optional parameter, digits, as well as Float#round. [Feature #12245] Modified files: trunk/ChangeLog trunk/NEWS trunk/numeric.c trunk/test/ruby/test_float.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 54624) +++ ChangeLog (revision 54625) @@ -1,4 +1,7 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 -Mon Apr 18 12:55:31 2016 Nobuyoshi Nakada <nobu@r...> +Mon Apr 18 12:56:31 2016 Nobuyoshi Nakada <nobu@r...> + + * numeric.c (flo_truncate): add an optional parameter, digits, as + well as Float#round. [Feature #12245] * numeric.c (int_truncate): add an optional parameter, digits, as well as Integer#round. [Feature #12245] Index: numeric.c =================================================================== --- numeric.c (revision 54624) +++ numeric.c (revision 54625) @@ -112,7 +112,7 @@ static VALUE int_cmp(VALUE x, VALUE y); https://github.com/ruby/ruby/blob/trunk/numeric.c#L112 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 VALUE flo_to_i(VALUE num); static int float_invariant_round(double number, int ndigits, VALUE *num); static ID id_coerce, id_div, id_divmod; @@ -1767,7 +1767,7 @@ flo_floor(int argc, VALUE *argv, VALUE n https://github.com/ruby/ruby/blob/trunk/numeric.c#L1767 ndigits = NUM2INT(argv[0]); } if (ndigits < 0) { - return rb_int_floor(flo_truncate(num), ndigits); + return rb_int_floor(flo_to_i(num), ndigits); } number = RFLOAT_VALUE(num); if (ndigits > 0) { @@ -2006,7 +2006,7 @@ flo_round(int argc, VALUE *argv, VALUE n https://github.com/ruby/ruby/blob/trunk/numeric.c#L2006 ndigits = NUM2INT(argv[0]); } if (ndigits < 0) { - return rb_int_round(flo_truncate(num), ndigits); + return rb_int_round(flo_to_i(num), ndigits); } number = RFLOAT_VALUE(num); if (ndigits == 0) { @@ -2057,15 +2057,14 @@ float_invariant_round(double number, int https://github.com/ruby/ruby/blob/trunk/numeric.c#L2057 * call-seq: * float.to_i -> integer * float.to_int -> integer - * float.truncate -> integer * * Returns the +float+ truncated to an Integer. * - * Synonyms are #to_i, #to_int, and #truncate. + * Synonyms are #to_i and #to_int */ static VALUE -flo_truncate(VALUE num) +flo_to_i(VALUE num) { double f = RFLOAT_VALUE(num); long val; @@ -2082,6 +2081,24 @@ flo_truncate(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2081 /* * call-seq: + * float.truncate([ndigits]) -> integer or float + * + * Truncates +float+ to a given precision in decimal digits (default 0 digits). + * + * Precision may be negative. Returns a floating point number when +ndigits+ + * is more than zero. + */ +static VALUE +flo_truncate(int argc, VALUE *argv, VALUE num) +{ + if (signbit(RFLOAT_VALUE(num))) + return flo_ceil(argc, argv, num); + else + return flo_floor(argc, argv, num); +} + +/* + * call-seq: * float.positive? -> true or false * * Returns +true+ if +float+ is greater than 0. @@ -2182,7 +2199,7 @@ num_round(int argc, VALUE* argv, VALUE n https://github.com/ruby/ruby/blob/trunk/numeric.c#L2199 static VALUE num_truncate(VALUE num) { - return flo_truncate(rb_Float(num)); + return flo_truncate(0, 0, rb_Float(num)); } static double @@ -4748,12 +4765,12 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4765 rb_define_method(rb_cFloat, "magnitude", flo_abs, 0); rb_define_method(rb_cFloat, "zero?", flo_zero_p, 0); - 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, "to_i", flo_to_i, 0); + rb_define_method(rb_cFloat, "to_int", flo_to_i, 0); rb_define_method(rb_cFloat, "floor", flo_floor, -1); 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); + rb_define_method(rb_cFloat, "truncate", flo_truncate, -1); rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0); rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0); Index: NEWS =================================================================== --- NEWS (revision 54624) +++ NEWS (revision 54625) @@ -38,8 +38,8 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L38 * Float - * Float#ceil and Float#floor now take an optional digits, as well as - Float#round. [Feature #12245] + * Float#ceil, Float#floor, and Float#truncate now take an optional + digits, as well as Float#round. [Feature #12245] * Integer Index: test/ruby/test_float.rb =================================================================== --- test/ruby/test_float.rb (revision 54624) +++ test/ruby/test_float.rb (revision 54625) @@ -501,6 +501,32 @@ class TestFloat < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_float.rb#L501 assert_equal(0.99, 0.981.ceil(prec)) end + def test_truncate_with_precision + assert_equal(1.100, 1.111.truncate(1)) + assert_equal(1.110, 1.111.truncate(2)) + assert_equal(11110, 11119.9.truncate(-1)) + assert_equal(11100, 11100.0.truncate(-2)) + assert_equal(11100, 11199.9.truncate(-2)) + assert_equal(-1.100, -1.111.truncate(1)) + assert_equal(-1.110, -1.111.truncate(2)) + assert_equal(-11110, -11111.1.truncate(-1)) + assert_equal(-11100, -11111.1.truncate(-2)) + assert_equal(0, 11111.1.truncate(-5)) + + assert_equal(10**300, 1.1e300.truncate(-300)) + assert_equal(-10**300, -1.1e300.truncate(-300)) + assert_equal(1.0e-300, 1.1e-300.truncate(300)) + assert_equal(-1.0e-300, -1.1e-300.truncate(300)) + + assert_equal(42.0, 42.0.truncate(308)) + assert_equal(1.0e307, 1.0e307.truncate(2)) + + assert_raise(TypeError) {1.0.truncate("4")} + assert_raise(TypeError) {1.0.truncate(nil)} + def (prec = Object.new).to_int; 2; end + assert_equal(0.99, 0.998.truncate(prec)) + end + VS = [ 18446744073709551617.0, 18446744073709551616.0, -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/