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

ruby-changes:7814

From: tadf <ko1@a...>
Date: Sun, 14 Sep 2008 10:18:34 +0900 (JST)
Subject: [ruby-changes:7814] Ruby:r19335 (trunk): * complex.c (f_{add,mul,sub}): omitted some shortcuts for preserve

tadf	2008-09-14 10:16:44 +0900 (Sun, 14 Sep 2008)

  New Revision: 19335

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

  Log:
    * complex.c (f_{add,mul,sub}): omitted some shortcuts for preserve
      signed zero anyway.
    
    * complex.c (nucomp_negate): new.

  Modified files:
    trunk/ChangeLog
    trunk/complex.c
    trunk/rational.c
    trunk/test/ruby/test_complex.rb

Index: complex.c
===================================================================
--- complex.c	(revision 19334)
+++ complex.c	(revision 19335)
@@ -64,13 +64,17 @@
     return rb_funcall(rb_mMath, id_##n, 2, x, y);\
 }
 
+#define PRESERVE_SIGNEDZERO
+
 inline static VALUE
 f_add(VALUE x, VALUE y)
 {
+#ifndef PRESERVE_SIGNEDZERO
     if (FIXNUM_P(y) && FIX2LONG(y) == 0)
 	return x;
     else if (FIXNUM_P(x) && FIX2LONG(x) == 0)
 	return y;
+#endif
     return rb_funcall(x, '+', 1, y);
 }
 
@@ -117,6 +121,7 @@
 inline static VALUE
 f_mul(VALUE x, VALUE y)
 {
+#ifndef PRESERVE_SIGNEDZERO
     if (FIXNUM_P(y)) {
 	long iy = FIX2LONG(y);
 	if (iy == 0) {
@@ -135,14 +140,17 @@
 	else if (ix == 1)
 	    return y;
     }
+#endif
     return rb_funcall(x, '*', 1, y);
 }
 
 inline static VALUE
 f_sub(VALUE x, VALUE y)
 {
+#ifndef PRESERVE_SIGNEDZERO
     if (FIXNUM_P(y) && FIX2LONG(y) == 0)
 	return x;
+#endif
     return rb_funcall(x, '-', 1, y);
 }
 
@@ -524,6 +532,14 @@
 }
 
 static VALUE
+nucomp_negate(VALUE self)
+{
+  get_dat1(self);
+  return f_complex_new2(CLASS_OF(self),
+			f_negate(dat->real), f_negate(dat->image));
+}
+
+static VALUE
 nucomp_add(VALUE self, VALUE other)
 {
     if (k_complex_p(other)) {
@@ -1393,6 +1409,7 @@
     rb_define_method(rb_cComplex, "image", nucomp_image, 0);
     rb_define_method(rb_cComplex, "imag", nucomp_image, 0);
 
+    rb_define_method(rb_cComplex, "-@", nucomp_negate, 0);
     rb_define_method(rb_cComplex, "+", nucomp_add, 1);
     rb_define_method(rb_cComplex, "-", nucomp_sub, 1);
     rb_define_method(rb_cComplex, "*", nucomp_mul, 1);
@@ -1474,3 +1491,9 @@
     rb_define_const(rb_cComplex, "I",
 		    f_complex_new_bang2(rb_cComplex, ZERO, ONE));
 }
+
+/*
+Local variables:
+c-file-style: "ruby"
+end:
+*/
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 19334)
+++ ChangeLog	(revision 19335)
@@ -1,3 +1,10 @@
+Sun Sep 14 10:10:43 2008  Tadayoshi Funaba  <tadf@d...>
+
+	* complex.c (f_{add,mul,sub}): omitted some shortcuts for preserve
+	  signed zero anyway.
+
+	* complex.c (nucomp_negate): new.
+
 Sun Sep 14 04:15:16 2008  Tanaka Akira  <akr@f...>
 
 	* include/ruby/oniguruma.h (OnigEncodingTypeST): add end argument for
Index: test/ruby/test_complex.rb
===================================================================
--- test/ruby/test_complex.rb	(revision 19334)
+++ test/ruby/test_complex.rb	(revision 19335)
@@ -303,6 +303,16 @@
     assert_equal(Complex(-1,1), +Complex(-1,1))
     assert_equal(Complex(1,-1), +Complex(1,-1))
     assert_equal(Complex(-1,-1), +Complex(-1,-1))
+
+    if -0.0.to_s == '-0.0'
+      c = +Complex(0.0,0.0)
+      assert_equal('0.0', c.real.to_s)
+      assert_equal('0.0', c.image.to_s)
+
+      c = +Complex(-0.0,-0.0)
+      assert_equal('-0.0', c.real.to_s)
+      assert_equal('-0.0', c.image.to_s)
+    end
   end
 
   def test_negate
@@ -313,6 +323,16 @@
     assert_equal(Complex(-1,1), -Complex(1,-1))
     assert_equal(Complex(1,1), -Complex(-1,-1))
 
+    if -0.0.to_s == '-0.0'
+      c = -Complex(0.0,0.0)
+      assert_equal('-0.0', c.real.to_s)
+      assert_equal('-0.0', c.image.to_s)
+
+      c = -Complex(-0.0,-0.0)
+      assert_equal('0.0', c.real.to_s)
+      assert_equal('0.0', c.image.to_s)
+    end
+
 =begin
     assert_equal(0, Complex(0).negate)
     assert_equal(-2, Complex(2).negate)
Index: rational.c
===================================================================
--- rational.c	(revision 19334)
+++ rational.c	(revision 19335)
@@ -1592,3 +1592,9 @@
     rb_define_singleton_method(rb_cRational, "induced_from",
 			       nurat_s_induced_from, 1);
 }
+
+/*
+Local variables:
+c-file-style: "ruby"
+end:
+*/

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

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