summaryrefslogtreecommitdiff
path: root/ext/bigdecimal
diff options
context:
space:
mode:
authormrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-10 14:05:07 +0000
committermrkn <mrkn@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2011-07-10 14:05:07 +0000
commit99f8f14d89308c17d8e9e53e03dd5ead6984e7fc (patch)
tree0282d70e2ad245a3f1cdec88f7f0929694592960 /ext/bigdecimal
parent819c2418b11d911f9ff0c81ad13a82f43b1092ee (diff)
* ext/bigdecimal/bigdecimal.c (rmpd_power_by_big_decimal): fix
precision treatment errors. * test/bigdecimal/test_bigdecimal.rb: add tests for the above change. fix precision treatment errors. * ext/bigdecimal/bigdecimal.c (BigDecimal_power): precision argument should be optional for its compatibility. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@32500 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/bigdecimal')
-rw-r--r--ext/bigdecimal/bigdecimal.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/ext/bigdecimal/bigdecimal.c b/ext/bigdecimal/bigdecimal.c
index 15d2c2c..0997dd6 100644
--- a/ext/bigdecimal/bigdecimal.c
+++ b/ext/bigdecimal/bigdecimal.c
@@ -1932,15 +1932,15 @@ is_even(VALUE x)
static VALUE
rmpd_power_by_big_decimal(Real const* x, Real const* exp, ssize_t const n)
{
- VALUE log_x, multiplied, y;
+ VALUE log_x, multiplied, y, vn;
if (VpIsZero(exp)) {
return ToValue(VpCreateRbObject(n, "1"));
}
- log_x = BigMath_log(x->obj, n);
- multiplied = BigDecimal_mult2(exp->obj, log_x, SSIZET2NUM(n));
- y = BigMath_exp(multiplied, n);
+ log_x = BigMath_log(x->obj, SSIZET2NUM(n+1));
+ multiplied = BigDecimal_mult2(exp->obj, log_x, SSIZET2NUM(n+1));
+ y = BigMath_exp(multiplied, SSIZET2NUM(n));
return y;
}
@@ -1954,15 +1954,18 @@ rmpd_power_by_big_decimal(Real const* x, Real const* exp, ssize_t const n)
* Also available as the operator **
*/
static VALUE
-BigDecimal_power(VALUE self, VALUE vexp, VALUE prec)
+BigDecimal_power(int argc, VALUE*argv, VALUE self)
{
ENTER(5);
+ VALUE vexp, prec;
Real* exp = NULL;
Real *x, *y;
ssize_t mp, ma, n;
SIGNED_VALUE int_exp;
double d;
+ rb_scan_args(argc, argv, "11", &vexp, &prec);
+
GUARD_OBJ(x, GetVpValue(self, 1));
n = NIL_P(prec) ? (ssize_t)(x->Prec*VpBaseFig()) : NUM2SSIZET(prec);
@@ -2179,7 +2182,7 @@ retry:
static VALUE
BigDecimal_power_op(VALUE self, VALUE exp)
{
- return BigDecimal_power(self, exp, Qnil);
+ return BigDecimal_power(1, &exp, self);
}
/* call-seq:
@@ -2910,7 +2913,7 @@ Init_bigdecimal(void)
rb_define_method(rb_cBigDecimal, "frac", BigDecimal_frac, 0);
rb_define_method(rb_cBigDecimal, "floor", BigDecimal_floor, -1);
rb_define_method(rb_cBigDecimal, "ceil", BigDecimal_ceil, -1);
- rb_define_method(rb_cBigDecimal, "power", BigDecimal_power, 2);
+ rb_define_method(rb_cBigDecimal, "power", BigDecimal_power, -1);
rb_define_method(rb_cBigDecimal, "**", BigDecimal_power_op, 1);
rb_define_method(rb_cBigDecimal, "<=>", BigDecimal_comp, 1);
rb_define_method(rb_cBigDecimal, "==", BigDecimal_eq, 1);