1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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
This commit is contained in:
nobu 2016-04-18 03:56:33 +00:00
parent 39f31b8c87
commit fe96a2495c
4 changed files with 59 additions and 13 deletions

View file

@ -1,4 +1,7 @@
Mon Apr 18 12:55:31 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
Mon Apr 18 12:56:31 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* 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]

4
NEWS
View file

@ -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

View file

@ -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);

View file

@ -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,