diff options
author | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-02 14:13:53 +0000 |
---|---|---|
committer | matz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2008-04-02 14:13:53 +0000 |
commit | c3d2e4d381a06f262c3096b3bdad87ce3948c677 (patch) | |
tree | cc76f8eb2379fcd20178ed4b5bc1b09ee3585d3f /numeric.c | |
parent | f67f196b1d89753af958249ffe7458aa2e3f42b6 (diff) |
* numeric.c (num_rdiv): should always return rational number.
* rational.c (nurat_add, nurat_sub, nurat_mul, nurat_fdiv,
nurat_cmp): use rb_num_coerce_bin().
* rational.c (nurat_division): does / and rdiv.
* .gdbinit (rp): no longer use rb_p().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15887 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'numeric.c')
-rw-r--r-- | numeric.c | 69 |
1 files changed, 40 insertions, 29 deletions
@@ -248,18 +248,46 @@ num_uminus(VALUE num) /* * call-seq: - * num.quo(numeric) => result * num.fdiv(numeric) => result * - * Equivalent to <code>Numeric#/</code>, but overridden in subclasses. + * Performs floating point division. */ static VALUE -num_quo(VALUE x, VALUE y) +num_fdiv(VALUE x, VALUE y) { - return rb_funcall(x, '/', 1, rb_Rational1(y)); + return rb_funcall(rb_Float(x), '/', 1, y); } +/* + * Document-method: quo + * + * call-seq: + * num.quo(numeric) => result + * + * Suppose to return most accurate division result, which + * by default in ratinal number for 1.9. + * + */ + +/* + * Document-method: rdiv + * + * call-seq: + * num.rdiv(numeric) => result + * + * Performs rational number division. + * + * 654321.rdiv(13731) #=> Rational(218107, 4577) + * 654321.rdiv(13731.5) #=> Rational(1308642, 27463) + * + */ + +static VALUE +num_rdiv(VALUE x, VALUE y) +{ + return rb_funcall(rb_Rational1(x), rb_intern("rdiv"), 1, y); +} static VALUE num_floor(VALUE num); @@ -647,17 +675,11 @@ flo_div(VALUE x, VALUE y) } static VALUE -flo_quo(VALUE x, VALUE y) +flo_fdiv(VALUE x, VALUE y) { return rb_funcall(x, '/', 1, y); } -static VALUE -flo_rdiv(VALUE x, VALUE y) -{ - return rb_funcall(rb_Rational1(x), '/', 1, y); -} - static void flodivmod(double x, double y, double *divp, double *modp) { @@ -2221,24 +2243,17 @@ fixdivmod(long x, long y, long *divp, long *modp) /* * call-seq: - * fix.quo(numeric) => float - * fix.fdiv(numeric) => float + * fix.fdiv(numeric) => float * * Returns the floating point result of dividing <i>fix</i> by * <i>numeric</i>. * - * 654321.quo(13731) #=> 47.6528293642124 - * 654321.quo(13731.24) #=> 47.6519964693647 + * 654321.rdiv(13731) #=> Rational(218107, 4577) + * 654321.rdiv(13731.24) #=> Rational(1308642, 27463) * */ static VALUE -fix_quo(VALUE x, VALUE y) -{ - return rb_funcall(rb_rational_raw1(x), '/', 1, y); -} - -static VALUE fix_fdiv(VALUE x, VALUE y) { if (FIXNUM_P(y)) { @@ -3162,9 +3177,9 @@ Init_Numeric(void) rb_define_method(rb_cNumeric, "-@", num_uminus, 0); rb_define_method(rb_cNumeric, "<=>", num_cmp, 1); rb_define_method(rb_cNumeric, "eql?", num_eql, 1); - rb_define_method(rb_cNumeric, "quo", num_quo, 1); - rb_define_method(rb_cNumeric, "rdiv", num_quo, 1); - rb_define_method(rb_cNumeric, "fdiv", num_quo, 1); + rb_define_method(rb_cNumeric, "fdiv", num_fdiv, 1); + rb_define_method(rb_cNumeric, "quo", num_rdiv, 1); + rb_define_method(rb_cNumeric, "rdiv", num_rdiv, 1); rb_define_method(rb_cNumeric, "div", num_div, 1); rb_define_method(rb_cNumeric, "divmod", num_divmod, 1); rb_define_method(rb_cNumeric, "modulo", num_modulo, 1); @@ -3230,8 +3245,6 @@ Init_Numeric(void) rb_define_method(rb_cFixnum, "%", fix_mod, 1); rb_define_method(rb_cFixnum, "modulo", fix_mod, 1); rb_define_method(rb_cFixnum, "divmod", fix_divmod, 1); - rb_define_method(rb_cFixnum, "quo", fix_quo, 1); - rb_define_method(rb_cFixnum, "rdiv", fix_quo, 1); rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1); rb_define_method(rb_cFixnum, "**", fix_pow, 1); @@ -3287,9 +3300,7 @@ Init_Numeric(void) rb_define_method(rb_cFloat, "-", flo_minus, 1); rb_define_method(rb_cFloat, "*", flo_mul, 1); rb_define_method(rb_cFloat, "/", flo_div, 1); - rb_define_method(rb_cFloat, "quo", flo_quo, 1); - rb_define_method(rb_cFloat, "rdiv", flo_rdiv, 1); - rb_define_method(rb_cFloat, "fdiv", flo_quo, 1); + rb_define_method(rb_cFloat, "fdiv", flo_fdiv, 1); rb_define_method(rb_cFloat, "%", flo_mod, 1); rb_define_method(rb_cFloat, "modulo", flo_mod, 1); rb_define_method(rb_cFloat, "divmod", flo_divmod, 1); |