ruby-changes:4529
From: ko1@a...
Date: Tue, 15 Apr 2008 07:31:13 +0900 (JST)
Subject: [ruby-changes:4529] tadf - Ruby:r16022 (trunk): * complex.c (nucomp_div):
tadf 2008-04-15 07:30:45 +0900 (Tue, 15 Apr 2008) New Revision: 16022 Modified files: trunk/ChangeLog trunk/complex.c trunk/math.c trunk/test/ruby/test_complex.rb Log: * complex.c (nucomp_div): [ruby-dev:34357] * complex.c (nucomp_abs): use hypot. * complex.c (nucomp_quo): do not force convertion. * test/ruby/test_complex.rb: omitted some meaningless tests. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_complex.rb?r1=16022&r2=16021&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/math.c?r1=16022&r2=16021&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/complex.c?r1=16022&r2=16021&diff_format=u http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=16022&r2=16021&diff_format=u Index: complex.c =================================================================== --- complex.c (revision 16021) +++ complex.c (revision 16022) @@ -478,6 +478,7 @@ extern VALUE math_cos(VALUE obj, VALUE x); extern VALUE math_cosh(VALUE obj, VALUE x); extern VALUE math_exp(VALUE obj, VALUE x); +extern VALUE math_hypot(VALUE obj, VALUE x, VALUE y); extern VALUE math_log(int argc, VALUE *argv); extern VALUE math_sin(VALUE obj, VALUE x); extern VALUE math_sinh(VALUE obj, VALUE x); @@ -487,6 +488,7 @@ #define m_cos_bang(x) math_cos(Qnil,x) #define m_cosh_bang(x) math_cosh(Qnil,x) #define m_exp_bang(x) math_exp(Qnil,x) +#define m_hypot(x,y) math_hypot(Qnil,x,y) static VALUE m_log_bang(VALUE x) @@ -681,7 +683,21 @@ f_div(dat->image, other)); } case T_COMPLEX: - return f_div(f_mul(self, f_conjugate(other)), f_abs2(other)); + { + get_dat2(self, other); + + if (TYPE(adat->real) == T_FLOAT || + TYPE(adat->image) == T_FLOAT || + TYPE(bdat->real) == T_FLOAT || + TYPE(bdat->image) == T_FLOAT) { + VALUE magn = m_hypot(bdat->real, bdat->image); + VALUE tmp = f_complex_new_bang2(CLASS_OF(self), + f_div(bdat->real, magn), + f_div(bdat->image, magn)); + return f_div(f_mul(self, f_conjugate(tmp)), magn); + } + return f_div(f_mul(self, f_conjugate(other)), f_abs2(other)); + } default: return rb_num_coerce_bin(self, other, '/'); } @@ -693,8 +709,8 @@ get_dat1(self); return f_div(f_complex_new2(CLASS_OF(self), - f_to_r(dat->real), - f_to_r(dat->image)), other); + f_quo(dat->real, ONE), + f_quo(dat->image, ONE)), other); } static VALUE @@ -824,8 +840,7 @@ nucomp_abs(VALUE self) { get_dat1(self); - return m_sqrt(f_add(f_mul(dat->real, dat->real), - f_mul(dat->image, dat->image))); + return m_hypot(dat->real, dat->image); } static VALUE Index: math.c =================================================================== --- math.c (revision 16021) +++ math.c (revision 16022) @@ -465,7 +465,7 @@ * Math.hypot(3, 4) #=> 5.0 */ -static VALUE +VALUE math_hypot(VALUE obj, VALUE x, VALUE y) { Need_Float2(x, y); Index: ChangeLog =================================================================== --- ChangeLog (revision 16021) +++ ChangeLog (revision 16022) @@ -1,3 +1,13 @@ +Tue Apr 15 07:21:21 2008 Tadayoshi Funaba <tadf@d...> + + * complex.c (nucomp_div): [ruby-dev:34357] + + * complex.c (nucomp_abs): use hypot. + + * complex.c (nucomp_quo): do not force convertion. + + * test/ruby/test_complex.rb: omitted some meaningless tests. + Mon Apr 14 23:25:50 2008 Yusuke Endoh <mame@t...> * test/ruby/test_objectspace.rb: add a test for Index: test/ruby/test_complex.rb =================================================================== --- test/ruby/test_complex.rb (revision 16021) +++ test/ruby/test_complex.rb (revision 16022) @@ -1055,6 +1055,7 @@ Complex.const_set(:Unify, unify_val) if f end +=begin def test_abs b = 2**100 def b.*(x); self; end rescue nil @@ -1075,6 +1076,7 @@ nan = inf/inf assert_raise(Errno::EDOM, Errno::ERANGE) { Complex(1, nan).abs } end +=end def test_coerce c = Complex(6, 3) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/