ruby-changes:37642
From: nobu <ko1@a...>
Date: Tue, 24 Feb 2015 22:59:01 +0900 (JST)
Subject: [ruby-changes:37642] nobu:r49723 (trunk): complex.c: multiply as rotation
nobu 2015-02-24 22:58:52 +0900 (Tue, 24 Feb 2015) New Revision: 49723 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=49723 Log: complex.c: multiply as rotation * complex.c (nucomp_mul): calculate as rotation in complex plane if matrix calculation resulted in NaN. Modified files: trunk/ChangeLog trunk/complex.c trunk/test/ruby/test_complex.rb Index: complex.c =================================================================== --- complex.c (revision 49722) +++ complex.c (revision 49723) @@ -17,6 +17,9 @@ https://github.com/ruby/ruby/blob/trunk/complex.c#L17 VALUE rb_cComplex; +static VALUE nucomp_abs(VALUE self); +static VALUE nucomp_arg(VALUE self); + static ID id_abs, id_arg, id_convert, id_denominator, id_eqeq_p, id_expt, id_fdiv, id_negate, id_numerator, id_quo, @@ -720,6 +723,38 @@ nucomp_mul(VALUE self, VALUE other) https://github.com/ruby/ruby/blob/trunk/complex.c#L723 imag = f_add(f_mul(adat->real, bdat->imag), f_mul(adat->imag, bdat->real)); + if ((RB_FLOAT_TYPE_P(real) && isnan(RFLOAT_VALUE(real))) || + (RB_FLOAT_TYPE_P(imag) && isnan(RFLOAT_VALUE(imag)))) { + VALUE abs = f_mul(nucomp_abs(self), nucomp_abs(other)); + VALUE arg = f_add(nucomp_arg(self), nucomp_arg(other)); + if (f_zero_p(arg)) { + real = abs; + imag = INT2FIX(0); + } + else if (RB_FLOAT_TYPE_P(arg)) { + double a = RFLOAT_VALUE(arg); + if (a == M_PI) { + real = f_negate(abs); + imag = INT2FIX(0); + } + else if (a == M_PI/2) { + imag = abs; + real = INT2FIX(0); + } + else if (a == M_PI*3/2) { + imag = f_negate(abs); + real = INT2FIX(0); + } + else { + goto polar; + } + } + else { + polar: + return f_complex_polar(CLASS_OF(self), abs, arg); + } + } + return f_complex_new2(CLASS_OF(self), real, imag); } if (k_numeric_p(other) && f_real_p(other)) { Index: ChangeLog =================================================================== --- ChangeLog (revision 49722) +++ ChangeLog (revision 49723) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Tue Feb 24 22:58:48 2015 Nobuyoshi Nakada <nobu@r...> + + * complex.c (nucomp_mul): calculate as rotation in complex plane + if matrix calculation resulted in NaN. + Tue Feb 24 21:45:39 2015 Kazuki Tanaka <mail@t...> * test/ruby/test_math.rb(test_cbrt): Add an assertion for Math.cbrt(1.0/0) Index: test/ruby/test_complex.rb =================================================================== --- test/ruby/test_complex.rb (revision 49722) +++ test/ruby/test_complex.rb (revision 49723) @@ -289,6 +289,13 @@ class Complex_Test < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/ruby/test_complex.rb#L289 assert_equal(Complex(Rational(2,1),Rational(4)), c * Rational(2)) assert_equal(Complex(Rational(2,3),Rational(4,3)), c * Rational(2,3)) + + c = Complex(Float::INFINITY, 0) + assert_equal(Complex(Float::INFINITY, 0), c * Complex(1, 0)) + assert_equal(Complex(0, Float::INFINITY), c * Complex(0, 1)) + c = Complex(0, Float::INFINITY) + assert_equal(Complex(0, Float::INFINITY), c * Complex(1, 0)) + assert_equal(Complex(-Float::INFINITY, 0), c * Complex(0, 1)) end def test_div -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/