From 4d05267ba80f0e55a43dae682e0cb865ccc99683 Mon Sep 17 00:00:00 2001 From: Michael Mclaughlin Date: Sun, 25 Jun 2017 23:35:15 +0100 Subject: [PATCH] Bugfix: #57 Powers of -1 for integers over Number.MAX_SAFE_INTEGER --- decimal.es6.js | 5 +++++ decimal.js | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/decimal.es6.js b/decimal.es6.js index a2fdcc6..7ab6e27 100644 --- a/decimal.es6.js +++ b/decimal.es6.js @@ -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. sign = sign < 0 && y.d[Math.max(e, k)] & 1 ? -1 : 1; + if (x.eq(-1)) { + x.s = sign; + return x; + } + // Estimate result exponent. // x^y = 10^e, where e = y * log10(x) // log10(x) = log10(x_significand) + x_exponent diff --git a/decimal.js b/decimal.js index d3c1227..768c113 100644 --- a/decimal.js +++ b/decimal.js @@ -2283,11 +2283,17 @@ // 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; + if (x.eq(-1)) { + x.s = sign; + return x; + } + // Estimate result exponent. // x^y = 10^e, where e = y * log10(x) // log10(x) = log10(x_significand) + x_exponent // log10(x_significand) = ln(x_significand) / ln(10) k = mathpow(+x, yn); + e = k == 0 || !isFinite(k) ? mathfloor(yn * (Math.log('0.' + digitsToString(x.d)) / Math.LN10 + x.e + 1)) : new Ctor(k + '').e;