aboutsummaryrefslogtreecommitdiffstats
path: root/complex.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-12 14:13:46 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2016-07-12 14:13:46 +0000
commit6967900fd910a767bbe7804ec4f21b027cedd735 (patch)
tree30b7be7a49a2c78b48488a0ceb58fd1530000ce5 /complex.c
parenta395aca709c9a9ffa73b7f69071a540672005973 (diff)
downloadruby-6967900fd910a767bbe7804ec4f21b027cedd735.tar.gz
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
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c34
1 files changed, 22 insertions, 12 deletions
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