diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-07-12 14:13:46 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2016-07-12 14:13:46 +0000 |
commit | 745a2aac690ef026ffd8a3c1ca82ddc61d6babe6 (patch) | |
tree | 30b7be7a49a2c78b48488a0ceb58fd1530000ce5 /math.c | |
parent | 89ed4f41a3cc169893ee50765e7e996a63bbd2e9 (diff) | |
download | ruby-745a2aac690ef026ffd8a3c1ca82ddc61d6babe6.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 'math.c')
-rw-r--r-- | math.c | 15 |
1 files changed, 15 insertions, 0 deletions
@@ -588,8 +588,23 @@ math_log10(VALUE obj, VALUE x) static VALUE math_sqrt(VALUE obj, VALUE x) { + return rb_math_sqrt(x); +} + +VALUE +rb_math_sqrt(VALUE x) +{ double d; + if (RB_TYPE_P(x, T_COMPLEX)) { + int neg = signbit(RCOMPLEX(x)->imag); + double re = Get_Double(RCOMPLEX(x)->real), im; + d = Get_Double(rb_complex_abs(x)); + im = sqrt((d - re) / 2.0); + re = sqrt((d + re) / 2.0); + if (neg) im = -im; + return rb_complex_new(DBL2NUM(re), DBL2NUM(im)); + } d = Get_Double(x); /* check for domain error */ if (d < 0.0) domain_error("sqrt"); |