summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2014-08-26 02:24:18 +0900
committerYukihiro "Matz" Matsumoto <[email protected]>2014-08-26 02:24:18 +0900
commitecc77829ce76ad18995095bd39ffeae3e3663fac (patch)
treecaef850b5a733fe952c2a8a41752d01a591f0608
parentb01b3c44728f66e0db1a9ab66fca7f6e8213f5e5 (diff)
parentbd6dc0da77470c2dfca7fbccf543cb94b17624af (diff)
downloadmruby-ecc77829ce76ad18995095bd39ffeae3e3663fac.tar.gz
mruby-ecc77829ce76ad18995095bd39ffeae3e3663fac.zip
Merge pull request #2562 from cubicdaiya/issues/pow_flo
Fix and add test for Numeric#pow behavior.
-rw-r--r--src/numeric.c7
-rw-r--r--test/t/numeric.rb8
2 files changed, 12 insertions, 3 deletions
diff --git a/src/numeric.c b/src/numeric.c
index 90ea6c965..3f211ca70 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -54,11 +54,12 @@ static mrb_value
num_pow(mrb_state *mrb, mrb_value x)
{
mrb_value y;
- mrb_float d;
+ mrb_float d, yv;
mrb_get_args(mrb, "o", &y);
- d = pow(mrb_to_flo(mrb, x), mrb_to_flo(mrb, y));
- if (mrb_fixnum_p(x) && mrb_fixnum_p(y) && FIXABLE(d))
+ yv = mrb_to_flo(mrb, y);
+ d = pow(mrb_to_flo(mrb, x), yv);
+ if (mrb_fixnum_p(x) && mrb_fixnum_p(y) && FIXABLE(d) && yv > 0)
return mrb_fixnum_value((mrb_int)d);
return mrb_float_value(mrb, d);
}
diff --git a/test/t/numeric.rb b/test/t/numeric.rb
index 2b01611a7..120cc960d 100644
--- a/test/t/numeric.rb
+++ b/test/t/numeric.rb
@@ -18,6 +18,14 @@ assert('Numeric#abs', '15.2.7.4.3') do
assert_equal(1.0, -1.abs)
end
+assert('Numeric#pow') do
+ assert_equal(8, 2 ** 3)
+ assert_equal(-8, -2 ** 3)
+ assert_equal(1, 2 ** 0)
+ assert_equal(1, 2.2 ** 0)
+ assert_equal(0.5, 2 ** -1)
+end
+
assert('Numeric#/', '15.2.8.3.4') do
n = Class.new(Numeric){ def /(x); 15.1;end }.new