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 --- numeric.c | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'numeric.c') 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); -- cgit v1.2.3