From 745a2aac690ef026ffd8a3c1ca82ddc61d6babe6 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 12 Jul 2016 14:13:46 +0000 Subject: math.c: Complex sqrt * math.c (rb_math_sqrt): [EXPERIMENTAL] move Complex sqrt support from mathn.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55646 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- complex.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'complex.c') diff --git a/complex.c b/complex.c index 9419ea9818..fc6ecbb0e0 100644 --- a/complex.c +++ b/complex.c @@ -535,6 +535,21 @@ m_sin(VALUE x) #if 0 imp1(sqrt) +VALUE +rb_complex_sqrt(VALUE x) +{ + int pos; + VALUE a, re, im; + get_dat1(x); + + pos = f_positive_p(dat->imag); + a = f_abs(x); + re = m_sqrt_bang(f_div(f_add(a, dat->real), TWO)); + im = m_sqrt_bang(f_div(f_sub(a, dat->real), TWO)); + if (!pos) im = f_negate(im); + return f_complex_new2(rb_cComplex, re, im); +} + static VALUE m_sqrt(VALUE x) { @@ -543,18 +558,7 @@ m_sqrt(VALUE x) return m_sqrt_bang(x); return f_complex_new2(rb_cComplex, ZERO, m_sqrt_bang(f_negate(x))); } - else { - get_dat1(x); - - if (f_negative_p(dat->imag)) - return f_conj(m_sqrt(f_conj(x))); - else { - VALUE a = f_abs(x); - return f_complex_new2(rb_cComplex, - m_sqrt_bang(f_div(f_add(a, dat->real), TWO)), - m_sqrt_bang(f_div(f_sub(a, dat->real), TWO))); - } - } + return rb_complex_sqrt(x); } #endif @@ -1415,6 +1419,12 @@ rb_complex_set_imag(VALUE cmp, VALUE i) return cmp; } +VALUE +rb_complex_abs(VALUE cmp) +{ + return nucomp_abs(cmp); +} + /* * call-seq: * cmp.to_i -> integer -- cgit v1.2.3