aboutsummaryrefslogtreecommitdiffstats
path: root/complex.c
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-17 14:53:00 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-17 14:53:00 +0000
commit06704527f8f9547f08c21a6975a9c2ee1eb63f0e (patch)
treef2e4cfaf96d362d3d00fc73c9624adb41f6edc07 /complex.c
parentae9d17e198fcab2f797c0b5bceebdf5e6742e6cd (diff)
downloadruby-06704527f8f9547f08c21a6975a9c2ee1eb63f0e.tar.gz
numeric.c, complex.c: Add finite? and infinite? consistent with Float
* numeric.c (num_finite_p, num_infinite_p): Add Numeric#finite? and Numeric#infinite? [Feature #12039] [ruby-core:73618] * complex.c (rb_complex_finite_p): Add Complex#finite? * complex.c (rb_complex_infinite_p): Add Complex#infinite? * test/ruby/test_bignum.rb: Add test for Integer#finite? and Integer#infinite? * test/ruby/test_fixnum.rb: ditto. * test/ruby/test_rational.rb: Add test for Rational#finite? and Rational#infinite? * test/ruby/test_complex.rb: Add test for Complex#finite? and Complex#infinite? git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55702 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c65
1 files changed, 65 insertions, 0 deletions
diff --git a/complex.c b/complex.c
index fc6ecbb0e0..def0758276 100644
--- a/complex.c
+++ b/complex.c
@@ -1331,6 +1331,68 @@ nucomp_inspect(VALUE self)
return s;
}
+/*
+ * call-seq:
+ * cmp.finite? -> true or false
+ *
+ * Returns +true+ if +cmp+'s magnitude is finite number,
+ * oterwise returns +false+.
+ */
+static VALUE
+rb_complex_finite_p(VALUE self)
+{
+ VALUE magnitude = nucomp_abs(self);
+ double f;
+
+ switch (TYPE(magnitude)) {
+ case T_FIXNUM: case T_BIGNUM: case T_RATIONAL:
+ return Qtrue;
+
+ case T_FLOAT:
+ f = RFLOAT_VALUE(magnitude);
+ return isinf(f) ? Qfalse : Qtrue;
+
+ default:
+ return rb_funcall(magnitude, rb_intern("finite?"), 0);
+ }
+}
+
+/*
+ * call-seq:
+ * cmp.infinite? -> nil or 1 or -1
+ *
+ * Returns values corresponding to the value of +cmp+'s magnitude:
+ *
+ * +finite+:: +nil+
+ * ++Infinity+:: ++1+
+ *
+ * For example:
+ *
+ * (1+1i).infinite? #=> nil
+ * (Float::INFINITY + 1i).infinite? #=> 1
+ */
+static VALUE
+rb_complex_infinite_p(VALUE self)
+{
+ VALUE magnitude = nucomp_abs(self);
+ double f;
+
+ switch (TYPE(magnitude)) {
+ case T_FIXNUM: case T_BIGNUM: case T_RATIONAL:
+ return Qnil;
+
+ case T_FLOAT:
+ f = RFLOAT_VALUE(magnitude);
+ if (isinf(f)) {
+ return INT2FIX(f < 0 ? -1 : 1);
+ }
+ return Qnil;
+
+ default:
+ return rb_funcall(magnitude, rb_intern("infinite?"), 0);
+ }
+}
+
/* :nodoc: */
static VALUE
nucomp_dumper(VALUE self)
@@ -2233,6 +2295,9 @@ Init_Complex(void)
rb_undef_method(rb_cComplex, "positive?");
rb_undef_method(rb_cComplex, "negative?");
+ rb_define_method(rb_cComplex, "finite?", rb_complex_finite_p, 0);
+ rb_define_method(rb_cComplex, "infinite?", rb_complex_infinite_p, 0);
+
rb_define_private_method(rb_cComplex, "marshal_dump", nucomp_marshal_dump, 0);
compat = rb_define_class_under(rb_cComplex, "compatible", rb_cObject); /* :nodoc: */
rb_define_private_method(compat, "marshal_load", nucomp_marshal_load, 1);