complex.c: no overflow
* complex.c (rb_complex_infinite_p): get rid of overflow and unnecessary multiplication. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60041 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
241ba38d70
commit
65d7479920
31
complex.c
31
complex.c
@ -253,6 +253,22 @@ f_finite_p(VALUE x)
|
|||||||
return RTEST(rb_funcallv(x, id_finite_p, 0, 0));
|
return RTEST(rb_funcallv(x, id_finite_p, 0, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE rb_flo_is_infinite_p(VALUE num);
|
||||||
|
inline static VALUE
|
||||||
|
f_infinite_p(VALUE x)
|
||||||
|
{
|
||||||
|
if (RB_INTEGER_TYPE_P(x)) {
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
else if (RB_FLOAT_TYPE_P(x)) {
|
||||||
|
return rb_flo_is_infinite_p(x);
|
||||||
|
}
|
||||||
|
else if (RB_TYPE_P(x, T_RATIONAL)) {
|
||||||
|
return Qnil;
|
||||||
|
}
|
||||||
|
return rb_funcallv(x, id_infinite_p, 0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
inline static int
|
inline static int
|
||||||
f_kind_of_p(VALUE x, VALUE c)
|
f_kind_of_p(VALUE x, VALUE c)
|
||||||
{
|
{
|
||||||
@ -1367,21 +1383,12 @@ rb_complex_finite_p(VALUE self)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_complex_infinite_p(VALUE self)
|
rb_complex_infinite_p(VALUE self)
|
||||||
{
|
{
|
||||||
VALUE magnitude = nucomp_abs(self);
|
get_dat1(self);
|
||||||
|
|
||||||
if (FINITE_TYPE_P(magnitude)) {
|
if (NIL_P(f_infinite_p(dat->real)) && NIL_P(f_infinite_p(dat->imag))) {
|
||||||
return Qnil;
|
return Qnil;
|
||||||
}
|
}
|
||||||
if (RB_FLOAT_TYPE_P(magnitude)) {
|
return ONE;
|
||||||
const double f = RFLOAT_VALUE(magnitude);
|
|
||||||
if (isinf(f)) {
|
|
||||||
return ONE;
|
|
||||||
}
|
|
||||||
return Qnil;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return rb_funcall(magnitude, id_infinite_p, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* :nodoc: */
|
/* :nodoc: */
|
||||||
|
@ -1743,8 +1743,8 @@ flo_is_nan_p(VALUE num)
|
|||||||
* (+1.0/0.0).infinite? #=> 1
|
* (+1.0/0.0).infinite? #=> 1
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
VALUE
|
||||||
flo_is_infinite_p(VALUE num)
|
rb_flo_is_infinite_p(VALUE num)
|
||||||
{
|
{
|
||||||
double value = RFLOAT_VALUE(num);
|
double value = RFLOAT_VALUE(num);
|
||||||
|
|
||||||
@ -5591,7 +5591,7 @@ Init_Numeric(void)
|
|||||||
rb_define_method(rb_cFloat, "truncate", flo_truncate, -1);
|
rb_define_method(rb_cFloat, "truncate", flo_truncate, -1);
|
||||||
|
|
||||||
rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0);
|
rb_define_method(rb_cFloat, "nan?", flo_is_nan_p, 0);
|
||||||
rb_define_method(rb_cFloat, "infinite?", flo_is_infinite_p, 0);
|
rb_define_method(rb_cFloat, "infinite?", rb_flo_is_infinite_p, 0);
|
||||||
rb_define_method(rb_cFloat, "finite?", rb_flo_is_finite_p, 0);
|
rb_define_method(rb_cFloat, "finite?", rb_flo_is_finite_p, 0);
|
||||||
rb_define_method(rb_cFloat, "next_float", flo_next_float, 0);
|
rb_define_method(rb_cFloat, "next_float", flo_next_float, 0);
|
||||||
rb_define_method(rb_cFloat, "prev_float", flo_prev_float, 0);
|
rb_define_method(rb_cFloat, "prev_float", flo_prev_float, 0);
|
||||||
|
@ -850,6 +850,9 @@ class Complex_Test < Test::Unit::TestCase
|
|||||||
assert_equal(1, Complex(-1, Float::INFINITY).infinite?)
|
assert_equal(1, Complex(-1, Float::INFINITY).infinite?)
|
||||||
assert_equal(1, Complex(1, -Float::INFINITY).infinite?)
|
assert_equal(1, Complex(1, -Float::INFINITY).infinite?)
|
||||||
assert_equal(1, Complex(-1, -Float::INFINITY).infinite?)
|
assert_equal(1, Complex(-1, -Float::INFINITY).infinite?)
|
||||||
|
assert_nil(Complex(Float::MAX, 0.0).infinite?)
|
||||||
|
assert_nil(Complex(0.0, Float::MAX).infinite?)
|
||||||
|
assert_nil(Complex(Float::MAX, Float::MAX).infinite?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_supp
|
def test_supp
|
||||||
|
Loading…
x
Reference in New Issue
Block a user