ruby-changes:37643
From: nobu <ko1@a...>
Date: Tue, 24 Feb 2015 22:59:12 +0900 (JST)
Subject: [ruby-changes:37643] nobu:r49724 (trunk): numeric.c: calculate complex numbers
nobu 2015-02-24 22:59:06 +0900 (Tue, 24 Feb 2015) New Revision: 49724 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49724 Log: numeric.c: calculate complex numbers * numeric.c (fix_plus, fix_mul): calculate complex numbers for commutative operations. Modified files: trunk/complex.c trunk/numeric.c Index: complex.c =================================================================== --- complex.c (revision 49723) +++ complex.c (revision 49724) @@ -674,11 +674,12 @@ f_addsub(VALUE self, VALUE other, https://github.com/ruby/ruby/blob/trunk/complex.c#L674 * Complex(9, 8) + 4 #=> (13+8i) * Complex(20, 9) + 9.8 #=> (29.8+9i) */ -static VALUE -nucomp_add(VALUE self, VALUE other) +VALUE +rb_nucomp_add(VALUE self, VALUE other) { return f_addsub(self, other, f_add, '+'); } +#define nucomp_add rb_nucomp_add /* * call-seq: @@ -710,8 +711,8 @@ nucomp_sub(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/complex.c#L711 * Complex(9, 8) * 4 #=> (36+32i) * Complex(20, 9) * 9.8 #=> (196.0+88.2i) */ -static VALUE -nucomp_mul(VALUE self, VALUE other) +VALUE +rb_nucomp_mul(VALUE self, VALUE other) { if (k_complex_p(other)) { VALUE real, imag; @@ -766,6 +767,7 @@ nucomp_mul(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/complex.c#L767 } return rb_num_coerce_bin(self, other, '*'); } +#define nucomp_mul rb_nucomp_mul inline static VALUE f_divide(VALUE self, VALUE other, Index: numeric.c =================================================================== --- numeric.c (revision 49723) +++ numeric.c (revision 49724) @@ -2864,6 +2864,10 @@ fix_plus(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2864 else if (RB_TYPE_P(y, T_FLOAT)) { return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y)); } + else if (RB_TYPE_P(y, T_COMPLEX)) { + VALUE rb_nucomp_add(VALUE, VALUE); + return rb_nucomp_add(y, x); + } else { return rb_num_coerce_bin(x, y, '+'); } @@ -2953,6 +2957,10 @@ fix_mul(VALUE x, VALUE y) https://github.com/ruby/ruby/blob/trunk/numeric.c#L2957 else if (RB_TYPE_P(y, T_FLOAT)) { return DBL2NUM((double)FIX2LONG(x) * RFLOAT_VALUE(y)); } + else if (RB_TYPE_P(y, T_COMPLEX)) { + VALUE rb_nucomp_mul(VALUE, VALUE); + return rb_nucomp_mul(y, x); + } else { return rb_num_coerce_bin(x, y, '*'); } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/