From fe96a2495cfd8dda16037df2451424b9b36a56e4 Mon Sep 17 00:00:00 2001 From: nobu Date: Mon, 18 Apr 2016 03:56:33 +0000 Subject: numeric.c: flo_truncate * numeric.c (flo_truncate): add an optional parameter, digits, as well as Float#round. [Feature #12245] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54625 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 ++++- NEWS | 4 ++-- numeric.c | 37 +++++++++++++++++++++++++++---------- test/ruby/test_float.rb | 26 ++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index 76be96ac64..6d163add93 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,7 @@ -Mon Apr 18 12:55:31 2016 Nobuyoshi Nakada +Mon Apr 18 12:56:31 2016 Nobuyoshi Nakada + + * 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] diff --git a/NEWS b/NEWS index cb19229761..31b0a25434 100644 --- a/NEWS +++ b/NEWS @@ -38,8 +38,8 @@ with all sufficient information, see the ChangeLog file or Redmine * 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 diff --git a/numeric.c b/numeric.c index 19e2fca6be..a4de6ed66a 100644 --- a/numeric.c +++ b/numeric.c @@ -112,7 +112,7 @@ 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 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 num) 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 num) 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 ndigits, VALUE *num) * 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; @@ -2080,6 +2079,24 @@ flo_truncate(VALUE num) return LONG2FIX(val); } +/* + * 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 @@ -2182,7 +2199,7 @@ num_round(int argc, VALUE* argv, VALUE num) 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) 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); diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb index 424436b088..06b1d85242 100644 --- a/test/ruby/test_float.rb +++ b/test/ruby/test_float.rb @@ -501,6 +501,32 @@ class TestFloat < Test::Unit::TestCase 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, -- cgit v1.2.3