* numeric.c (num_div): should raise ZeroDivisionError.

* numeric.c (fix_divide): ditto.

* test/ruby/test_numeric.rb (TestNumeric::test_divmod): avoid
  ZeroDivisionError in tests.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16650 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2008-05-27 12:51:28 +00:00
Родитель 8a3f0ce44a
Коммит 77446d45a8
3 изменённых файлов: 16 добавлений и 4 удалений

Просмотреть файл

@ -31,6 +31,13 @@ Tue May 27 15:36:27 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* numeric.c (num_fdiv): fallback to_f should always return float * numeric.c (num_fdiv): fallback to_f should always return float
result. should not use #quo that may return rational. result. should not use #quo that may return rational.
* numeric.c (num_div): should raise ZeroDivisionError.
* numeric.c (fix_divide): ditto.
* test/ruby/test_numeric.rb (TestNumeric::test_divmod): avoid
ZeroDivisionError in tests.
Tue May 27 13:14:53 2008 Akinori MUSHA <knu@iDaemons.org> Tue May 27 13:14:53 2008 Akinori MUSHA <knu@iDaemons.org>
* enum.c (enum_to_a): Pass arguments through to #each(). * enum.c (enum_to_a): Pass arguments through to #each().

Просмотреть файл

@ -288,6 +288,7 @@ static VALUE num_floor(VALUE num);
static VALUE static VALUE
num_div(VALUE x, VALUE y) num_div(VALUE x, VALUE y)
{ {
if (rb_equal(INT2FIX(0), y)) rb_num_zerodiv();
return num_floor(rb_funcall(x, '/', 1, y)); return num_floor(rb_funcall(x, '/', 1, y));
} }
@ -2261,11 +2262,15 @@ fix_divide(VALUE x, VALUE y, ID op)
return rb_big_div(x, y); return rb_big_div(x, y);
case T_FLOAT: case T_FLOAT:
{ {
double div = (double)FIX2LONG(x) / RFLOAT_VALUE(y); double div;
if (op == '/') { if (op == '/') {
div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
return DOUBLE2NUM(div); return DOUBLE2NUM(div);
} }
else { else {
if (RFLOAT_VALUE(y) == 0) rb_num_zerodiv();
div = (double)FIX2LONG(x) / RFLOAT_VALUE(y);
return rb_dbl2big(floor(div)); return rb_dbl2big(floor(div));
} }
} }

Просмотреть файл

@ -60,9 +60,9 @@ class TestNumeric < Test::Unit::TestCase
def %(x); :mod; end def %(x); :mod; end
end end
assert_equal(42, DummyNumeric.new.div(0)) assert_equal(42, DummyNumeric.new.div(1))
assert_equal(:mod, DummyNumeric.new.modulo(0)) assert_equal(:mod, DummyNumeric.new.modulo(1))
assert_equal([42, :mod], DummyNumeric.new.divmod(0)) assert_equal([42, :mod], DummyNumeric.new.divmod(1))
assert_kind_of(Integer, 11.divmod(3.5).first, '[ruby-dev:34006]') assert_kind_of(Integer, 11.divmod(3.5).first, '[ruby-dev:34006]')