mirror of
				https://github.com/ruby/ruby.git
				synced 2022-11-09 12:17:21 -05:00 
			
		
		
		
	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
This commit is contained in:
		
							parent
							
								
									af2d3c9866
								
							
						
					
					
						commit
						94468b4ea5
					
				
					 7 changed files with 170 additions and 0 deletions
				
			
		
							
								
								
									
										20
									
								
								ChangeLog
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								ChangeLog
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,3 +1,23 @@
 | 
			
		|||
Sun Jul 17 23:42:00 2016  Kenta Murata  <mrkn@mrkn.jp>
 | 
			
		||||
 | 
			
		||||
	* 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?
 | 
			
		||||
 | 
			
		||||
Sun Jul 17 20:59:24 2016  Nobuyoshi Nakada  <nobu@ruby-lang.org>
 | 
			
		||||
 | 
			
		||||
	* common.mk, enc/depend (casefold.h, name2ctype.h): move to
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										65
									
								
								complex.c
									
										
									
									
									
								
							
							
						
						
									
										65
									
								
								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);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										30
									
								
								numeric.c
									
										
									
									
									
								
							
							
						
						
									
										30
									
								
								numeric.c
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -671,6 +671,34 @@ num_nonzero_p(VALUE num)
 | 
			
		|||
    return num;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  call-seq:
 | 
			
		||||
 *     num.finite?  ->  true or false
 | 
			
		||||
 *
 | 
			
		||||
 *  Return true if +num+ is finite number, oterwise returns false.
 | 
			
		||||
 */
 | 
			
		||||
static VALUE
 | 
			
		||||
num_finite_p(VALUE num)
 | 
			
		||||
{
 | 
			
		||||
    return Qtrue;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  call-seq:
 | 
			
		||||
 *     num.infinite?  ->  nil or 1 or -1
 | 
			
		||||
 *
 | 
			
		||||
 *  Returns values corresponding to the value of +num+'s magnitude:
 | 
			
		||||
 *
 | 
			
		||||
 *  +finite+::    +nil+
 | 
			
		||||
 *  +-Infinity+:: +-1+
 | 
			
		||||
 *  ++Infinity+:: ++1+
 | 
			
		||||
 */
 | 
			
		||||
static VALUE
 | 
			
		||||
num_infinite_p(VALUE num)
 | 
			
		||||
{
 | 
			
		||||
    return Qnil;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *  call-seq:
 | 
			
		||||
 *     num.to_int  ->  integer
 | 
			
		||||
| 
						 | 
				
			
			@ -5002,6 +5030,8 @@ Init_Numeric(void)
 | 
			
		|||
    rb_define_method(rb_cNumeric, "integer?", num_int_p, 0);
 | 
			
		||||
    rb_define_method(rb_cNumeric, "zero?", num_zero_p, 0);
 | 
			
		||||
    rb_define_method(rb_cNumeric, "nonzero?", num_nonzero_p, 0);
 | 
			
		||||
    rb_define_method(rb_cNumeric, "finite?", num_finite_p, 0);
 | 
			
		||||
    rb_define_method(rb_cNumeric, "infinite?", num_infinite_p, 0);
 | 
			
		||||
 | 
			
		||||
    rb_define_method(rb_cNumeric, "floor", num_floor, -1);
 | 
			
		||||
    rb_define_method(rb_cNumeric, "ceil", num_ceil, -1);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -778,5 +778,15 @@ class TestBignum < Test::Unit::TestCase
 | 
			
		|||
    assert_raise(TypeError) { T1024P.digits("10") }
 | 
			
		||||
    assert_raise(TypeError) { T1024P.digits("a") }
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_finite_p
 | 
			
		||||
    assert_predicate(T1024P, :finite?)
 | 
			
		||||
    assert_predicate(-T1024P, :finite?)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_infinite_p
 | 
			
		||||
    assert_nil(T1024P.infinite?)
 | 
			
		||||
    assert_nil((-T1024P).infinite?)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -826,6 +826,30 @@ class Complex_Test < Test::Unit::TestCase
 | 
			
		|||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_finite_p
 | 
			
		||||
    assert_predicate(1+1i, :finite?)
 | 
			
		||||
    assert_predicate(1-1i, :finite?)
 | 
			
		||||
    assert_predicate(-1+1i, :finite?)
 | 
			
		||||
    assert_predicate(-1-1i, :finite?)
 | 
			
		||||
    assert_not_predicate(Float::INFINITY + 1i, :finite?)
 | 
			
		||||
    assert_not_predicate(Complex(1, Float::INFINITY), :finite?)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_infinite_p
 | 
			
		||||
    assert_nil((1+1i).infinite?)
 | 
			
		||||
    assert_nil((1-1i).infinite?)
 | 
			
		||||
    assert_nil((-1+1i).infinite?)
 | 
			
		||||
    assert_nil((-1-1i).infinite?)
 | 
			
		||||
    assert_equal(1, (Float::INFINITY + 1i).infinite?)
 | 
			
		||||
    assert_equal(1, (Float::INFINITY - 1i).infinite?)
 | 
			
		||||
    assert_equal(1, (-Float::INFINITY + 1i).infinite?)
 | 
			
		||||
    assert_equal(1, (-Float::INFINITY - 1i).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?)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_supp
 | 
			
		||||
    assert_equal(true, 1.real?)
 | 
			
		||||
    assert_equal(true, 1.1.real?)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -337,4 +337,16 @@ class TestFixnum < Test::Unit::TestCase
 | 
			
		|||
    assert_not_predicate(0, :negative?)
 | 
			
		||||
    assert_not_predicate(1, :negative?)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_finite_p
 | 
			
		||||
    assert_predicate(1, :finite?)
 | 
			
		||||
    assert_predicate(0, :finite?)
 | 
			
		||||
    assert_predicate(-1, :finite?)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_infinite_p
 | 
			
		||||
    assert_nil(1.infinite?)
 | 
			
		||||
    assert_nil(0.infinite?)
 | 
			
		||||
    assert_nil(-1.infinite?)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -939,4 +939,13 @@ class Rational_Test < Test::Unit::TestCase
 | 
			
		|||
  def test_known_bug
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_finite_p
 | 
			
		||||
    assert_predicate(1/2r, :finite?)
 | 
			
		||||
    assert_predicate(-1/2r, :finite?)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def test_infinite_p
 | 
			
		||||
    assert_nil((1/2r).infinite?)
 | 
			
		||||
    assert_nil((-1/2r).infinite?)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue