Skip to content

Commit

Permalink
Expanded f_denominator
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Jul 15, 2019
1 parent 325d546 commit 34019a2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
14 changes: 12 additions & 2 deletions complex.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,17 @@ f_arg(VALUE x)
return rb_funcall(x, id_arg, 0);
}

fun1(denominator)
inline static VALUE
f_denominator(VALUE x)
{
if (RB_TYPE_P(x, T_RATIONAL)) {
return RRATIONAL(x)->den;
}
if (RB_FLOAT_TYPE_P(x)) {
return rb_float_denominator(x);
}
return INT2FIX(1);
}

inline static VALUE
f_negate(VALUE x)
Expand Down Expand Up @@ -1252,7 +1262,7 @@ nucomp_numerator(VALUE self)

get_dat1(self);

cd = f_denominator(self);
cd = nucomp_denominator(self);
return f_complex_new2(CLASS_OF(self),
f_mul(f_numerator(dat->real),
f_div(cd, f_denominator(dat->real))),
Expand Down
1 change: 1 addition & 0 deletions internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_denominator(VALUE x);

/* re.c */
VALUE rb_reg_compile(VALUE str, int options, const char *sourcefile, int sourceline);
Expand Down
6 changes: 3 additions & 3 deletions rational.c
Original file line number Diff line number Diff line change
Expand Up @@ -2080,8 +2080,8 @@ float_numerator(VALUE self)
*
* See also Float#numerator.
*/
static VALUE
float_denominator(VALUE self)
VALUE
rb_float_denominator(VALUE self)
{
double d = RFLOAT_VALUE(self);
VALUE r;
Expand Down Expand Up @@ -2778,7 +2778,7 @@ Init_Rational(void)
rb_define_method(rb_cInteger, "denominator", integer_denominator, 0);

rb_define_method(rb_cFloat, "numerator", float_numerator, 0);
rb_define_method(rb_cFloat, "denominator", float_denominator, 0);
rb_define_method(rb_cFloat, "denominator", rb_float_denominator, 0);

rb_define_method(rb_cNilClass, "to_r", nilclass_to_r, 0);
rb_define_method(rb_cNilClass, "rationalize", nilclass_rationalize, -1);
Expand Down

0 comments on commit 34019a2

Please sign in to comment.