1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00

Bugfix: #57 Powers of -1 for integers over Number.MAX_SAFE_INTEGER

This commit is contained in:
Michael Mclaughlin 2017-06-25 23:35:15 +01:00
parent 3566299686
commit 4d05267ba8
2 changed files with 11 additions and 0 deletions

View File

@ -2281,6 +2281,11 @@ P.toPower = P.pow = function (y) {
// Result is negative if x is negative and the last digit of integer y is odd. // Result is negative if x is negative and the last digit of integer y is odd.
sign = sign < 0 && y.d[Math.max(e, k)] & 1 ? -1 : 1; sign = sign < 0 && y.d[Math.max(e, k)] & 1 ? -1 : 1;
if (x.eq(-1)) {
x.s = sign;
return x;
}
// Estimate result exponent. // Estimate result exponent.
// x^y = 10^e, where e = y * log10(x) // x^y = 10^e, where e = y * log10(x)
// log10(x) = log10(x_significand) + x_exponent // log10(x) = log10(x_significand) + x_exponent

View File

@ -2283,11 +2283,17 @@
// Result is negative if x is negative and the last digit of integer y is odd. // Result is negative if x is negative and the last digit of integer y is odd.
sign = sign < 0 && y.d[Math.max(e, k)] & 1 ? -1 : 1; sign = sign < 0 && y.d[Math.max(e, k)] & 1 ? -1 : 1;
if (x.eq(-1)) {
x.s = sign;
return x;
}
// Estimate result exponent. // Estimate result exponent.
// x^y = 10^e, where e = y * log10(x) // x^y = 10^e, where e = y * log10(x)
// log10(x) = log10(x_significand) + x_exponent // log10(x) = log10(x_significand) + x_exponent
// log10(x_significand) = ln(x_significand) / ln(10) // log10(x_significand) = ln(x_significand) / ln(10)
k = mathpow(+x, yn); k = mathpow(+x, yn);
e = k == 0 || !isFinite(k) e = k == 0 || !isFinite(k)
? mathfloor(yn * (Math.log('0.' + digitsToString(x.d)) / Math.LN10 + x.e + 1)) ? mathfloor(yn * (Math.log('0.' + digitsToString(x.d)) / Math.LN10 + x.e + 1))
: new Ctor(k + '').e; : new Ctor(k + '').e;