summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNobuyoshi Nakada <nobu@ruby-lang.org>2019-08-02 11:25:41 +0900
committerNobuyoshi Nakada <nobu@ruby-lang.org>2019-08-02 11:26:58 +0900
commitf9a0492b76956b545c746ab0d3ec0e555e77dfcd (patch)
tree9db8b0b1fec93c6eed5b51af63d2929a6ef00199
parentffe4a6ebf93166544c4265271f7b8cffa2208af6 (diff)
Expanded f_ceil
-rw-r--r--internal.h1
-rw-r--r--numeric.c9
-rw-r--r--rational.c18
3 files changed, 19 insertions, 9 deletions
diff --git a/internal.h b/internal.h
index fac8ece..5ecb229 100644
--- a/internal.h
+++ b/internal.h
@@ -1772,6 +1772,7 @@ VALUE rb_int_odd_p(VALUE num);
int rb_int_positive_p(VALUE num);
int rb_int_negative_p(VALUE num);
VALUE rb_num_pow(VALUE x, VALUE y);
+VALUE rb_float_ceil(VALUE num, int ndigits);
static inline VALUE
rb_num_compare_with_zero(VALUE num, ID mid)
diff --git a/numeric.c b/numeric.c
index 5b0ab8c..7ce6590 100644
--- a/numeric.c
+++ b/numeric.c
@@ -2002,12 +2002,19 @@ flo_floor(int argc, VALUE *argv, VALUE num)
static VALUE
flo_ceil(int argc, VALUE *argv, VALUE num)
{
- double number, f;
int ndigits = 0;
if (rb_check_arity(argc, 0, 1)) {
ndigits = NUM2INT(argv[0]);
}
+ return rb_float_ceil(num, ndigits);
+}
+
+VALUE
+rb_float_ceil(VALUE num, int ndigits)
+{
+ double number, f;
+
number = RFLOAT_VALUE(num);
if (number == 0.0) {
return ndigits > 0 ? DBL2NUM(number) : INT2FIX(0);
diff --git a/rational.c b/rational.c
index 3737db4..1dc08a0 100644
--- a/rational.c
+++ b/rational.c
@@ -43,13 +43,6 @@ static ID id_abs, id_idiv, id_integer_p,
static VALUE nurat_to_f(VALUE self);
-#define fun1(n) \
-inline static VALUE \
-f_##n(VALUE x)\
-{\
- return rb_funcall(x, id_##n, 0);\
-}
-
inline static VALUE
f_add(VALUE x, VALUE y)
{
@@ -1597,7 +1590,16 @@ nurat_to_r(VALUE self)
}
#define id_ceil rb_intern("ceil")
-#define f_ceil(x) rb_funcall((x), id_ceil, 0)
+static VALUE
+f_ceil(VALUE x)
+{
+ if (RB_INTEGER_TYPE_P(x))
+ return x;
+ if (RB_FLOAT_TYPE_P(x))
+ return rb_float_ceil(x, 0);
+
+ return rb_funcall(x, id_ceil, 0);
+}
#define id_quo rb_intern("quo")
#define f_quo(x,y) rb_funcall((x), id_quo, 1, (y))