mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Integer.sqrt argument check
* numeric.c (rb_int_s_isqrt): check if the argument is an integer. [Feature #13219] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57706 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
bdd6b995f9
commit
8c44061928
2 changed files with 3 additions and 2 deletions
|
@ -5158,6 +5158,7 @@ static VALUE
|
||||||
rb_int_s_isqrt(VALUE self, VALUE num)
|
rb_int_s_isqrt(VALUE self, VALUE num)
|
||||||
{
|
{
|
||||||
unsigned long n, sq;
|
unsigned long n, sq;
|
||||||
|
num = rb_to_int(num);
|
||||||
if (FIXNUM_P(num)) {
|
if (FIXNUM_P(num)) {
|
||||||
if (FIXNUM_NEGATIVE_P(num)) {
|
if (FIXNUM_NEGATIVE_P(num)) {
|
||||||
domain_error("isqrt");
|
domain_error("isqrt");
|
||||||
|
@ -5166,7 +5167,7 @@ rb_int_s_isqrt(VALUE self, VALUE num)
|
||||||
sq = rb_ulong_isqrt(n);
|
sq = rb_ulong_isqrt(n);
|
||||||
return LONG2FIX(sq);
|
return LONG2FIX(sq);
|
||||||
}
|
}
|
||||||
if (RB_TYPE_P(num, T_BIGNUM)) {
|
else {
|
||||||
size_t biglen;
|
size_t biglen;
|
||||||
if (RBIGNUM_NEGATIVE_P(num)) {
|
if (RBIGNUM_NEGATIVE_P(num)) {
|
||||||
domain_error("isqrt");
|
domain_error("isqrt");
|
||||||
|
@ -5183,7 +5184,6 @@ rb_int_s_isqrt(VALUE self, VALUE num)
|
||||||
#endif
|
#endif
|
||||||
return rb_big_isqrt(num);
|
return rb_big_isqrt(num);
|
||||||
}
|
}
|
||||||
return Qnil;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -466,6 +466,7 @@ class TestInteger < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_square_root
|
def test_square_root
|
||||||
|
assert_raise(TypeError) {Integer.sqrt("x")}
|
||||||
assert_raise(Math::DomainError) {Integer.sqrt(-1)}
|
assert_raise(Math::DomainError) {Integer.sqrt(-1)}
|
||||||
assert_equal(0, Integer.sqrt(0))
|
assert_equal(0, Integer.sqrt(0))
|
||||||
(1...4).each {|i| assert_equal(1, Integer.sqrt(i))}
|
(1...4).each {|i| assert_equal(1, Integer.sqrt(i))}
|
||||||
|
|
Loading…
Add table
Reference in a new issue