Skip to content

Commit 8151d0c

Browse files
committed
[js] Fix nqp::pow_I.
1 parent 768b20d commit 8151d0c

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/vm/js/nqp-runtime/bignum.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ function intishBool(b) {
2626
return b ? 1 : 0;
2727
}
2828

29+
function makeNum(type, num) {
30+
var instance = type._STable.REPR.allocate(type._STable);
31+
instance.$$setNum(num);
32+
return instance;
33+
}
34+
2935
function makeBI(type, num) {
3036
var instance = type._STable.REPR.allocate(type._STable);
3137
instance.$$setBignum(num);
@@ -93,8 +99,22 @@ op.div_I = function(a, b, type) {
9399
return makeBI(type, getBI(a).div(getBI(b)));
94100
};
95101

96-
op.pow_I = function(a, b, type) {
97-
return makeBI(type, getBI(a).pow(getBI(b)));
102+
op.pow_I = function(a, b, numType, biType) {
103+
var base = getBI(a);
104+
var exponent = getBI(b);
105+
if (exponent.lt(0)) {
106+
return makeNum(numType, Math.pow(base.toNumber(), exponent.toNumber()));
107+
} else {
108+
if (exponent.gt(4294967296) && !base.eq(1) && !base.eq(0)) {
109+
if (base.eq(-1)) {
110+
// workaround a bug in bignum
111+
return makeBI(biType, bignum(exponent.mod(2).eq(0) ? 1 : -1));
112+
} else {
113+
return makeNum(numType, base.lt(0) && exponent.mod(2).eq(1) ? Number.NEGATIVE_INFINITY : Number.POSITIVE_INFINITY);
114+
}
115+
}
116+
return makeBI(biType, base.pow(exponent));
117+
}
98118
};
99119

100120
op.mod_I = function(n, m, type) {

0 commit comments

Comments
 (0)