From ca524bcd494e2f284c3211cad8e8dde70e8aa86a Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Tue, 16 Jul 2019 08:15:05 +0900 Subject: [PATCH] Expanded f_numerator --- complex.c | 13 ++++++++++++- internal.h | 1 + rational.c | 6 +++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/complex.c b/complex.c index 74d91b044f..fa00036ffb 100644 --- a/complex.c +++ b/complex.c @@ -191,6 +191,18 @@ f_arg(VALUE x) return rb_funcall(x, id_arg, 0); } +inline static VALUE +f_numerator(VALUE x) +{ + if (RB_TYPE_P(x, T_RATIONAL)) { + return RRATIONAL(x)->num; + } + if (RB_FLOAT_TYPE_P(x)) { + return rb_float_numerator(x); + } + return x; +} + inline static VALUE f_denominator(VALUE x) { @@ -221,7 +233,6 @@ f_negate(VALUE x) return rb_funcall(x, id_negate, 0); } -fun1(numerator) fun1(real_p) inline static VALUE diff --git a/internal.h b/internal.h index bd82231d37..2f0a5afe80 100644 --- a/internal.h +++ b/internal.h @@ -2029,6 +2029,7 @@ VALUE rb_rational_abs(VALUE self); VALUE rb_rational_cmp(VALUE self, VALUE other); VALUE rb_rational_pow(VALUE self, VALUE other); VALUE rb_numeric_quo(VALUE x, VALUE y); +VALUE rb_float_numerator(VALUE x); VALUE rb_float_denominator(VALUE x); /* re.c */ diff --git a/rational.c b/rational.c index 7607628cb0..7113e15e8e 100644 --- a/rational.c +++ b/rational.c @@ -2057,8 +2057,8 @@ static VALUE float_to_r(VALUE self); * * See also Float#denominator. */ -static VALUE -float_numerator(VALUE self) +VALUE +rb_float_numerator(VALUE self) { double d = RFLOAT_VALUE(self); VALUE r; @@ -2777,7 +2777,7 @@ Init_Rational(void) rb_define_method(rb_cInteger, "numerator", integer_numerator, 0); rb_define_method(rb_cInteger, "denominator", integer_denominator, 0); - rb_define_method(rb_cFloat, "numerator", float_numerator, 0); + rb_define_method(rb_cFloat, "numerator", rb_float_numerator, 0); rb_define_method(rb_cFloat, "denominator", rb_float_denominator, 0); rb_define_method(rb_cNilClass, "to_r", nilclass_to_r, 0);