summaryrefslogtreecommitdiff
path: root/rational.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-07 02:50:32 (GMT)
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2013-06-07 02:50:32 (GMT)
commit5385ae9af43208c014040ed1025b09415d48b36f (patch)
tree099bef333ef1a2c93c01e5a07486b287d36e3962 /rational.c
parent0b87f8d4cfa627173037c00712e28eb0d5df8307 (diff)
* rational.c (numeric_quo): move num_quo in numeric.c to numeric_quo
in rational.c to refer canonicalization state for mathn support. [ruby-core:41575] [Bug #5736] * numeric.c (num_quo): ditto. * test/test_mathn.rb: add a test for the change at r41109. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@41132 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'rational.c')
-rw-r--r--rational.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/rational.c b/rational.c
index d5942f1..86c33b5 100644
--- a/rational.c
+++ b/rational.c
@@ -1793,6 +1793,32 @@ numeric_denominator(VALUE self)
return f_denominator(f_to_r(self));
}
+
+/*
+ * call-seq:
+ * num.quo(int_or_rat) -> rat
+ * num.quo(flo) -> flo
+ *
+ * Returns most exact division (rational for integers, float for floats).
+ */
+
+static VALUE
+numeric_quo(VALUE x, VALUE y)
+{
+ if (RB_TYPE_P(y, T_FLOAT)) {
+ return f_fdiv(x, y);
+ }
+
+ if (canonicalization) {
+ x = rb_rational_raw1(x);
+ }
+ else {
+ x = rb_convert_type(x, T_RATIONAL, "Rational", "to_r");
+ }
+ return rb_funcall(x, '/', 1, y);
+}
+
+
/*
* call-seq:
* int.numerator -> self
@@ -2529,6 +2555,7 @@ Init_Rational(void)
rb_define_method(rb_cNumeric, "numerator", numeric_numerator, 0);
rb_define_method(rb_cNumeric, "denominator", numeric_denominator, 0);
+ rb_define_method(rb_cNumeric, "quo", numeric_quo, 1);
rb_define_method(rb_cInteger, "numerator", integer_numerator, 0);
rb_define_method(rb_cInteger, "denominator", integer_denominator, 0);