summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-08-16 23:03:45 +0000
committertadf <tadf@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-08-16 23:03:45 +0000
commit02a7cbec822ba2f2c1b830a9f1747e84822aaddf (patch)
tree850fd24aeb7b04a1eb4cc090e687110227ac1c0c
parentb21088b431e03d15032caad160888e16b8f2198b (diff)
* numeric.c (flo_pow,fix_pow): may return complex number.
* bignum.c (rb_big_pow): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24563 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog6
-rw-r--r--bignum.c2
-rw-r--r--numeric.c17
3 files changed, 21 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 20629994ce..ae7b37df4c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+Mon Aug 17 07:59:00 2009 Tadayoshi Funaba <tadf@dotrb.org>
+
+ * numeric.c (flo_pow,fix_pow): may return complex number.
+
+ * bignum.c (rb_big_pow): ditto.
+
Mon Aug 17 07:16:10 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
* numeric.c (num_imaginary): num#i to return imaginary counterpart
diff --git a/bignum.c b/bignum.c
index f2104f042c..7f02bcace7 100644
--- a/bignum.c
+++ b/bignum.c
@@ -2544,6 +2544,8 @@ rb_big_pow(VALUE x, VALUE y)
switch (TYPE(y)) {
case T_FLOAT:
d = RFLOAT_VALUE(y);
+ if ((!RBIGNUM_SIGN(x) && !BIGZEROP(x)) && d != round(d))
+ return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
break;
case T_BIGNUM:
diff --git a/numeric.c b/numeric.c
index 4ba69fd5d9..dedd8e2ee5 100644
--- a/numeric.c
+++ b/numeric.c
@@ -814,7 +814,6 @@ flo_divmod(VALUE x, VALUE y)
* Raises <code>float</code> the <code>other</code> power.
*
* 2.0**3 #=> 8.0
- * (-8.0)**0.5 #=> NaN # try (-8.0)**Complex(0.5)
*/
static VALUE
@@ -826,7 +825,13 @@ flo_pow(VALUE x, VALUE y)
case T_BIGNUM:
return DBL2NUM(pow(RFLOAT_VALUE(x), rb_big2dbl(y)));
case T_FLOAT:
- return DBL2NUM(pow(RFLOAT_VALUE(x), RFLOAT_VALUE(y)));
+ {
+ double dx = RFLOAT_VALUE(x);
+ double dy = RFLOAT_VALUE(y);
+ if (dx < 0 && dy != round(dy))
+ return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
+ return DBL2NUM(pow(dx, dy));
+ }
default:
return rb_num_coerce_bin(x, y, rb_intern("**"));
}
@@ -2468,7 +2473,6 @@ int_pow(long x, unsigned long y)
* 2 ** 3 #=> 8
* 2 ** -1 #=> 0.5
* 2 ** 0.5 #=> 1.4142135623731
- * (-8)**0.5 #=> NaN # try (-8)**Complex(0.5)
*/
static VALUE
@@ -2517,7 +2521,12 @@ fix_pow(VALUE x, VALUE y)
return DBL2NUM(RFLOAT_VALUE(y) < 0 ? infinite_value() : 0.0);
}
if (a == 1) return DBL2NUM(1.0);
- return DBL2NUM(pow((double)a, RFLOAT_VALUE(y)));
+ {
+ double dy = RFLOAT_VALUE(y);
+ if (a < 0 && dy != round(dy))
+ return rb_funcall(rb_complex_raw1(x), rb_intern("**"), 1, y);
+ return DBL2NUM(pow((double)a, dy));
+ }
default:
return rb_num_coerce_bin(x, y, rb_intern("**"));
}