summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-14 01:16:44 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-09-14 01:16:44 +0000
commitae1630ba2faaf110999797d60241f8a397e53f4b (patch)
tree5809e4e36f761a07aab1fe7252840f8b625d153c
parent5bc0e4b0229344e7b2611ebb40bd094f93277530 (diff)
downloadruby-ae1630ba2faaf110999797d60241f8a397e53f4b.tar.gz
ruby-ae1630ba2faaf110999797d60241f8a397e53f4b.tar.xz
ruby-ae1630ba2faaf110999797d60241f8a397e53f4b.zip
* complex.c (f_{add,mul,sub}): omitted some shortcuts for preserve
signed zero anyway. * complex.c (nucomp_negate): new. git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@19335 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--complex.c23
-rw-r--r--rational.c6
-rw-r--r--test/ruby/test_complex.rb20
4 files changed, 56 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index c99d66005..c06fbcd54 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Sun Sep 14 10:10:43 2008 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * 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@fsij.org>
* include/ruby/oniguruma.h (OnigEncodingTypeST): add end argument for
diff --git a/complex.c b/complex.c
index 4d7dc2901..07b822356 100644
--- a/complex.c
+++ b/complex.c
@@ -64,13 +64,17 @@ m_##n(VALUE x, VALUE y)\
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 @@ binop(mod, '%')
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 @@ f_mul(VALUE x, VALUE y)
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 @@ nucomp_image(VALUE self)
}
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 @@ Init_Complex(void)
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 @@ Init_Complex(void)
rb_define_const(rb_cComplex, "I",
f_complex_new_bang2(rb_cComplex, ZERO, ONE));
}
+
+/*
+Local variables:
+c-file-style: "ruby"
+end:
+*/
diff --git a/rational.c b/rational.c
index 7274ace1f..4e0c5a12a 100644
--- a/rational.c
+++ b/rational.c
@@ -1592,3 +1592,9 @@ Init_Rational(void)
rb_define_singleton_method(rb_cRational, "induced_from",
nurat_s_induced_from, 1);
}
+
+/*
+Local variables:
+c-file-style: "ruby"
+end:
+*/
diff --git a/test/ruby/test_complex.rb b/test/ruby/test_complex.rb
index ef612e227..6f9cabb7b 100644
--- a/test/ruby/test_complex.rb
+++ b/test/ruby/test_complex.rb
@@ -303,6 +303,16 @@ class Complex_Test < Test::Unit::TestCase
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 @@ class Complex_Test < Test::Unit::TestCase
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)