From 9466848db3107535a6887469ad6c89adc1f911b3 Mon Sep 17 00:00:00 2001 From: matz Date: Mon, 1 May 2006 03:46:47 +0000 Subject: * numeric.c (num_div): use floor rather than rb_Integer(). [ruby-dev:28589] * numeric.c (flo_divmod): the first element of Float#divmod should be an integer. [ruby-dev:28589] * test/ruby/test_float.rb: add tests for divmod, div, modulo and remainder. * util.c (ruby_strtod): fixed wrong conversion. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@10123 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- numeric.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) (limited to 'numeric.c') diff --git a/numeric.c b/numeric.c index 4c8bacdaf6..a937868ce4 100644 --- a/numeric.c +++ b/numeric.c @@ -256,6 +256,8 @@ num_quo(x, y) } +static VALUE num_floor(VALUE num); + /* * call-seq: * num.div(numeric) => integer @@ -269,7 +271,7 @@ static VALUE num_div(x, y) VALUE x, y; { - return rb_Integer(rb_funcall(x, '/', 1, y)); + return num_floor(rb_funcall(x, '/', 1, y)); } @@ -297,21 +299,21 @@ num_div(x, y) * ------+-----+---------------+---------+-------------+--------------- * -13 | -4 | 3, -1 | 3 | -1 | -1 * ------+-----+---------------+---------+-------------+--------------- - * 11.5 | 4 | 2.0, 3.5 | 2.875 | 3.5 | 3.5 + * 11.5 | 4 | 2, 3.5 | 2.875 | 3.5 | 3.5 * ------+-----+---------------+---------+-------------+--------------- - * 11.5 | -4 | -3.0, -0.5 | -2.875 | -0.5 | 3.5 + * 11.5 | -4 | -3, -0.5 | -2.875 | -0.5 | 3.5 * ------+-----+---------------+---------+-------------+--------------- - * -11.5 | 4 | -3.0 0.5 | -2.875 | 0.5 | -3.5 + * -11.5 | 4 | -3, 0.5 | -2.875 | 0.5 | -3.5 * ------+-----+---------------+---------+-------------+--------------- - * -11.5 | -4 | 2.0 -3.5 | 2.875 | -3.5 | -3.5 + * -11.5 | -4 | 2 -3.5 | 2.875 | -3.5 | -3.5 * * * Examples * 11.divmod(3) #=> [3, 2] * 11.divmod(-3) #=> [-4, -1] - * 11.divmod(3.5) #=> [3.0, 0.5] - * (-11).divmod(3.5) #=> [-4.0, 3.0] - * (11.5).divmod(3.5) #=> [3.0, 1.0] + * 11.divmod(3.5) #=> [3, 0.5] + * (-11).divmod(3.5) #=> [-4, 3.0] + * (11.5).divmod(3.5) #=> [3, 1.0] */ static VALUE @@ -715,7 +717,7 @@ static VALUE flo_divmod(x, y) VALUE x, y; { - double fy, div, mod; + double fy, div, mod, val; volatile VALUE a, b; switch (TYPE(y)) { @@ -732,7 +734,13 @@ flo_divmod(x, y) return rb_num_coerce_bin(x, y); } flodivmod(RFLOAT(x)->value, fy, &div, &mod); - a = rb_float_new(div); + if (FIXABLE(div)) { + val = div; + a = LONG2FIX(val); + } + else { + a = rb_dbl2big(div); + } b = rb_float_new(mod); return rb_assoc_new(a, b); } -- cgit v1.2.3