@phensley/decimal
Advanced tools
Comparing version 0.25.7 to 0.25.8
@@ -206,3 +206,9 @@ import { add, divide, multiply, subtract, trimLeadingZeros, DivMod } from './math'; | ||
var r = this.handleFlags(0 /* ADDITION */, v); | ||
return r === undefined ? this.addsub(this, v, v.sign) : r; | ||
if (r === undefined) { | ||
if (this.isZero()) { | ||
return v; | ||
} | ||
return v.isZero() ? this : this.addsub(this, v, v.sign); | ||
} | ||
return r; | ||
}; | ||
@@ -215,3 +221,9 @@ /** | ||
var r = this.handleFlags(1 /* SUBTRACTION */, v); | ||
return r === undefined ? this.addsub(this, v, -v.sign) : r; | ||
if (r === undefined) { | ||
if (this.isZero()) { | ||
return v.negate(); | ||
} | ||
return v.isZero() ? this : this.addsub(this, v, -v.sign); | ||
} | ||
return r; | ||
}; | ||
@@ -843,6 +855,19 @@ /** | ||
var w = this; | ||
var prec = w.precision(); | ||
// Check if shift exceeds precision, so all digits are shifted to | ||
// zero with no rounding possible. Just set zero and bump the exponent. | ||
if (prec < shift) { | ||
w.data = [0]; | ||
w.exp += shift; | ||
return; | ||
} | ||
// We only want to round up when there is a free zero integer | ||
// digit to the left. We do this when the number is < 0 or | ||
// we're not shifting out all of the digits. | ||
var round = w.alignexp() < 0 || prec !== shift; | ||
var div = new DivMod(); | ||
var _d = div.word(shift, 7 /* RDIGITS */), q = _d[0], r = _d[1]; | ||
var data = w.data.slice(); | ||
w.data.fill(0); | ||
var div = new DivMod(); | ||
var _d = div.word(shift, 7 /* RDIGITS */), q = _d[0], r = _d[1]; | ||
// check if we divided evenly | ||
var i = 0, j = 0; | ||
@@ -860,31 +885,28 @@ var rnd = 0, rest = 0; | ||
} | ||
w.exp += shift; | ||
if (w.round(rnd, rest, mode)) { | ||
w._increment(); | ||
} | ||
else { | ||
var hiprev = 0; | ||
var ph = POWERS10[7 /* RDIGITS */ - r]; | ||
if (q < data.length) { | ||
_b = div.pow10(data[q], r), hiprev = _b[0], rest = _b[1]; | ||
} | ||
w.trim(); | ||
return; | ||
_c = div.pow10(rest, r - 1), rnd = _c[0], rest = _c[1]; | ||
if (rest === 0 && q > 0) { | ||
rest = allzero(data, q) === 0 ? 1 : 0; | ||
} | ||
for (j = 0, i = q + 1; i < data.length; i++, j++) { | ||
var _e = div.pow10(data[i], r), hi = _e[0], lo = _e[1]; | ||
w.data[j] = ph * lo + hiprev; | ||
hiprev = hi; | ||
} | ||
if (hiprev !== 0) { | ||
w.data[j] = hiprev; | ||
} | ||
} | ||
var hiprev = 0; | ||
var ph = POWERS10[7 /* RDIGITS */ - r]; | ||
if (q < data.length) { | ||
_b = div.pow10(data[q], r), hiprev = _b[0], rest = _b[1]; | ||
} | ||
_c = div.pow10(rest, r - 1), rnd = _c[0], rest = _c[1]; | ||
if (rest === 0 && q > 0) { | ||
rest = allzero(data, q) === 0 ? 1 : 0; | ||
} | ||
for (j = 0, i = q + 1; i < data.length; i++, j++) { | ||
var _e = div.pow10(data[i], r), hi = _e[0], lo = _e[1]; | ||
w.data[j] = ph * lo + hiprev; | ||
hiprev = hi; | ||
} | ||
if (hiprev !== 0) { | ||
w.data[j] = hiprev; | ||
} | ||
w.trim(); | ||
w.exp += shift; | ||
if (w.round(rnd, rest, mode)) { | ||
if (round && w.round(rnd, rest, mode)) { | ||
// If precision changes due to rounding, subtract from exponent | ||
w._increment(); | ||
} | ||
w.trim(); | ||
}; | ||
@@ -927,3 +949,2 @@ Decimal.prototype._setScale = function (scale, roundingMode) { | ||
Decimal.prototype._increment = function () { | ||
var z = this.isZero(); | ||
var d = this.data; | ||
@@ -941,6 +962,2 @@ var len = d.length; | ||
} | ||
// Check if we incremented from zero. | ||
if (z) { | ||
this.sign = 1; | ||
} | ||
}; | ||
@@ -947,0 +964,0 @@ /** |
@@ -36,19 +36,29 @@ import { POWERS10 } from './types'; | ||
export var subtract = function (u, v) { | ||
var vlen = v.length; | ||
var n = u.length; | ||
var w = new Array(n); | ||
var m = u.length; | ||
var n = v.length; | ||
var w = new Array(m); | ||
// S1. Initialize | ||
var j = 0; | ||
var k = 0; | ||
// S2. Subtract digits | ||
while (j < n) { | ||
// v may be shorter than u | ||
var vj = j < vlen ? v[j] : 0; | ||
// S2. Subtract digits | ||
var z = u[j] - vj + k; | ||
var z = u[j] - v[j] - k; | ||
w[j] = z < 0 ? z + 10000000 /* RADIX */ : z; | ||
// .. k is set to -1 or 0, to borrow | ||
k = z < 0 ? -1 : 0; | ||
// k is set to 1 or 0, indicating a borrow | ||
k = z < 0 ? 1 : 0; | ||
j++; | ||
// S3. Loop on j | ||
} | ||
// Propagate the borrow flag up | ||
while (k && j < m) { | ||
var z = u[j] - k; | ||
w[j] = z < 0 ? z + 10000000 /* RADIX */ : z; | ||
k = z < 0 ? 1 : 0; | ||
j++; | ||
} | ||
// Borrow done, copy remainder of larger number | ||
while (j < m) { | ||
w[j] = u[j]; | ||
j++; | ||
} | ||
return w; | ||
@@ -128,4 +138,2 @@ }; | ||
q.fill(0); | ||
var r = new Array(m); | ||
r.fill(0); | ||
// D1. Normalize | ||
@@ -194,2 +202,4 @@ var d = (10000000 /* RADIX */ / (v[n - 1] + 1)) | 0; | ||
k = 0; | ||
var r = new Array(n); | ||
r.fill(0); | ||
for (var i = n - 1; i >= 0; i--) { | ||
@@ -196,0 +206,0 @@ p = u[i] + (k * 10000000 /* RADIX */); |
@@ -53,2 +53,5 @@ import { DivMod } from './math'; | ||
export var allzero = function (data, len) { | ||
if (len > data.length) { | ||
return 1; | ||
} | ||
while (--len >= 0) { | ||
@@ -55,0 +58,0 @@ if (data[len] !== 0) { |
@@ -208,3 +208,9 @@ "use strict"; | ||
var r = this.handleFlags(0 /* ADDITION */, v); | ||
return r === undefined ? this.addsub(this, v, v.sign) : r; | ||
if (r === undefined) { | ||
if (this.isZero()) { | ||
return v; | ||
} | ||
return v.isZero() ? this : this.addsub(this, v, v.sign); | ||
} | ||
return r; | ||
}; | ||
@@ -217,3 +223,9 @@ /** | ||
var r = this.handleFlags(1 /* SUBTRACTION */, v); | ||
return r === undefined ? this.addsub(this, v, -v.sign) : r; | ||
if (r === undefined) { | ||
if (this.isZero()) { | ||
return v.negate(); | ||
} | ||
return v.isZero() ? this : this.addsub(this, v, -v.sign); | ||
} | ||
return r; | ||
}; | ||
@@ -845,6 +857,19 @@ /** | ||
var w = this; | ||
var prec = w.precision(); | ||
// Check if shift exceeds precision, so all digits are shifted to | ||
// zero with no rounding possible. Just set zero and bump the exponent. | ||
if (prec < shift) { | ||
w.data = [0]; | ||
w.exp += shift; | ||
return; | ||
} | ||
// We only want to round up when there is a free zero integer | ||
// digit to the left. We do this when the number is < 0 or | ||
// we're not shifting out all of the digits. | ||
var round = w.alignexp() < 0 || prec !== shift; | ||
var div = new math_1.DivMod(); | ||
var _d = div.word(shift, 7 /* RDIGITS */), q = _d[0], r = _d[1]; | ||
var data = w.data.slice(); | ||
w.data.fill(0); | ||
var div = new math_1.DivMod(); | ||
var _d = div.word(shift, 7 /* RDIGITS */), q = _d[0], r = _d[1]; | ||
// check if we divided evenly | ||
var i = 0, j = 0; | ||
@@ -862,31 +887,28 @@ var rnd = 0, rest = 0; | ||
} | ||
w.exp += shift; | ||
if (w.round(rnd, rest, mode)) { | ||
w._increment(); | ||
} | ||
else { | ||
var hiprev = 0; | ||
var ph = types_1.POWERS10[7 /* RDIGITS */ - r]; | ||
if (q < data.length) { | ||
_b = div.pow10(data[q], r), hiprev = _b[0], rest = _b[1]; | ||
} | ||
w.trim(); | ||
return; | ||
_c = div.pow10(rest, r - 1), rnd = _c[0], rest = _c[1]; | ||
if (rest === 0 && q > 0) { | ||
rest = operations_1.allzero(data, q) === 0 ? 1 : 0; | ||
} | ||
for (j = 0, i = q + 1; i < data.length; i++, j++) { | ||
var _e = div.pow10(data[i], r), hi = _e[0], lo = _e[1]; | ||
w.data[j] = ph * lo + hiprev; | ||
hiprev = hi; | ||
} | ||
if (hiprev !== 0) { | ||
w.data[j] = hiprev; | ||
} | ||
} | ||
var hiprev = 0; | ||
var ph = types_1.POWERS10[7 /* RDIGITS */ - r]; | ||
if (q < data.length) { | ||
_b = div.pow10(data[q], r), hiprev = _b[0], rest = _b[1]; | ||
} | ||
_c = div.pow10(rest, r - 1), rnd = _c[0], rest = _c[1]; | ||
if (rest === 0 && q > 0) { | ||
rest = operations_1.allzero(data, q) === 0 ? 1 : 0; | ||
} | ||
for (j = 0, i = q + 1; i < data.length; i++, j++) { | ||
var _e = div.pow10(data[i], r), hi = _e[0], lo = _e[1]; | ||
w.data[j] = ph * lo + hiprev; | ||
hiprev = hi; | ||
} | ||
if (hiprev !== 0) { | ||
w.data[j] = hiprev; | ||
} | ||
w.trim(); | ||
w.exp += shift; | ||
if (w.round(rnd, rest, mode)) { | ||
if (round && w.round(rnd, rest, mode)) { | ||
// If precision changes due to rounding, subtract from exponent | ||
w._increment(); | ||
} | ||
w.trim(); | ||
}; | ||
@@ -929,3 +951,2 @@ Decimal.prototype._setScale = function (scale, roundingMode) { | ||
Decimal.prototype._increment = function () { | ||
var z = this.isZero(); | ||
var d = this.data; | ||
@@ -943,6 +964,2 @@ var len = d.length; | ||
} | ||
// Check if we incremented from zero. | ||
if (z) { | ||
this.sign = 1; | ||
} | ||
}; | ||
@@ -949,0 +966,0 @@ /** |
@@ -38,19 +38,29 @@ "use strict"; | ||
exports.subtract = function (u, v) { | ||
var vlen = v.length; | ||
var n = u.length; | ||
var w = new Array(n); | ||
var m = u.length; | ||
var n = v.length; | ||
var w = new Array(m); | ||
// S1. Initialize | ||
var j = 0; | ||
var k = 0; | ||
// S2. Subtract digits | ||
while (j < n) { | ||
// v may be shorter than u | ||
var vj = j < vlen ? v[j] : 0; | ||
// S2. Subtract digits | ||
var z = u[j] - vj + k; | ||
var z = u[j] - v[j] - k; | ||
w[j] = z < 0 ? z + 10000000 /* RADIX */ : z; | ||
// .. k is set to -1 or 0, to borrow | ||
k = z < 0 ? -1 : 0; | ||
// k is set to 1 or 0, indicating a borrow | ||
k = z < 0 ? 1 : 0; | ||
j++; | ||
// S3. Loop on j | ||
} | ||
// Propagate the borrow flag up | ||
while (k && j < m) { | ||
var z = u[j] - k; | ||
w[j] = z < 0 ? z + 10000000 /* RADIX */ : z; | ||
k = z < 0 ? 1 : 0; | ||
j++; | ||
} | ||
// Borrow done, copy remainder of larger number | ||
while (j < m) { | ||
w[j] = u[j]; | ||
j++; | ||
} | ||
return w; | ||
@@ -130,4 +140,2 @@ }; | ||
q.fill(0); | ||
var r = new Array(m); | ||
r.fill(0); | ||
// D1. Normalize | ||
@@ -196,2 +204,4 @@ var d = (10000000 /* RADIX */ / (v[n - 1] + 1)) | 0; | ||
k = 0; | ||
var r = new Array(n); | ||
r.fill(0); | ||
for (var i = n - 1; i >= 0; i--) { | ||
@@ -198,0 +208,0 @@ p = u[i] + (k * 10000000 /* RADIX */); |
@@ -55,2 +55,5 @@ "use strict"; | ||
exports.allzero = function (data, len) { | ||
if (len > data.length) { | ||
return 1; | ||
} | ||
while (--len >= 0) { | ||
@@ -57,0 +60,0 @@ if (data[len] !== 0) { |
{ | ||
"name": "@phensley/decimal", | ||
"version": "0.25.7", | ||
"version": "0.25.8", | ||
"description": "Arbitrary precision decimal math", | ||
@@ -65,3 +65,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "f0a3dec6085c0efa0661285198c89be3dd068c5e" | ||
"gitHead": "65fbe5997828a0f34966c7f31bf21e262a4fb6ab" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
253843
4337