1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* complex.c (f_{add,mul,sub}): omitted some shortcuts for preserve

signed zero anyway.

	* complex.c (nucomp_negate): new.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19335 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
tadf 2008-09-14 01:16:44 +00:00
parent 19416601a0
commit e4b3a81769
4 changed files with 56 additions and 0 deletions

View file

@ -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

View file

@ -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);
}
@ -523,6 +531,14 @@ nucomp_image(VALUE self)
return dat->image;
}
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)
{
@ -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:
*/

View file

@ -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:
*/

View file

@ -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)