From ed00379c79dcfd50867befa0a13f015aff695c00 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 24 Feb 2015 13:59:06 +0000 Subject: [PATCH] numeric.c: calculate complex numbers * numeric.c (fix_plus, fix_mul): calculate complex numbers for commutative operations. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49724 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- complex.c | 10 ++++++---- numeric.c | 8 ++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/complex.c b/complex.c index a169c95b71..9e352a8c39 100644 --- a/complex.c +++ b/complex.c @@ -674,11 +674,12 @@ f_addsub(VALUE self, VALUE other, * 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) * 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) } return rb_num_coerce_bin(self, other, '*'); } +#define nucomp_mul rb_nucomp_mul inline static VALUE f_divide(VALUE self, VALUE other, diff --git a/numeric.c b/numeric.c index 2474b49114..1950754f60 100644 --- a/numeric.c +++ b/numeric.c @@ -2864,6 +2864,10 @@ fix_plus(VALUE x, VALUE y) 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) 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, '*'); }