[前][次][番号順一覧][スレッド一覧]

ruby-changes:38441

From: nobu <ko1@a...>
Date: Sun, 17 May 2015 15:01:58 +0900 (JST)
Subject: [ruby-changes:38441] nobu:r50522 (trunk): numeric.c: Numeric#positive? and Numeric#negative?

nobu	2015-05-17 15:01:47 +0900 (Sun, 17 May 2015)

  New Revision: 50522

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50522

  Log:
    numeric.c: Numeric#positive? and Numeric#negative?
    
    * numeric.c (num_positive_p, num_negative_p): add methods
      Numeric#positive? and Numeric#negative?.
      [ruby-core:69173] [Feature #11151]
    * numeric.c (flo_positive_p, flo_negative_p): specialiazed
      functions for Float.
    * complex.c (Init_Complex): Complex do not have positive? and
      negative? methods

  Modified files:
    trunk/ChangeLog
    trunk/NEWS
    trunk/complex.c
    trunk/numeric.c
    trunk/test/ruby/test_fixnum.rb
    trunk/test/ruby/test_float.rb
    trunk/test/ruby/test_numeric.rb
    trunk/test/ruby/test_rational.rb
Index: complex.c
===================================================================
--- complex.c	(revision 50521)
+++ complex.c	(revision 50522)
@@ -2222,6 +2222,9 @@ Init_Complex(void) https://github.com/ruby/ruby/blob/trunk/complex.c#L2222
     rb_define_method(rb_cComplex, "to_s", nucomp_to_s, 0);
     rb_define_method(rb_cComplex, "inspect", nucomp_inspect, 0);
 
+    rb_undef_method(rb_cComplex, "positive?");
+    rb_undef_method(rb_cComplex, "negative?");
+
     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);
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 50521)
+++ ChangeLog	(revision 50522)
@@ -1,3 +1,15 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun May 17 15:01:26 2015  Nobuyoshi Nakada  <nobu@r...>
+
+	* numeric.c (num_positive_p, num_negative_p): add methods
+	  Numeric#positive? and Numeric#negative?.
+	  [ruby-core:69173] [Feature #11151]
+
+	* numeric.c (flo_positive_p, flo_negative_p): specialiazed
+	  functions for Float.
+
+	* complex.c (Init_Complex): Complex do not have positive? and
+	  negative? methods
+
 Sun May 17 14:57:42 2015  Eric Wong  <e@8...>
 
 	* lib/webrick/server.rb (accept_client): avoid redundant fcntl call
Index: NEWS
===================================================================
--- NEWS	(revision 50521)
+++ NEWS	(revision 50522)
@@ -19,6 +19,11 @@ with all sufficient information, see the https://github.com/ruby/ruby/blob/trunk/NEWS#L19
 
   * Enumerable#grep_v is added as inverse version of Enumerable#grep.
 
+* Numeric
+
+  * Numeric#positive? and Numeric#negative? are added, which return
+    true when the receiver is positive and negative respectively.
+
 === Core classes compatibility issues (excluding feature bug fixes)
 
 * Array
Index: numeric.c
===================================================================
--- numeric.c	(revision 50521)
+++ numeric.c	(revision 50522)
@@ -648,6 +648,42 @@ num_to_int(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L648
     return rb_funcallv(num, id_to_i, 0, 0);
 }
 
+/*
+ *  call-seq:
+ *     num.positive? ->  true or false
+ *
+ *  Returns +true+ if +num+ is greater than 0.
+ */
+
+static VALUE
+num_positive_p(VALUE num)
+{
+    const ID mid = '>';
+
+    if (FIXNUM_P(num)) {
+	if (method_basic_p(rb_cFixnum))
+	    return (SIGNED_VALUE)num > (SIGNED_VALUE)INT2FIX(0);
+    }
+    else if (RB_TYPE_P(num, T_BIGNUM)) {
+	if (method_basic_p(rb_cBignum))
+	    return BIGNUM_POSITIVE_P(num);
+    }
+    return RTEST(compare_with_zero(num, mid));
+}
+
+/*
+ *  call-seq:
+ *     num.positive? ->  true or false
+ *
+ *  Returns +true+ if +num+ is less than 0.
+ */
+
+static VALUE
+num_negative_p(VALUE num)
+{
+    return negative_int_p(num) ? Qtrue : Qfalse;
+}
+
 
 /********************************************************************
  *
@@ -1830,6 +1866,34 @@ flo_truncate(VALUE num) https://github.com/ruby/ruby/blob/trunk/numeric.c#L1866
 
 /*
  *  call-seq:
+ *     float.positive? ->  true or false
+ *
+ *  Returns +true+ if +float+ is greater than 0.
+ */
+
+static VALUE
+flo_positive_p(VALUE num)
+{
+    double f = RFLOAT_VALUE(num);
+    return f > 0.0 ? Qtrue : Qfalse;
+}
+
+/*
+ *  call-seq:
+ *     float.negative? ->  true or false
+ *
+ *  Returns +true+ if +float+ is less than 0.
+ */
+
+static VALUE
+flo_negative_p(VALUE num)
+{
+    double f = RFLOAT_VALUE(num);
+    return f < 0.0 ? Qtrue : Qfalse;
+}
+
+/*
+ *  call-seq:
  *     num.floor  ->  integer
  *
  *  Returns the largest integer less than or equal to +num+.
@@ -4062,6 +4126,8 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4126
     rb_define_method(rb_cNumeric, "round", num_round, -1);
     rb_define_method(rb_cNumeric, "truncate", num_truncate, 0);
     rb_define_method(rb_cNumeric, "step", num_step, -1);
+    rb_define_method(rb_cNumeric, "positive?", num_positive_p, 0);
+    rb_define_method(rb_cNumeric, "negative?", num_negative_p, 0);
 
     rb_cInteger = rb_define_class("Integer", rb_cNumeric);
     rb_undef_alloc_func(rb_cInteger);
@@ -4265,6 +4331,8 @@ Init_Numeric(void) https://github.com/ruby/ruby/blob/trunk/numeric.c#L4331
     rb_define_method(rb_cFloat, "finite?",   flo_is_finite_p, 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, "positive?", flo_positive_p, 0);
+    rb_define_method(rb_cFloat, "negative?", flo_negative_p, 0);
 
     id_to = rb_intern("to");
     id_by = rb_intern("by");
Index: test/ruby/test_float.rb
===================================================================
--- test/ruby/test_float.rb	(revision 50521)
+++ test/ruby/test_float.rb	(revision 50522)
@@ -356,6 +356,30 @@ class TestFloat < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_float.rb#L356
     assert_not_predicate(1.0, :zero?)
   end
 
+  def test_positive_p
+    assert_predicate(+1.0, :positive?)
+    assert_not_predicate(+0.0, :positive?)
+    assert_not_predicate(-0.0, :positive?)
+    assert_not_predicate(-1.0, :positive?)
+    assert_predicate(+(0.0.next_float), :positive?)
+    assert_not_predicate(-(0.0.next_float), :positive?)
+    assert_predicate(Float::INFINITY, :positive?)
+    assert_not_predicate(-Float::INFINITY, :positive?)
+    assert_not_predicate(Float::NAN, :positive?)
+  end
+
+  def test_negative_p
+    assert_predicate(-1.0, :negative?)
+    assert_not_predicate(-0.0, :negative?)
+    assert_not_predicate(+0.0, :negative?)
+    assert_not_predicate(+1.0, :negative?)
+    assert_predicate(-(0.0.next_float), :negative?)
+    assert_not_predicate(+(0.0.next_float), :negative?)
+    assert_predicate(-Float::INFINITY, :negative?)
+    assert_not_predicate(Float::INFINITY, :negative?)
+    assert_not_predicate(Float::NAN, :negative?)
+  end
+
   def test_infinite_p
     inf = Float::INFINITY
     assert_equal(1, inf.infinite?)
Index: test/ruby/test_numeric.rb
===================================================================
--- test/ruby/test_numeric.rb	(revision 50521)
+++ test/ruby/test_numeric.rb	(revision 50522)
@@ -146,6 +146,30 @@ class TestNumeric < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_numeric.rb#L146
     assert_predicate(a, :zero?)
   end
 
+  def test_positive_p
+    a = Class.new(Numeric) do
+      def >(x); true; end
+    end.new
+    assert_predicate(a, :positive?)
+
+    a = Class.new(Numeric) do
+      def >(x); false; end
+    end.new
+    assert_not_predicate(a, :positive?)
+  end
+
+  def test_negative_p
+    a = Class.new(Numeric) do
+      def <(x); true; end
+    end.new
+    assert_predicate(a, :negative?)
+
+    a = Class.new(Numeric) do
+      def <(x); false; end
+    end.new
+    assert_not_predicate(a, :negative?)
+  end
+
   def test_to_int
     a = Class.new(Numeric) do
       def to_i; :ok; end
Index: test/ruby/test_fixnum.rb
===================================================================
--- test/ruby/test_fixnum.rb	(revision 50521)
+++ test/ruby/test_fixnum.rb	(revision 50522)
@@ -312,4 +312,21 @@ class TestFixnum < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_fixnum.rb#L312
     assert_equal(1, 5.remainder(4))
     assert_predicate(4.remainder(Float::NAN), :nan?)
   end
+
+  def test_zero_p
+    assert_predicate(0, :zero?)
+    assert_not_predicate(1, :zero?)
+  end
+
+  def test_positive_p
+    assert_predicate(1, :positive?)
+    assert_not_predicate(0, :positive?)
+    assert_not_predicate(-1, :positive?)
+  end
+
+  def test_negative_p
+    assert_predicate(-1, :negative?)
+    assert_not_predicate(0, :negative?)
+    assert_not_predicate(1, :negative?)
+  end
 end
Index: test/ruby/test_rational.rb
===================================================================
--- test/ruby/test_rational.rb	(revision 50521)
+++ test/ruby/test_rational.rb	(revision 50522)
@@ -921,6 +921,16 @@ class Rational_Test < Test::Unit::TestCa https://github.com/ruby/ruby/blob/trunk/test/ruby/test_rational.rb#L921
     assert_raise(ZeroDivisionError, bug5713) { Rational(0, 1) ** Rational(-2,3) }
   end
 
+  def test_positive_p
+    assert_predicate(1/2r, :positive?)
+    assert_not_predicate(-1/2r, :positive?)
+  end
+
+  def test_negative_p
+    assert_predicate(-1/2r, :negative?)
+    assert_not_predicate(1/2r, :negative?)
+  end
+
   def test_known_bug
   end
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]