New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@phensley/decimal

Package Overview
Dependencies
Maintainers
1
Versions
201
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@phensley/decimal - npm Package Compare versions

Comparing version 0.14.4 to 0.14.5

53

lib-es/decimal.d.ts
import { NumberOperands } from './operands';
import { DecimalFormatter, Part } from './format';
import { MathContext, RoundingModeType } from './types';
import { DecimalFlag, MathContext, RoundingModeType } from './types';
declare const enum Op {
ADDITION = 0,
SUBTRACTION = 1,
MULTIPLICATION = 2,
DIVISION = 3,
MOD = 4
}
export declare const DECIMAL_DIGITS: string[];

@@ -16,3 +23,7 @@ export declare type DecimalArg = number | string | Decimal;

protected exp: number;
protected flag: DecimalFlag;
constructor(num: DecimalArg);
isNaN(): boolean;
isFinite(): boolean;
isInfinity(): boolean;
/**

@@ -26,2 +37,4 @@ * Compare decimal u to v, returning the following:

* If the abs flag is true compare the absolute values.
*
* Any NAN argument will always return -1.
*/

@@ -31,2 +44,4 @@ compare(v: DecimalArg, abs?: boolean): number;

* Compute operands for this number, used for determining the plural category.
*
* A NAN or INFINITY will return the same operands as ZERO.
*/

@@ -160,6 +175,26 @@ operands(): NumberOperands;

format<R>(formatter: DecimalFormatter<R>, decimal: string, group: string, minInt: number, minGroup: number, priGroup: number, secGroup: number, zeroScale: boolean, digits?: string[]): void;
protected formatFlags(): string;
protected formatFlagsParts(): Part[];
protected formatString(d: Decimal, minInt: number): string;
protected formatParts(d: Decimal, minInt: number): Part[];
protected static fromRaw(sign: number, exp: number, data: number[]): Decimal;
/**
* Handle setting of flags for operations per the IEEE-754-2008 specification.
* These rules are also referenced in the EcmaScript specification:
*
* 12.7.3.1 - Applying the mul operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mul-operator
*
* 12.7.3.2 - Applying the div operator:
* https://tc39.github.io/ecma262/#sec-applying-the-div-operator
*
* 12.7.3.3 - Applying the mod operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mod-operator
*
* 12.8.5 - Applying the additive operators to numbers:
* https://tc39.github.io/ecma262/#sec-applying-the-additive-operators-to-numbers
*
*/
protected handleFlags(op: Op, v: Decimal): Decimal | undefined;
protected static fromRaw(sign: number, exp: number, data: number[], flag: DecimalFlag): Decimal;
/**
* Mutating in-place shift left.

@@ -175,6 +210,2 @@ */

/**
* Check for u/0 or 0/v cases.
*/
protected checkDivision(v: Decimal): boolean;
/**
* Trim leading zeros from a result and reset sign and exponent accordingly.

@@ -206,3 +237,7 @@ */

*
* Expects strings of the form: "[-+][digits][.][digits][eE][-+][digits]"
* Expects strings of the form:
* "[-+][digits][.][digits][eE][-+][digits]"
* or:
* "[nN]a[nN]" for a NaN
* "[-+]?[iI]nfinity" for positive or negative infinity
*/

@@ -217,2 +252,6 @@ protected _parse(str: string): string | undefined;

E: Decimal;
NAN: Decimal;
POSITIVE_INFINITY: Decimal;
NEGATIVE_INFINITY: Decimal;
};
export {};

@@ -11,2 +11,6 @@ import { add, divide, multiply, subtract, trimLeadingZeros, DivMod } from './math';

var DEFAULT_PRECISION = 28;
var EMPTY = [];
var NAN_VALUES = new Set(['nan', 'NaN']);
var POS_INFINITY = new Set(['infinity', '+infinity', 'Infinity', '+Infinity']);
var NEG_INFINITY = new Set(['-infinity', '-Infinity']);
export var DECIMAL_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

@@ -51,2 +55,6 @@ export var coerceDecimal = function (n) {

function Decimal(num) {
this.data = EMPTY;
this.sign = 0;
this.exp = 0;
this.flag = 0 /* NONE */;
if (typeof num === 'string' || typeof num === 'number') {

@@ -59,4 +67,14 @@ this.parse(num);

this.exp = num.exp;
this.flag = num.flag;
}
}
Decimal.prototype.isNaN = function () {
return this.flag === 1 /* NAN */;
};
Decimal.prototype.isFinite = function () {
return this.flag === 0;
};
Decimal.prototype.isInfinity = function () {
return this.flag === 2 /* INFINITY */;
};
/**

@@ -70,2 +88,4 @@ * Compare decimal u to v, returning the following:

* If the abs flag is true compare the absolute values.
*
* Any NAN argument will always return -1.
*/

@@ -76,2 +96,27 @@ Decimal.prototype.compare = function (v, abs) {

v = coerceDecimal(v);
if (u.flag || v.flag) {
// NAN is never equal to itself or any other value
if (u.flag === 1 /* NAN */ || v.flag === 1 /* NAN */) {
return -1;
}
// INFINITY
// Infinities can be equal if their sign matches
if (u.flag === v.flag) {
return u.sign === v.sign ? 0 : u.sign === -1 ? -1 : 1;
}
// Negative infinity before all other values
// Positive infinity after all other values
return u.flag === 2 /* INFINITY */ ?
u.sign === -1 ? -1 : 1 :
v.sign === -1 ? -1 : 1;
}
// TODO: improve representation of zero and sign, since with
// current representation there are many edge cases. Below
// code fixes issues of comparisons with zero.
if (u.sign === 0) {
return v.sign === 0 ? 0 : v.sign === 1 ? -1 : 1;
}
else if (v.sign === 0) {
return u.sign;
}
var us = u.sign;

@@ -113,2 +158,4 @@ var vs = v.sign;

* Compute operands for this number, used for determining the plural category.
*
* A NAN or INFINITY will return the same operands as ZERO.
*/

@@ -122,3 +169,3 @@ Decimal.prototype.operands = function () {

Decimal.prototype.abs = function () {
return this.sign === -1 ? Decimal.fromRaw(-this.sign, this.exp, this.data) : this;
return this.sign === -1 ? Decimal.fromRaw(-this.sign, this.exp, this.data, this.flag) : this;
};

@@ -129,3 +176,3 @@ /**

Decimal.prototype.negate = function () {
return this.sign === 0 ? this : Decimal.fromRaw(-this.sign, this.exp, this.data);
return this.sign === 0 ? this : Decimal.fromRaw(-this.sign, this.exp, this.data, this.flag);
};

@@ -149,2 +196,5 @@ /**

Decimal.prototype.isInteger = function () {
if (this.flag) {
return false;
}
return this.sign === 0 ? true : this.exp + this.trailingZeros() >= 0;

@@ -156,3 +206,3 @@ };

Decimal.prototype.toInteger = function () {
return this.setScale(0, 'down');
return this.flag ? this : this.setScale(0, 'down');
};

@@ -164,3 +214,4 @@ /**

v = coerceDecimal(v);
return this.addsub(this, v, v.sign);
var r = this.handleFlags(0 /* ADDITION */, v);
return r === undefined ? this.addsub(this, v, v.sign) : r;
};

@@ -172,3 +223,4 @@ /**

v = coerceDecimal(v);
return this.addsub(this, v, v.sign === 1 ? -1 : 1);
var r = this.handleFlags(1 /* SUBTRACTION */, v);
return r === undefined ? this.addsub(this, v, v.sign === 1 ? -1 : 1) : r;
};

@@ -180,4 +232,8 @@ /**

var _a = parseMathContext('half-even', context), usePrecision = _a[0], scaleprec = _a[1], rounding = _a[2];
v = coerceDecimal(v);
var r = this.handleFlags(2 /* MULTIPLICATION */, v);
if (r !== undefined) {
return r;
}
var u = this;
v = coerceDecimal(v);
var w = new Decimal(ZERO);

@@ -210,7 +266,8 @@ w.exp = (u.exp + v.exp);

Decimal.prototype.divide = function (v, context) {
var _a = parseMathContext('half-even', context), usePrecision = _a[0], scaleprec = _a[1], rounding = _a[2];
v = coerceDecimal(v);
if (this.checkDivision(v)) {
return usePrecision ? ZERO : ZERO.setScale(scaleprec);
var r = this.handleFlags(3 /* DIVISION */, v);
if (r !== undefined) {
return r;
}
var _a = parseMathContext('half-even', context), usePrecision = _a[0], scaleprec = _a[1], rounding = _a[2];
var u = this;

@@ -275,4 +332,6 @@ if (!usePrecision) {

v = coerceDecimal(v);
if (this.checkDivision(v)) {
return [ZERO, ZERO];
var rq = this.handleFlags(3 /* DIVISION */, v);
if (rq !== undefined) {
var rm = this.handleFlags(4 /* MOD */, v);
return [rq, rm];
}

@@ -314,3 +373,5 @@ var u = this;

Decimal.prototype.mod = function (v) {
return this.divmod(v)[1];
v = coerceDecimal(v);
var r = this.handleFlags(4 /* MOD */, v);
return r === undefined ? this.divmod(v)[1] : r;
};

@@ -321,2 +382,5 @@ /**

Decimal.prototype.trailingZeros = function () {
if (this.flag) {
return 0;
}
var d = this.data;

@@ -342,2 +406,5 @@ var len = d.length;

Decimal.prototype.stripTrailingZeros = function () {
if (this.flag) {
return this;
}
var r = new Decimal(this);

@@ -353,6 +420,9 @@ r._stripTrailingZeros();

if (minIntDigits === void 0) { minIntDigits = 1; }
if (this.flag) {
return [this, 0];
}
minIntDigits = minIntDigits <= 1 ? 1 : minIntDigits;
var exp = -(this.precision() - 1) + (minIntDigits - 1);
// ensure exponent is not negative zero
var coeff = Decimal.fromRaw(this.sign, exp === 0 ? 0 : exp, this.data);
var coeff = Decimal.fromRaw(this.sign, exp === 0 ? 0 : exp, this.data, this.flag);
return [

@@ -367,2 +437,5 @@ coeff,

Decimal.prototype.precision = function () {
if (this.flag) {
return 0;
}
if (this.sign === 0) {

@@ -378,3 +451,3 @@ return 1;

Decimal.prototype.scale = function () {
return this.exp === 0 ? 0 : -this.exp;
return this.flag ? 0 : this.exp === 0 ? 0 : -this.exp;
};

@@ -385,3 +458,3 @@ /**

Decimal.prototype.integerDigits = function () {
return Math.max(this.precision() + this.exp, 1);
return this.flag ? 0 : Math.max(this.precision() + this.exp, 1);
};

@@ -393,2 +466,5 @@ /**

if (roundingMode === void 0) { roundingMode = 'half-even'; }
if (this.flag) {
return this;
}
var r = new Decimal(this);

@@ -404,3 +480,3 @@ r._setScale(floor(scale), roundingMode);

Decimal.prototype.alignexp = function () {
return (this.exp + this.precision()) - 1;
return this.flag ? 0 : (this.exp + this.precision()) - 1;
};

@@ -412,2 +488,5 @@ /**

Decimal.prototype.movePoint = function (n) {
if (this.flag) {
return this;
}
var w = new Decimal(this);

@@ -421,2 +500,5 @@ w.exp += floor(n);

Decimal.prototype.shiftleft = function (shift) {
if (this.flag) {
return this;
}
var w = new Decimal(this);

@@ -432,2 +514,5 @@ w._shiftleft(floor(shift));

if (mode === void 0) { mode = 'half-even'; }
if (this.flag) {
return this;
}
var w = new Decimal(this);

@@ -441,2 +526,5 @@ w._shiftright(floor(shift), mode);

Decimal.prototype.increment = function () {
if (this.flag) {
return this;
}
var r = new Decimal(this);

@@ -453,3 +541,3 @@ if (r.sign === -1 || r.exp !== 0) {

Decimal.prototype.decrement = function () {
return this.subtract(DecimalConstants.ONE);
return this.flag ? this : this.subtract(DecimalConstants.ONE);
};

@@ -460,3 +548,3 @@ /**

Decimal.prototype.toString = function () {
return this.formatString(this, 1);
return this.flag ? this.formatFlags() : this.formatString(this, 1);
};

@@ -468,2 +556,5 @@ /**

if (minIntegers === void 0) { minIntegers = 1; }
if (this.flag) {
return this.formatFlags();
}
var _a = this.scientific(minIntegers), coeff = _a[0], exp = _a[1];

@@ -478,3 +569,3 @@ var r = this.formatString(coeff, minIntegers);

Decimal.prototype.toParts = function () {
return this.formatParts(this, 1);
return this.flag ? this.formatFlagsParts() : this.formatParts(this, 1);
};

@@ -486,2 +577,5 @@ /**

if (minIntegers === void 0) { minIntegers = 1; }
if (this.flag) {
return this.formatFlagsParts();
}
var _a = this.scientific(minIntegers), coeff = _a[0], exp = _a[1];

@@ -613,2 +707,21 @@ var r = this.formatParts(coeff, minIntegers);

};
Decimal.prototype.formatFlags = function () {
switch (this.flag) {
case 1 /* NAN */:
return 'NaN';
case 2 /* INFINITY */:
default:
return this.sign === 1 ? 'Infinity' : '-Infinity';
}
};
Decimal.prototype.formatFlagsParts = function () {
switch (this.flag) {
case 1 /* NAN */:
return [{ type: 'nan', value: 'NaN' }];
case 2 /* INFINITY */:
default:
var s = this.sign === 1 ? 'Infinity' : '-Infinity';
return [{ type: 'infinity', value: s }];
}
};
Decimal.prototype.formatString = function (d, minInt) {

@@ -626,5 +739,89 @@ var f = new StringDecimalFormatter();

};
Decimal.fromRaw = function (sign, exp, data) {
return new this({ sign: sign, exp: exp, data: data });
/**
* Handle setting of flags for operations per the IEEE-754-2008 specification.
* These rules are also referenced in the EcmaScript specification:
*
* 12.7.3.1 - Applying the mul operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mul-operator
*
* 12.7.3.2 - Applying the div operator:
* https://tc39.github.io/ecma262/#sec-applying-the-div-operator
*
* 12.7.3.3 - Applying the mod operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mod-operator
*
* 12.8.5 - Applying the additive operators to numbers:
* https://tc39.github.io/ecma262/#sec-applying-the-additive-operators-to-numbers
*
*/
Decimal.prototype.handleFlags = function (op, v) {
var u = this;
var uflag = u.flag;
var vflag = v.flag;
// Any operation involving a NAN returns a NAN
if (uflag === 1 /* NAN */ || vflag === 1 /* NAN */) {
return NAN;
}
var uinf = u.flag === 2 /* INFINITY */;
var vinf = v.flag === 2 /* INFINITY */;
var uzero = !uflag && !u.sign;
var vzero = !vflag && !v.sign;
switch (op) {
case 0 /* ADDITION */:
if (uinf && vinf) {
return u.sign === v.sign ? (u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY) : NAN;
}
else if (uinf || vinf) {
return uinf ? u : v;
}
break;
case 1 /* SUBTRACTION */:
if (uinf && vinf) {
return u.sign === v.sign ? NAN : u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
else if (uinf || vinf) {
return uinf ? (u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY)
: v.sign === 1 ? NEGATIVE_INFINITY : POSITIVE_INFINITY;
}
break;
case 2 /* MULTIPLICATION */:
if (uinf) {
return vzero ? NAN : u.sign === v.sign ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
if (vinf) {
return uzero ? NAN : u.sign === v.sign ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
break;
case 3 /* DIVISION */:
if (uinf && vinf) {
return NAN;
}
if (uinf) {
return vzero ? (u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY) :
u.sign === v.sign ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
if (vinf) {
return ZERO;
}
if (vzero) {
return uzero ? NAN : u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
break;
case 4 /* MOD */:
if (uinf || vzero) {
return NAN;
}
if (!uinf && vinf) {
return u;
}
if (uzero && (!vzero && !vinf)) {
return u;
}
break;
}
return undefined;
};
Decimal.fromRaw = function (sign, exp, data, flag) {
return new this({ sign: sign, exp: exp, data: data, flag: flag });
};
/**

@@ -763,11 +960,2 @@ * Mutating in-place shift left.

/**
* Check for u/0 or 0/v cases.
*/
Decimal.prototype.checkDivision = function (v) {
if (v.sign === 0) {
throw new Error('Divide by zero');
}
return this.sign === 0;
};
/**
* Trim leading zeros from a result and reset sign and exponent accordingly.

@@ -847,5 +1035,11 @@ */

var _a, _b, _c;
if (u.flag) {
return u;
}
if (v.flag) {
return v;
}
var zero = u.sign === 0;
if (zero || v.sign === 0) {
return zero ? Decimal.fromRaw(vsign, v.exp, v.data) : new Decimal(u);
return zero ? Decimal.fromRaw(vsign, v.exp, v.data, v.flag) : new Decimal(u);
}

@@ -894,2 +1088,13 @@ var m = u; // m = bigger

Decimal.prototype.parse = function (arg) {
if (typeof arg === 'number') {
if (isNaN(arg)) {
this.flag = 1 /* NAN */;
return;
}
if (!isFinite(arg)) {
this.flag = 2 /* INFINITY */;
this.sign = arg === Infinity ? 1 : -1;
return;
}
}
var str = typeof arg === 'string' ? arg : arg.toString();

@@ -904,5 +1109,23 @@ var msg = this._parse(str);

*
* Expects strings of the form: "[-+][digits][.][digits][eE][-+][digits]"
* Expects strings of the form:
* "[-+][digits][.][digits][eE][-+][digits]"
* or:
* "[nN]a[nN]" for a NaN
* "[-+]?[iI]nfinity" for positive or negative infinity
*/
Decimal.prototype._parse = function (str) {
if (NAN_VALUES.has(str)) {
this.flag = 1 /* NAN */;
return;
}
if (POS_INFINITY.has(str)) {
this.flag = 2 /* INFINITY */;
this.sign = 1;
return;
}
if (NEG_INFINITY.has(str)) {
this.flag = 2 /* INFINITY */;
this.sign = -1;
return;
}
var len = str.length;

@@ -1016,2 +1239,5 @@ // Local variables to accumulate digits, sign and exponent

'57496696762772407663035354759457138217852516642742746');
var NAN = new Decimal(NaN);
var NEGATIVE_INFINITY = new Decimal(-Infinity);
var POSITIVE_INFINITY = new Decimal(Infinity);
export var DecimalConstants = {

@@ -1022,4 +1248,7 @@ ZERO: ZERO,

PI: PI,
E: E
E: E,
NAN: NAN,
POSITIVE_INFINITY: POSITIVE_INFINITY,
NEGATIVE_INFINITY: NEGATIVE_INFINITY
};
//# sourceMappingURL=decimal.js.map

@@ -0,1 +1,6 @@

export declare const enum DecimalFlag {
NONE = 0,
NAN = 1,
INFINITY = 2
}
export declare type RoundingModeType = 'up' | 'down' | 'ceiling' | 'floor' | 'half-up' | 'half-down' | 'half-even';

@@ -2,0 +7,0 @@ /**

import { NumberOperands } from './operands';
import { DecimalFormatter, Part } from './format';
import { MathContext, RoundingModeType } from './types';
import { DecimalFlag, MathContext, RoundingModeType } from './types';
declare const enum Op {
ADDITION = 0,
SUBTRACTION = 1,
MULTIPLICATION = 2,
DIVISION = 3,
MOD = 4
}
export declare const DECIMAL_DIGITS: string[];

@@ -16,3 +23,7 @@ export declare type DecimalArg = number | string | Decimal;

protected exp: number;
protected flag: DecimalFlag;
constructor(num: DecimalArg);
isNaN(): boolean;
isFinite(): boolean;
isInfinity(): boolean;
/**

@@ -26,2 +37,4 @@ * Compare decimal u to v, returning the following:

* If the abs flag is true compare the absolute values.
*
* Any NAN argument will always return -1.
*/

@@ -31,2 +44,4 @@ compare(v: DecimalArg, abs?: boolean): number;

* Compute operands for this number, used for determining the plural category.
*
* A NAN or INFINITY will return the same operands as ZERO.
*/

@@ -160,6 +175,26 @@ operands(): NumberOperands;

format<R>(formatter: DecimalFormatter<R>, decimal: string, group: string, minInt: number, minGroup: number, priGroup: number, secGroup: number, zeroScale: boolean, digits?: string[]): void;
protected formatFlags(): string;
protected formatFlagsParts(): Part[];
protected formatString(d: Decimal, minInt: number): string;
protected formatParts(d: Decimal, minInt: number): Part[];
protected static fromRaw(sign: number, exp: number, data: number[]): Decimal;
/**
* Handle setting of flags for operations per the IEEE-754-2008 specification.
* These rules are also referenced in the EcmaScript specification:
*
* 12.7.3.1 - Applying the mul operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mul-operator
*
* 12.7.3.2 - Applying the div operator:
* https://tc39.github.io/ecma262/#sec-applying-the-div-operator
*
* 12.7.3.3 - Applying the mod operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mod-operator
*
* 12.8.5 - Applying the additive operators to numbers:
* https://tc39.github.io/ecma262/#sec-applying-the-additive-operators-to-numbers
*
*/
protected handleFlags(op: Op, v: Decimal): Decimal | undefined;
protected static fromRaw(sign: number, exp: number, data: number[], flag: DecimalFlag): Decimal;
/**
* Mutating in-place shift left.

@@ -175,6 +210,2 @@ */

/**
* Check for u/0 or 0/v cases.
*/
protected checkDivision(v: Decimal): boolean;
/**
* Trim leading zeros from a result and reset sign and exponent accordingly.

@@ -206,3 +237,7 @@ */

*
* Expects strings of the form: "[-+][digits][.][digits][eE][-+][digits]"
* Expects strings of the form:
* "[-+][digits][.][digits][eE][-+][digits]"
* or:
* "[nN]a[nN]" for a NaN
* "[-+]?[iI]nfinity" for positive or negative infinity
*/

@@ -217,2 +252,6 @@ protected _parse(str: string): string | undefined;

E: Decimal;
NAN: Decimal;
POSITIVE_INFINITY: Decimal;
NEGATIVE_INFINITY: Decimal;
};
export {};

@@ -13,2 +13,6 @@ "use strict";

var DEFAULT_PRECISION = 28;
var EMPTY = [];
var NAN_VALUES = new Set(['nan', 'NaN']);
var POS_INFINITY = new Set(['infinity', '+infinity', 'Infinity', '+Infinity']);
var NEG_INFINITY = new Set(['-infinity', '-Infinity']);
exports.DECIMAL_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

@@ -53,2 +57,6 @@ exports.coerceDecimal = function (n) {

function Decimal(num) {
this.data = EMPTY;
this.sign = 0;
this.exp = 0;
this.flag = 0 /* NONE */;
if (typeof num === 'string' || typeof num === 'number') {

@@ -61,4 +69,14 @@ this.parse(num);

this.exp = num.exp;
this.flag = num.flag;
}
}
Decimal.prototype.isNaN = function () {
return this.flag === 1 /* NAN */;
};
Decimal.prototype.isFinite = function () {
return this.flag === 0;
};
Decimal.prototype.isInfinity = function () {
return this.flag === 2 /* INFINITY */;
};
/**

@@ -72,2 +90,4 @@ * Compare decimal u to v, returning the following:

* If the abs flag is true compare the absolute values.
*
* Any NAN argument will always return -1.
*/

@@ -78,2 +98,27 @@ Decimal.prototype.compare = function (v, abs) {

v = exports.coerceDecimal(v);
if (u.flag || v.flag) {
// NAN is never equal to itself or any other value
if (u.flag === 1 /* NAN */ || v.flag === 1 /* NAN */) {
return -1;
}
// INFINITY
// Infinities can be equal if their sign matches
if (u.flag === v.flag) {
return u.sign === v.sign ? 0 : u.sign === -1 ? -1 : 1;
}
// Negative infinity before all other values
// Positive infinity after all other values
return u.flag === 2 /* INFINITY */ ?
u.sign === -1 ? -1 : 1 :
v.sign === -1 ? -1 : 1;
}
// TODO: improve representation of zero and sign, since with
// current representation there are many edge cases. Below
// code fixes issues of comparisons with zero.
if (u.sign === 0) {
return v.sign === 0 ? 0 : v.sign === 1 ? -1 : 1;
}
else if (v.sign === 0) {
return u.sign;
}
var us = u.sign;

@@ -115,2 +160,4 @@ var vs = v.sign;

* Compute operands for this number, used for determining the plural category.
*
* A NAN or INFINITY will return the same operands as ZERO.
*/

@@ -124,3 +171,3 @@ Decimal.prototype.operands = function () {

Decimal.prototype.abs = function () {
return this.sign === -1 ? Decimal.fromRaw(-this.sign, this.exp, this.data) : this;
return this.sign === -1 ? Decimal.fromRaw(-this.sign, this.exp, this.data, this.flag) : this;
};

@@ -131,3 +178,3 @@ /**

Decimal.prototype.negate = function () {
return this.sign === 0 ? this : Decimal.fromRaw(-this.sign, this.exp, this.data);
return this.sign === 0 ? this : Decimal.fromRaw(-this.sign, this.exp, this.data, this.flag);
};

@@ -151,2 +198,5 @@ /**

Decimal.prototype.isInteger = function () {
if (this.flag) {
return false;
}
return this.sign === 0 ? true : this.exp + this.trailingZeros() >= 0;

@@ -158,3 +208,3 @@ };

Decimal.prototype.toInteger = function () {
return this.setScale(0, 'down');
return this.flag ? this : this.setScale(0, 'down');
};

@@ -166,3 +216,4 @@ /**

v = exports.coerceDecimal(v);
return this.addsub(this, v, v.sign);
var r = this.handleFlags(0 /* ADDITION */, v);
return r === undefined ? this.addsub(this, v, v.sign) : r;
};

@@ -174,3 +225,4 @@ /**

v = exports.coerceDecimal(v);
return this.addsub(this, v, v.sign === 1 ? -1 : 1);
var r = this.handleFlags(1 /* SUBTRACTION */, v);
return r === undefined ? this.addsub(this, v, v.sign === 1 ? -1 : 1) : r;
};

@@ -182,4 +234,8 @@ /**

var _a = parseMathContext('half-even', context), usePrecision = _a[0], scaleprec = _a[1], rounding = _a[2];
v = exports.coerceDecimal(v);
var r = this.handleFlags(2 /* MULTIPLICATION */, v);
if (r !== undefined) {
return r;
}
var u = this;
v = exports.coerceDecimal(v);
var w = new Decimal(ZERO);

@@ -212,7 +268,8 @@ w.exp = (u.exp + v.exp);

Decimal.prototype.divide = function (v, context) {
var _a = parseMathContext('half-even', context), usePrecision = _a[0], scaleprec = _a[1], rounding = _a[2];
v = exports.coerceDecimal(v);
if (this.checkDivision(v)) {
return usePrecision ? ZERO : ZERO.setScale(scaleprec);
var r = this.handleFlags(3 /* DIVISION */, v);
if (r !== undefined) {
return r;
}
var _a = parseMathContext('half-even', context), usePrecision = _a[0], scaleprec = _a[1], rounding = _a[2];
var u = this;

@@ -277,4 +334,6 @@ if (!usePrecision) {

v = exports.coerceDecimal(v);
if (this.checkDivision(v)) {
return [ZERO, ZERO];
var rq = this.handleFlags(3 /* DIVISION */, v);
if (rq !== undefined) {
var rm = this.handleFlags(4 /* MOD */, v);
return [rq, rm];
}

@@ -316,3 +375,5 @@ var u = this;

Decimal.prototype.mod = function (v) {
return this.divmod(v)[1];
v = exports.coerceDecimal(v);
var r = this.handleFlags(4 /* MOD */, v);
return r === undefined ? this.divmod(v)[1] : r;
};

@@ -323,2 +384,5 @@ /**

Decimal.prototype.trailingZeros = function () {
if (this.flag) {
return 0;
}
var d = this.data;

@@ -344,2 +408,5 @@ var len = d.length;

Decimal.prototype.stripTrailingZeros = function () {
if (this.flag) {
return this;
}
var r = new Decimal(this);

@@ -355,6 +422,9 @@ r._stripTrailingZeros();

if (minIntDigits === void 0) { minIntDigits = 1; }
if (this.flag) {
return [this, 0];
}
minIntDigits = minIntDigits <= 1 ? 1 : minIntDigits;
var exp = -(this.precision() - 1) + (minIntDigits - 1);
// ensure exponent is not negative zero
var coeff = Decimal.fromRaw(this.sign, exp === 0 ? 0 : exp, this.data);
var coeff = Decimal.fromRaw(this.sign, exp === 0 ? 0 : exp, this.data, this.flag);
return [

@@ -369,2 +439,5 @@ coeff,

Decimal.prototype.precision = function () {
if (this.flag) {
return 0;
}
if (this.sign === 0) {

@@ -380,3 +453,3 @@ return 1;

Decimal.prototype.scale = function () {
return this.exp === 0 ? 0 : -this.exp;
return this.flag ? 0 : this.exp === 0 ? 0 : -this.exp;
};

@@ -387,3 +460,3 @@ /**

Decimal.prototype.integerDigits = function () {
return Math.max(this.precision() + this.exp, 1);
return this.flag ? 0 : Math.max(this.precision() + this.exp, 1);
};

@@ -395,2 +468,5 @@ /**

if (roundingMode === void 0) { roundingMode = 'half-even'; }
if (this.flag) {
return this;
}
var r = new Decimal(this);

@@ -406,3 +482,3 @@ r._setScale(floor(scale), roundingMode);

Decimal.prototype.alignexp = function () {
return (this.exp + this.precision()) - 1;
return this.flag ? 0 : (this.exp + this.precision()) - 1;
};

@@ -414,2 +490,5 @@ /**

Decimal.prototype.movePoint = function (n) {
if (this.flag) {
return this;
}
var w = new Decimal(this);

@@ -423,2 +502,5 @@ w.exp += floor(n);

Decimal.prototype.shiftleft = function (shift) {
if (this.flag) {
return this;
}
var w = new Decimal(this);

@@ -434,2 +516,5 @@ w._shiftleft(floor(shift));

if (mode === void 0) { mode = 'half-even'; }
if (this.flag) {
return this;
}
var w = new Decimal(this);

@@ -443,2 +528,5 @@ w._shiftright(floor(shift), mode);

Decimal.prototype.increment = function () {
if (this.flag) {
return this;
}
var r = new Decimal(this);

@@ -455,3 +543,3 @@ if (r.sign === -1 || r.exp !== 0) {

Decimal.prototype.decrement = function () {
return this.subtract(exports.DecimalConstants.ONE);
return this.flag ? this : this.subtract(exports.DecimalConstants.ONE);
};

@@ -462,3 +550,3 @@ /**

Decimal.prototype.toString = function () {
return this.formatString(this, 1);
return this.flag ? this.formatFlags() : this.formatString(this, 1);
};

@@ -470,2 +558,5 @@ /**

if (minIntegers === void 0) { minIntegers = 1; }
if (this.flag) {
return this.formatFlags();
}
var _a = this.scientific(minIntegers), coeff = _a[0], exp = _a[1];

@@ -480,3 +571,3 @@ var r = this.formatString(coeff, minIntegers);

Decimal.prototype.toParts = function () {
return this.formatParts(this, 1);
return this.flag ? this.formatFlagsParts() : this.formatParts(this, 1);
};

@@ -488,2 +579,5 @@ /**

if (minIntegers === void 0) { minIntegers = 1; }
if (this.flag) {
return this.formatFlagsParts();
}
var _a = this.scientific(minIntegers), coeff = _a[0], exp = _a[1];

@@ -615,2 +709,21 @@ var r = this.formatParts(coeff, minIntegers);

};
Decimal.prototype.formatFlags = function () {
switch (this.flag) {
case 1 /* NAN */:
return 'NaN';
case 2 /* INFINITY */:
default:
return this.sign === 1 ? 'Infinity' : '-Infinity';
}
};
Decimal.prototype.formatFlagsParts = function () {
switch (this.flag) {
case 1 /* NAN */:
return [{ type: 'nan', value: 'NaN' }];
case 2 /* INFINITY */:
default:
var s = this.sign === 1 ? 'Infinity' : '-Infinity';
return [{ type: 'infinity', value: s }];
}
};
Decimal.prototype.formatString = function (d, minInt) {

@@ -628,5 +741,89 @@ var f = new format_1.StringDecimalFormatter();

};
Decimal.fromRaw = function (sign, exp, data) {
return new this({ sign: sign, exp: exp, data: data });
/**
* Handle setting of flags for operations per the IEEE-754-2008 specification.
* These rules are also referenced in the EcmaScript specification:
*
* 12.7.3.1 - Applying the mul operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mul-operator
*
* 12.7.3.2 - Applying the div operator:
* https://tc39.github.io/ecma262/#sec-applying-the-div-operator
*
* 12.7.3.3 - Applying the mod operator:
* https://tc39.github.io/ecma262/#sec-applying-the-mod-operator
*
* 12.8.5 - Applying the additive operators to numbers:
* https://tc39.github.io/ecma262/#sec-applying-the-additive-operators-to-numbers
*
*/
Decimal.prototype.handleFlags = function (op, v) {
var u = this;
var uflag = u.flag;
var vflag = v.flag;
// Any operation involving a NAN returns a NAN
if (uflag === 1 /* NAN */ || vflag === 1 /* NAN */) {
return NAN;
}
var uinf = u.flag === 2 /* INFINITY */;
var vinf = v.flag === 2 /* INFINITY */;
var uzero = !uflag && !u.sign;
var vzero = !vflag && !v.sign;
switch (op) {
case 0 /* ADDITION */:
if (uinf && vinf) {
return u.sign === v.sign ? (u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY) : NAN;
}
else if (uinf || vinf) {
return uinf ? u : v;
}
break;
case 1 /* SUBTRACTION */:
if (uinf && vinf) {
return u.sign === v.sign ? NAN : u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
else if (uinf || vinf) {
return uinf ? (u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY)
: v.sign === 1 ? NEGATIVE_INFINITY : POSITIVE_INFINITY;
}
break;
case 2 /* MULTIPLICATION */:
if (uinf) {
return vzero ? NAN : u.sign === v.sign ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
if (vinf) {
return uzero ? NAN : u.sign === v.sign ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
break;
case 3 /* DIVISION */:
if (uinf && vinf) {
return NAN;
}
if (uinf) {
return vzero ? (u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY) :
u.sign === v.sign ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
if (vinf) {
return ZERO;
}
if (vzero) {
return uzero ? NAN : u.sign === 1 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
}
break;
case 4 /* MOD */:
if (uinf || vzero) {
return NAN;
}
if (!uinf && vinf) {
return u;
}
if (uzero && (!vzero && !vinf)) {
return u;
}
break;
}
return undefined;
};
Decimal.fromRaw = function (sign, exp, data, flag) {
return new this({ sign: sign, exp: exp, data: data, flag: flag });
};
/**

@@ -765,11 +962,2 @@ * Mutating in-place shift left.

/**
* Check for u/0 or 0/v cases.
*/
Decimal.prototype.checkDivision = function (v) {
if (v.sign === 0) {
throw new Error('Divide by zero');
}
return this.sign === 0;
};
/**
* Trim leading zeros from a result and reset sign and exponent accordingly.

@@ -849,5 +1037,11 @@ */

var _a, _b, _c;
if (u.flag) {
return u;
}
if (v.flag) {
return v;
}
var zero = u.sign === 0;
if (zero || v.sign === 0) {
return zero ? Decimal.fromRaw(vsign, v.exp, v.data) : new Decimal(u);
return zero ? Decimal.fromRaw(vsign, v.exp, v.data, v.flag) : new Decimal(u);
}

@@ -896,2 +1090,13 @@ var m = u; // m = bigger

Decimal.prototype.parse = function (arg) {
if (typeof arg === 'number') {
if (isNaN(arg)) {
this.flag = 1 /* NAN */;
return;
}
if (!isFinite(arg)) {
this.flag = 2 /* INFINITY */;
this.sign = arg === Infinity ? 1 : -1;
return;
}
}
var str = typeof arg === 'string' ? arg : arg.toString();

@@ -906,5 +1111,23 @@ var msg = this._parse(str);

*
* Expects strings of the form: "[-+][digits][.][digits][eE][-+][digits]"
* Expects strings of the form:
* "[-+][digits][.][digits][eE][-+][digits]"
* or:
* "[nN]a[nN]" for a NaN
* "[-+]?[iI]nfinity" for positive or negative infinity
*/
Decimal.prototype._parse = function (str) {
if (NAN_VALUES.has(str)) {
this.flag = 1 /* NAN */;
return;
}
if (POS_INFINITY.has(str)) {
this.flag = 2 /* INFINITY */;
this.sign = 1;
return;
}
if (NEG_INFINITY.has(str)) {
this.flag = 2 /* INFINITY */;
this.sign = -1;
return;
}
var len = str.length;

@@ -1018,2 +1241,5 @@ // Local variables to accumulate digits, sign and exponent

'57496696762772407663035354759457138217852516642742746');
var NAN = new Decimal(NaN);
var NEGATIVE_INFINITY = new Decimal(-Infinity);
var POSITIVE_INFINITY = new Decimal(Infinity);
exports.DecimalConstants = {

@@ -1024,4 +1250,7 @@ ZERO: ZERO,

PI: PI,
E: E
E: E,
NAN: NAN,
POSITIVE_INFINITY: POSITIVE_INFINITY,
NEGATIVE_INFINITY: NEGATIVE_INFINITY
};
//# sourceMappingURL=decimal.js.map

@@ -0,1 +1,6 @@

export declare const enum DecimalFlag {
NONE = 0,
NAN = 1,
INFINITY = 2
}
export declare type RoundingModeType = 'up' | 'down' | 'ceiling' | 'floor' | 'half-up' | 'half-down' | 'half-even';

@@ -2,0 +7,0 @@ /**

4

package.json
{
"name": "@phensley/decimal",
"version": "0.14.4",
"version": "0.14.5",
"description": "Arbitrary precision decimal math",

@@ -65,3 +65,3 @@ "main": "lib/index.js",

},
"gitHead": "3c572a15f763f6ae005248708794d5c162cb9cc9"
"gitHead": "00535f189a1f9c3e0368825766e4076389f78419"
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc