complex.c: no overflow
* complex.c (rb_complex_finite_p): get rid of overflow and unnecessary multiplication. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60040 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
9eed577cf5
commit
241ba38d70
28
complex.c
28
complex.c
@ -237,6 +237,22 @@ f_zero_p(VALUE x)
|
|||||||
|
|
||||||
#define f_nonzero_p(x) (!f_zero_p(x))
|
#define f_nonzero_p(x) (!f_zero_p(x))
|
||||||
|
|
||||||
|
VALUE rb_flo_is_finite_p(VALUE num);
|
||||||
|
inline static int
|
||||||
|
f_finite_p(VALUE x)
|
||||||
|
{
|
||||||
|
if (RB_INTEGER_TYPE_P(x)) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
else if (RB_FLOAT_TYPE_P(x)) {
|
||||||
|
return (int)rb_flo_is_finite_p(x);
|
||||||
|
}
|
||||||
|
else if (RB_TYPE_P(x, T_RATIONAL)) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
return RTEST(rb_funcallv(x, id_finite_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)
|
||||||
{
|
{
|
||||||
@ -1326,18 +1342,12 @@ nucomp_inspect(VALUE self)
|
|||||||
static VALUE
|
static VALUE
|
||||||
rb_complex_finite_p(VALUE self)
|
rb_complex_finite_p(VALUE self)
|
||||||
{
|
{
|
||||||
VALUE magnitude = nucomp_abs(self);
|
get_dat1(self);
|
||||||
|
|
||||||
if (FINITE_TYPE_P(magnitude)) {
|
if (f_finite_p(dat->real) && f_finite_p(dat->imag)) {
|
||||||
return Qtrue;
|
return Qtrue;
|
||||||
}
|
}
|
||||||
else if (RB_FLOAT_TYPE_P(magnitude)) {
|
return Qfalse;
|
||||||
const double f = RFLOAT_VALUE(magnitude);
|
|
||||||
return isinf(f) ? Qfalse : Qtrue;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return rb_funcall(magnitude, id_finite_p, 0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1763,8 +1763,8 @@ flo_is_infinite_p(VALUE num)
|
|||||||
* i.e. it is not infinite and Float#nan? is +false+.
|
* i.e. it is not infinite and Float#nan? is +false+.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static VALUE
|
VALUE
|
||||||
flo_is_finite_p(VALUE num)
|
rb_flo_is_finite_p(VALUE num)
|
||||||
{
|
{
|
||||||
double value = RFLOAT_VALUE(num);
|
double value = RFLOAT_VALUE(num);
|
||||||
|
|
||||||
@ -5592,7 +5592,7 @@ Init_Numeric(void)
|
|||||||
|
|
||||||
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?", flo_is_infinite_p, 0);
|
||||||
rb_define_method(rb_cFloat, "finite?", 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);
|
||||||
rb_define_method(rb_cFloat, "positive?", flo_positive_p, 0);
|
rb_define_method(rb_cFloat, "positive?", flo_positive_p, 0);
|
||||||
|
@ -832,6 +832,9 @@ class Complex_Test < Test::Unit::TestCase
|
|||||||
assert_predicate(-1-1i, :finite?)
|
assert_predicate(-1-1i, :finite?)
|
||||||
assert_not_predicate(Float::INFINITY + 1i, :finite?)
|
assert_not_predicate(Float::INFINITY + 1i, :finite?)
|
||||||
assert_not_predicate(Complex(1, Float::INFINITY), :finite?)
|
assert_not_predicate(Complex(1, Float::INFINITY), :finite?)
|
||||||
|
assert_predicate(Complex(Float::MAX, 0.0), :finite?)
|
||||||
|
assert_predicate(Complex(0.0, Float::MAX), :finite?)
|
||||||
|
assert_predicate(Complex(Float::MAX, Float::MAX), :finite?)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_infinite_p
|
def test_infinite_p
|
||||||
|
Loading…
x
Reference in New Issue
Block a user