aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-13 06:47:55 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-04-13 06:47:55 +0000
commit15558a8f573ce07ead22796aef9e531604bb4733 (patch)
treec14110238a2d106aa5cfdb3e806d61778809715d
parent163604ec52ef0d0543f76ecee9b9ec0e02e8d929 (diff)
downloadruby-15558a8f573ce07ead22796aef9e531604bb4733.tar.gz
numeric.c: int_floor
* numeric.c (int_floor): add an optional parameter, digits, as well as Integer#round. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54561 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog5
-rw-r--r--numeric.c56
-rw-r--r--test/ruby/test_integer.rb25
3 files changed, 85 insertions, 1 deletions
diff --git a/ChangeLog b/ChangeLog
index dc21137af3..08a93881c2 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Wed Apr 13 15:47:53 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
+
+ * numeric.c (int_floor): add an optional parameter, digits, as
+ well as Integer#round.
+
Wed Apr 13 14:47:47 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* file.c (Init_File): add alias File.empty? to File.zero?.
diff --git a/numeric.c b/numeric.c
index a8d75ea069..e12c905191 100644
--- a/numeric.c
+++ b/numeric.c
@@ -110,6 +110,7 @@ static VALUE fix_rshift(long, unsigned long);
static VALUE int_pow(long x, unsigned long y);
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);
static VALUE flo_truncate(VALUE num);
static int float_invariant_round(double number, int ndigits, VALUE *num);
@@ -1820,6 +1821,28 @@ rb_int_round(VALUE num, int ndigits)
return n;
}
+VALUE
+rb_int_floor(VALUE num, int ndigits)
+{
+ VALUE f;
+
+ if (int_round_zero_p(num, ndigits))
+ return INT2FIX(0);
+ f = int_pow(10, -ndigits);
+ if (FIXNUM_P(num) && FIXNUM_P(f)) {
+ SIGNED_VALUE x = FIX2LONG(num), y = FIX2LONG(f);
+ int neg = x < 0;
+ if (neg) x = -x + y - 1;
+ x = x / y * y;
+ if (neg) x = -x;
+ return LONG2NUM(x);
+ }
+ if (RB_TYPE_P(f, T_FLOAT)) {
+ /* then int_pow overflow */
+ return INT2FIX(0);
+ }
+ return rb_int_minus(num, rb_int_modulo(num, f));
+}
/*
* call-seq:
@@ -4171,6 +4194,37 @@ int_round(int argc, VALUE* argv, VALUE num)
}
/*
+ * call-seq:
+ * int.floor([ndigits]) -> integer or float
+ *
+ * Returns the largest number less than or equal to +int+ 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.floor #=> 1
+ * 1.floor(2) #=> 1.0
+ * 15.floor(-1) #=> 10
+ */
+
+static VALUE
+int_floor(int argc, VALUE* argv, VALUE num)
+{
+ int ndigits;
+
+ if (!rb_check_arity(argc, 0, 1)) return num;
+ ndigits = NUM2INT(argv[0]);
+ if (ndigits > 0) {
+ return rb_Float(num);
+ }
+ if (ndigits == 0) {
+ return num;
+ }
+ return rb_int_floor(num, ndigits);
+}
+
+/*
* Document-class: ZeroDivisionError
*
* Raised when attempting to divide an integer by 0.
@@ -4340,7 +4394,7 @@ Init_Numeric(void)
rb_define_method(rb_cInteger, "to_i", int_to_i, 0);
rb_define_method(rb_cInteger, "to_int", int_to_i, 0);
rb_define_method(rb_cInteger, "to_f", int_to_f, 0);
- rb_define_method(rb_cInteger, "floor", int_to_i, 0);
+ rb_define_method(rb_cInteger, "floor", int_floor, -1);
rb_define_method(rb_cInteger, "ceil", int_to_i, 0);
rb_define_method(rb_cInteger, "truncate", int_to_i, 0);
rb_define_method(rb_cInteger, "round", int_round, -1);
diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb
index a82562f887..ab7d0279ac 100644
--- a/test/ruby/test_integer.rb
+++ b/test/ruby/test_integer.rb
@@ -199,6 +199,31 @@ class TestInteger < Test::Unit::TestCase
assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1110, (-1111_1111_1111_1111_1111_1111_1111_1111).round(-1))
end
+ def test_floor
+ assert_int_equal(11111, 11111.floor)
+ assert_int_equal(11111, 11111.floor(0))
+
+ assert_float_equal(11111.0, 11111.floor(1))
+ assert_float_equal(11111.0, 11111.floor(2))
+
+ assert_int_equal(11110, 11110.floor(-1))
+ assert_int_equal(11110, 11119.floor(-1))
+ assert_int_equal(11100, 11100.floor(-2))
+ assert_int_equal(11100, 11199.floor(-2))
+ assert_int_equal(0, 11111.floor(-5))
+ assert_int_equal(+200, +299.floor(-2))
+ assert_int_equal(+300, +300.floor(-2))
+ assert_int_equal(-300, -299.floor(-2))
+ assert_int_equal(-300, -300.floor(-2))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70).floor(-71))
+ assert_int_equal(-30 * 10**70, (-25 * 10**70).floor(-71))
+ assert_int_equal(+20 * 10**70, (+25 * 10**70 - 1).floor(-71))
+ assert_int_equal(-30 * 10**70, (-25 * 10**70 + 1).floor(-71))
+
+ assert_int_equal(1111_1111_1111_1111_1111_1111_1111_1110, 1111_1111_1111_1111_1111_1111_1111_1111.floor(-1))
+ assert_int_equal(-1111_1111_1111_1111_1111_1111_1111_1120, (-1111_1111_1111_1111_1111_1111_1111_1111).floor(-1))
+ end
+
def test_bitwise_and_with_integer_mimic_object
def (obj = Object.new).to_int
10