summaryrefslogtreecommitdiff
path: root/complex.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-17 02:37:32 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-06-17 02:37:32 +0000
commit4fedad85efa84f25cad199eb325d3a300778a046 (patch)
tree98e8c59f05a3a61e4e271d2e1ab100494a59b954 /complex.c
parent90d065530784a110681ccef1326d63eac9cdc469 (diff)
refine Integer#** and Float#**
* complex.c (rb_dbl_complex_polar): utility function, which returns more precise value in right angle cases. * bignum.c (rb_big_pow): use rb_dbl_complex_polar(). * numeric.c (rb_float_pow, fix_pow): create a Complex by polar form. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63678 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'complex.c')
-rw-r--r--complex.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/complex.c b/complex.c
index e9bfc02f7a..2b452e23c0 100644
--- a/complex.c
+++ b/complex.c
@@ -540,6 +540,28 @@ f_complex_polar(VALUE klass, VALUE x, VALUE y)
f_mul(x, m_sin(y)));
}
+/* returns a Complex or Float of ang*PI-rotated abs */
+VALUE
+rb_dbl_complex_polar(double abs, double ang)
+{
+ double fi;
+ const double fr = modf(ang, &fi);
+ int pos = fr == +0.5;
+
+ if (pos || fr == -0.5) {
+ if ((modf(fi / 2.0, &fi) != fr) ^ pos) abs = -abs;
+ return rb_complex_new(RFLOAT_0, DBL2NUM(abs));
+ }
+ else if (fr == 0.0) {
+ if (modf(fi / 2.0, &fi) != 0.0) abs = -abs;
+ return DBL2NUM(abs);
+ }
+ else {
+ ang *= M_PI;
+ return rb_complex_new(DBL2NUM(abs * cos(ang)), DBL2NUM(abs * sin(ang)));
+ }
+}
+
/*
* call-seq:
* Complex.polar(abs[, arg]) -> complex