rational.c: implement `Rational#/` and `#quo` in C.
MODIFY
mrbgems/mruby-rational/mrblib/rational.rb
mrbgems/mruby-rational/mrblib/rational.rb
@@ -7,18 +7,6 @@ class Rational < Numeric
"#{numerator}/#{denominator}"
end
- def /(rhs)
- if rhs.is_a? Rational
- Rational(numerator * rhs.denominator, denominator * rhs.numerator)
- elsif rhs.is_a? Integer
- Rational(numerator, denominator * rhs)
- elsif rhs.is_a? Numeric
- numerator / rhs / denominator
- end
- end
-
- alias quo /
-
def <=>(rhs)
case rhs
when Integer, Float

MODIFY
mrbgems/mruby-rational/src/rational.c
mrbgems/mruby-rational/src/rational.c
@@ -545,6 +545,48 @@ rational_mul(mrb_state *mrb, mrb_value x)
mrb_int mrb_num_div_int(mrb_state *, mrb_int, mrb_int);
+static mrb_value
+rational_div(mrb_state *mrb, mrb_value x)
+{
+ struct mrb_rational *p1 = rational_ptr(mrb, x);
+ mrb_value y = mrb_get_arg1(mrb);
+
+ switch (mrb_type(y)) {
+ case MRB_TT_INTEGER:
+ {
+ mrb_int z = mrb_integer(y);
+ if (mrb_int_mul_overflow(p1->denominator, z, &z)) rat_overflow(mrb);
+ return rational_new_i(mrb, p1->numerator, z);
+ }
+ case MRB_TT_RATIONAL:
+ {
+ struct mrb_rational *p2 = rational_ptr(mrb, y);
+ mrb_int a, b;
+
+ if (mrb_int_mul_overflow(p1->numerator, p2->denominator, &a)) rat_overflow(mrb);
+ if (mrb_int_mul_overflow(p2->numerator, p1->denominator, &b)) rat_overflow(mrb);
+ return rational_new_i(mrb, a, b);
+ }
+
+#if defined(MRB_USE_COMPLEX)
+ case MRB_TT_COMPLEX:
+ x = mrb_complex_new(mrb, rat_to_flo(p1), 0);
+ return mrb_funcall_id(mrb, x, MRB_OPSYM(div), 1, y);
+#endif
+
+ default:
+#ifndef MRB_NO_FLOAT
+ case MRB_TT_FLOAT:
+ {
+ mrb_float z = mrb_num_div_flo(mrb, p1->numerator, mrb_to_flo(mrb, y));
+ return mrb_float_value(mrb, mrb_num_div_flo(mrb, z, (mrb_float)p1->denominator));
+ }
+#else
+ mrb_raise(mrb, E_TYPE_ERROR, "non integer division");
+#endif
+ }
+}
+
/* 15.2.8.3.4 */
/*
* redefine Integer#/
@@ -631,6 +673,8 @@ void mrb_mruby_rational_gem_init(mrb_state *mrb)
mrb_define_method(mrb, rat, "+", rational_add, MRB_ARGS_REQ(1));
mrb_define_method(mrb, rat, "-", rational_sub, MRB_ARGS_REQ(1));
mrb_define_method(mrb, rat, "*", rational_mul, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, rat, "/", rational_div, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, rat, "quo", rational_div, MRB_ARGS_REQ(1));
mrb_define_method(mrb, mrb->integer_class, "to_r", fix_to_r, MRB_ARGS_NONE());
mrb_define_method(mrb, mrb->integer_class, "/", int_div, MRB_ARGS_REQ(1)); /* overrride */
mrb_define_method(mrb, mrb->integer_class, "quo", int_quo, MRB_ARGS_REQ(1)); /* overrride */

rational.c: implement `Rational#/` and `#quo` in C.