aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-13 06:54:38 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-13 06:54:38 +0000
commitd56b2776847f91fea70a3008ce29be544343f770 (patch)
treeb371d9e362af746914adfac2c19e437037f6b2d7
parenta1542d33ca30cdecb741be526d043be6bba01d76 (diff)
downloadruby-d56b2776847f91fea70a3008ce29be544343f770.tar.gz
numeric.c: flo_floor
* numeric.c (flo_floor): add an optional parameter, digits, as well as Integer#floor. [Feature #12245] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54563 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--numeric.c46
-rw-r--r--test/ruby/test_float.rb22
3 files changed, 66 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index e2a89940e6..44e7329e30 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,7 @@
-Wed Apr 13 15:50:22 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+Wed Apr 13 15:54:36 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * 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]
diff --git a/numeric.c b/numeric.c
index 53939dce9c..6cf4b181f7 100644
--- a/numeric.c
+++ b/numeric.c
@@ -1727,22 +1727,56 @@ flo_prev_float(VALUE vx)
/*
* 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)
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)
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);
diff --git a/test/ruby/test_float.rb b/test/ruby/test_float.rb
index aeac359081..9deed4dea4 100644
--- a/test/ruby/test_float.rb
+++ b/test/ruby/test_float.rb
@@ -444,6 +444,28 @@ class TestFloat < Test::Unit::TestCase
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,