diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | complex.c | 23 | ||||
-rw-r--r-- | rational.c | 6 | ||||
-rw-r--r-- | test/ruby/test_complex.rb | 20 |
4 files changed, 56 insertions, 0 deletions
@@ -1,3 +1,10 @@ +Sun Sep 14 10:10:43 2008 Tadayoshi Funaba <[email protected]> + + * 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 <[email protected]> * include/ruby/oniguruma.h (OnigEncodingTypeST): add end argument for @@ -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 7274ace1ff..4e0c5a12ab 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 ef612e2279..6f9cabb7b9 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) |