mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
rational.c: zero division
* rational.c (read_rat_nos): denominator cannot be 0, raise zero division in that case. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
1e07906a42
commit
b296e29035
2 changed files with 6 additions and 0 deletions
|
@ -2396,6 +2396,7 @@ read_rat_nos(const char **s, int sign, int strict, VALUE *num)
|
||||||
if (**s == '/') {
|
if (**s == '/') {
|
||||||
(*s)++;
|
(*s)++;
|
||||||
if (!read_digits(s, strict, &den, NULL)) goto failed;
|
if (!read_digits(s, strict, &den, NULL)) goto failed;
|
||||||
|
if (den == ZERO) rb_num_zerodiv();
|
||||||
nurat_reduce(num, &den);
|
nurat_reduce(num, &den);
|
||||||
if (div != ONE && den != ONE)
|
if (div != ONE && den != ONE)
|
||||||
den = rb_int_mul(den, div);
|
den = rb_int_mul(den, div);
|
||||||
|
|
|
@ -776,6 +776,11 @@ class Rational_Test < Test::Unit::TestCase
|
||||||
ng[5, 3, '5/3x']
|
ng[5, 3, '5/3x']
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_parse_zero_denominator
|
||||||
|
assert_raise(ZeroDivisionError) {"1/0".to_r}
|
||||||
|
assert_raise(ZeroDivisionError) {Rational("1/0")}
|
||||||
|
end
|
||||||
|
|
||||||
def test_to_i
|
def test_to_i
|
||||||
assert_equal(1, Rational(3,2).to_i)
|
assert_equal(1, Rational(3,2).to_i)
|
||||||
assert_equal(1, Integer(Rational(3,2)))
|
assert_equal(1, Integer(Rational(3,2)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue