mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* numeric.c: cancelled recent changes (except to remove rdiv).
* bignum.c: ditto. * bignum.c: added rb_big_idiv. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15918 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
ba9c34e2ad
commit
4e046758f5
4 changed files with 63 additions and 38 deletions
|
@ -1,3 +1,11 @@
|
||||||
|
Mon Apr 7 22:41:21 2008 Tadayoshi Funaba <tadf@dotrb.org>
|
||||||
|
|
||||||
|
* numeric.c: cancelled recent changes (except to remove rdiv).
|
||||||
|
|
||||||
|
* bignum.c: ditto.
|
||||||
|
|
||||||
|
* bignum.c: added rb_big_idiv.
|
||||||
|
|
||||||
Mon Apr 7 15:51:31 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Mon Apr 7 15:51:31 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* encoding.c (enc_init_db): moved to enc/encdb.c.
|
* encoding.c (enc_init_db): moved to enc/encdb.c.
|
||||||
|
|
32
bignum.c
32
bignum.c
|
@ -1800,15 +1800,9 @@ bigdivmod(VALUE x, VALUE y, VALUE *divp, VALUE *modp)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
* call-seq:
|
|
||||||
* big / other => Numeric
|
|
||||||
*
|
|
||||||
* Divides big by other, returning the result.
|
|
||||||
*/
|
|
||||||
|
|
||||||
VALUE
|
static VALUE
|
||||||
rb_big_div(VALUE x, VALUE y)
|
rb_big_divide(VALUE x, VALUE y, ID op)
|
||||||
{
|
{
|
||||||
VALUE z;
|
VALUE z;
|
||||||
|
|
||||||
|
@ -1824,13 +1818,32 @@ rb_big_div(VALUE x, VALUE y)
|
||||||
return DOUBLE2NUM(rb_big2dbl(x) / RFLOAT_VALUE(y));
|
return DOUBLE2NUM(rb_big2dbl(x) / RFLOAT_VALUE(y));
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return rb_num_coerce_bin(x, y, '/');
|
return rb_num_coerce_bin(x, y, op);
|
||||||
}
|
}
|
||||||
bigdivmod(x, y, &z, 0);
|
bigdivmod(x, y, &z, 0);
|
||||||
|
|
||||||
return bignorm(z);
|
return bignorm(z);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* call-seq:
|
||||||
|
* big / other => Numeric
|
||||||
|
*
|
||||||
|
* Divides big by other, returning the result.
|
||||||
|
*/
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_big_div(VALUE x, VALUE y)
|
||||||
|
{
|
||||||
|
return rb_big_divide(x, y, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_big_idiv(VALUE x, VALUE y)
|
||||||
|
{
|
||||||
|
return rb_big_divide(x, y, rb_intern("div"));
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* big % other => Numeric
|
* big % other => Numeric
|
||||||
|
@ -2663,6 +2676,7 @@ Init_Bignum(void)
|
||||||
rb_define_method(rb_cBignum, "*", rb_big_mul, 1);
|
rb_define_method(rb_cBignum, "*", rb_big_mul, 1);
|
||||||
rb_define_method(rb_cBignum, "/", rb_big_div, 1);
|
rb_define_method(rb_cBignum, "/", rb_big_div, 1);
|
||||||
rb_define_method(rb_cBignum, "%", rb_big_modulo, 1);
|
rb_define_method(rb_cBignum, "%", rb_big_modulo, 1);
|
||||||
|
rb_define_method(rb_cBignum, "div", rb_big_idiv, 1);
|
||||||
rb_define_method(rb_cBignum, "divmod", rb_big_divmod, 1);
|
rb_define_method(rb_cBignum, "divmod", rb_big_divmod, 1);
|
||||||
rb_define_method(rb_cBignum, "modulo", rb_big_modulo, 1);
|
rb_define_method(rb_cBignum, "modulo", rb_big_modulo, 1);
|
||||||
rb_define_method(rb_cBignum, "remainder", rb_big_remainder, 1);
|
rb_define_method(rb_cBignum, "remainder", rb_big_remainder, 1);
|
||||||
|
|
50
numeric.c
50
numeric.c
|
@ -248,38 +248,19 @@ num_uminus(VALUE num)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
|
* num.quo(numeric) => result
|
||||||
* num.fdiv(numeric) => result
|
* num.fdiv(numeric) => result
|
||||||
*
|
*
|
||||||
* Performs floating point division.
|
* Equivalent to <code>Numeric#/</code>, but overridden in subclasses.
|
||||||
*/
|
|
||||||
|
|
||||||
static VALUE
|
|
||||||
num_fdiv(VALUE x, VALUE 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
|
|
||||||
* is either rational or float (if any of operands are float).
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* 654321.quo(13731) #=> Rational(218107, 4577)
|
|
||||||
* 654321.quo(13731.24) #=> 47.6519964693647
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
num_quo(VALUE x, VALUE y)
|
num_quo(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
return rb_funcall(rb_Rational1(x), rb_intern("quo"), 1, y);
|
return rb_funcall(x, '/', 1, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static VALUE num_floor(VALUE num);
|
static VALUE num_floor(VALUE num);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -294,7 +275,7 @@ static VALUE num_floor(VALUE num);
|
||||||
static VALUE
|
static VALUE
|
||||||
num_div(VALUE x, VALUE y)
|
num_div(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
return rb_funcall(rb_funcall(x, '/', 1, y), rb_intern("floor"), 0, 0);
|
return num_floor(rb_funcall(x, '/', 1, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -666,7 +647,7 @@ flo_div(VALUE x, VALUE y)
|
||||||
}
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
flo_fdiv(VALUE x, VALUE y)
|
flo_quo(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
return rb_funcall(x, '/', 1, y);
|
return rb_funcall(x, '/', 1, y);
|
||||||
}
|
}
|
||||||
|
@ -2234,12 +2215,23 @@ fixdivmod(long x, long y, long *divp, long *modp)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* fix.fdiv(numeric) => float
|
* fix.quo(numeric) => float
|
||||||
|
* fix.fdiv(numeric) => float
|
||||||
*
|
*
|
||||||
* Returns the floating point result of dividing <i>fix</i> by
|
* Returns the floating point result of dividing <i>fix</i> by
|
||||||
* <i>numeric</i>.
|
* <i>numeric</i>.
|
||||||
|
*
|
||||||
|
* 654321.quo(13731) #=> 47.6528293642124
|
||||||
|
* 654321.quo(13731.24) #=> 47.6519964693647
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static VALUE
|
||||||
|
fix_quo(VALUE x, VALUE y)
|
||||||
|
{
|
||||||
|
return rb_funcall(rb_rational_raw1(x), '/', 1, y);
|
||||||
|
}
|
||||||
|
|
||||||
static VALUE
|
static VALUE
|
||||||
fix_fdiv(VALUE x, VALUE y)
|
fix_fdiv(VALUE x, VALUE y)
|
||||||
{
|
{
|
||||||
|
@ -3164,8 +3156,8 @@ Init_Numeric(void)
|
||||||
rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
|
rb_define_method(rb_cNumeric, "-@", num_uminus, 0);
|
||||||
rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);
|
rb_define_method(rb_cNumeric, "<=>", num_cmp, 1);
|
||||||
rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
|
rb_define_method(rb_cNumeric, "eql?", num_eql, 1);
|
||||||
rb_define_method(rb_cNumeric, "fdiv", num_fdiv, 1);
|
|
||||||
rb_define_method(rb_cNumeric, "quo", num_quo, 1);
|
rb_define_method(rb_cNumeric, "quo", num_quo, 1);
|
||||||
|
rb_define_method(rb_cNumeric, "fdiv", num_quo, 1);
|
||||||
rb_define_method(rb_cNumeric, "div", num_div, 1);
|
rb_define_method(rb_cNumeric, "div", num_div, 1);
|
||||||
rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
|
rb_define_method(rb_cNumeric, "divmod", num_divmod, 1);
|
||||||
rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
|
rb_define_method(rb_cNumeric, "modulo", num_modulo, 1);
|
||||||
|
@ -3231,6 +3223,7 @@ Init_Numeric(void)
|
||||||
rb_define_method(rb_cFixnum, "%", fix_mod, 1);
|
rb_define_method(rb_cFixnum, "%", fix_mod, 1);
|
||||||
rb_define_method(rb_cFixnum, "modulo", 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, "divmod", fix_divmod, 1);
|
||||||
|
rb_define_method(rb_cFixnum, "quo", fix_quo, 1);
|
||||||
rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1);
|
rb_define_method(rb_cFixnum, "fdiv", fix_fdiv, 1);
|
||||||
rb_define_method(rb_cFixnum, "**", fix_pow, 1);
|
rb_define_method(rb_cFixnum, "**", fix_pow, 1);
|
||||||
|
|
||||||
|
@ -3286,7 +3279,8 @@ Init_Numeric(void)
|
||||||
rb_define_method(rb_cFloat, "-", flo_minus, 1);
|
rb_define_method(rb_cFloat, "-", flo_minus, 1);
|
||||||
rb_define_method(rb_cFloat, "*", flo_mul, 1);
|
rb_define_method(rb_cFloat, "*", flo_mul, 1);
|
||||||
rb_define_method(rb_cFloat, "/", flo_div, 1);
|
rb_define_method(rb_cFloat, "/", flo_div, 1);
|
||||||
rb_define_method(rb_cFloat, "fdiv", flo_fdiv, 1);
|
rb_define_method(rb_cFloat, "quo", flo_quo, 1);
|
||||||
|
rb_define_method(rb_cFloat, "fdiv", flo_quo, 1);
|
||||||
rb_define_method(rb_cFloat, "%", flo_mod, 1);
|
rb_define_method(rb_cFloat, "%", flo_mod, 1);
|
||||||
rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
|
rb_define_method(rb_cFloat, "modulo", flo_mod, 1);
|
||||||
rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
|
rb_define_method(rb_cFloat, "divmod", flo_divmod, 1);
|
||||||
|
|
|
@ -51,7 +51,16 @@ class TestNumeric < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_quo
|
def test_quo
|
||||||
assert_raise(ArgumentError) {DummyNumeric.new.quo(0)}
|
DummyNumeric.class_eval do
|
||||||
|
def /(x); :div; end
|
||||||
|
end
|
||||||
|
|
||||||
|
assert_equal(:div, DummyNumeric.new.quo(0))
|
||||||
|
|
||||||
|
ensure
|
||||||
|
DummyNumeric.class_eval do
|
||||||
|
remove_method :/
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_divmod
|
def test_divmod
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue