complex.c: FINITE_TYPE_P

* complex.c (FINITE_TYPE_P): extract predicate.
* complex.c (rb_complex_finite_p, rb_complex_infinite_p): use
  dedicated predicates instead of switch by TYPE.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56507 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-10-28 06:19:00 +00:00
parent 8998c06461
commit 2226372268
2 changed files with 21 additions and 16 deletions

View File

@ -1,3 +1,10 @@
Fri Oct 28 15:18:58 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* complex.c (FINITE_TYPE_P): extract predicate.
* complex.c (rb_complex_finite_p, rb_complex_infinite_p): use
dedicated predicates instead of switch by TYPE.
Thu Oct 27 23:28:12 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> Thu Oct 27 23:28:12 2016 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/ruby.h (rb_integer_type_p): turn into macro to help * include/ruby/ruby.h (rb_integer_type_p): turn into macro to help

View File

@ -1331,6 +1331,8 @@ nucomp_inspect(VALUE self)
return s; return s;
} }
#define FINITE_TYPE_P(v) (RB_INTEGER_TYPE_P(v) || RB_TYPE_P(v, T_RATIONAL))
/* /*
* call-seq: * call-seq:
* cmp.finite? -> true or false * cmp.finite? -> true or false
@ -1342,17 +1344,15 @@ static VALUE
rb_complex_finite_p(VALUE self) rb_complex_finite_p(VALUE self)
{ {
VALUE magnitude = nucomp_abs(self); VALUE magnitude = nucomp_abs(self);
double f;
switch (TYPE(magnitude)) { if (FINITE_TYPE_P(magnitude)) {
case T_FIXNUM: case T_BIGNUM: case T_RATIONAL:
return Qtrue; return Qtrue;
}
case T_FLOAT: else if (RB_FLOAT_TYPE_P(magnitude)) {
f = RFLOAT_VALUE(magnitude); const double f = RFLOAT_VALUE(magnitude);
return isinf(f) ? Qfalse : Qtrue; return isinf(f) ? Qfalse : Qtrue;
}
default: else {
return rb_funcall(magnitude, rb_intern("finite?"), 0); return rb_funcall(magnitude, rb_intern("finite?"), 0);
} }
} }
@ -1375,20 +1375,18 @@ static VALUE
rb_complex_infinite_p(VALUE self) rb_complex_infinite_p(VALUE self)
{ {
VALUE magnitude = nucomp_abs(self); VALUE magnitude = nucomp_abs(self);
double f;
switch (TYPE(magnitude)) { if (FINITE_TYPE_P(magnitude)) {
case T_FIXNUM: case T_BIGNUM: case T_RATIONAL:
return Qnil; return Qnil;
}
case T_FLOAT: if (RB_FLOAT_TYPE_P(magnitude)) {
f = RFLOAT_VALUE(magnitude); const double f = RFLOAT_VALUE(magnitude);
if (isinf(f)) { if (isinf(f)) {
return INT2FIX(f < 0 ? -1 : 1); return INT2FIX(f < 0 ? -1 : 1);
} }
return Qnil; return Qnil;
}
default: else {
return rb_funcall(magnitude, rb_intern("infinite?"), 0); return rb_funcall(magnitude, rb_intern("infinite?"), 0);
} }
} }