pull/186/head v10.3.0
Michael Mclaughlin 3 years ago
parent c29c80c6e3
commit 220f11c498

@ -1,6 +1,7 @@
language: node_js language: node_js
node_js: node_js:
- "node" - "node"
- "15"
- "14" - "14"
- "13" - "13"
- "12" - "12"

@ -1,3 +1,17 @@
#### 10.3.0
* 22/06/2021
* Support underscores as separators.
* #101 Add `Decimal.clamp` method.
* #161 Fix Decimal instances deemed plain objects.
* #100 Add `Decimal.sum` method.
* #146 `Symbol.for` to `Symbol['for']` for IE8.
* #132 Fix possible infinite loop when `minE` is very low.
* #180 Accept Decimals of different origin.
* Update Typescript definitions.
* Update minification examples in *README*.
* Add minified versions for both *decimal.js* and *decimal.mjs*.
* Add *files* field to *package.json*, and remove build script.
#### 10.2.1 #### 10.2.1
* 28/09/2020 * 28/09/2020
* Correct `sqrt` initial estimate. * Correct `sqrt` initial estimate.

@ -1,6 +1,6 @@
The MIT Licence. The MIT Licence.
Copyright (c) 2020 Michael Mclaughlin Copyright (c) 2021 Michael Mclaughlin
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

@ -32,7 +32,8 @@ This library also adds the trigonometric functions, among others, and supports n
which makes it a significantly larger library than *bignumber.js* and the even smaller which makes it a significantly larger library than *bignumber.js* and the even smaller
[big.js](https://github.com/MikeMcl/big.js/). [big.js](https://github.com/MikeMcl/big.js/).
For a lighter version of this library without the trigonometric functions see [decimal.js-light](https://github.com/MikeMcl/decimal.js-light/). For a lighter version of this library without the trigonometric functions see
[decimal.js-light](https://github.com/MikeMcl/decimal.js-light/).
## Load ## Load
@ -43,7 +44,7 @@ Browser:
```html ```html
<script src='path/to/decimal.js'></script> <script src='path/to/decimal.js'></script>
``` ```
or
```html ```html
<script type="module"> <script type="module">
import Decimal from './path/to/decimal.mjs'; import Decimal from './path/to/decimal.mjs';
@ -54,26 +55,18 @@ import Decimal from './path/to/decimal.mjs';
[Node.js](https://nodejs.org): [Node.js](https://nodejs.org):
```bash ```bash
$ npm install decimal.js npm install decimal.js
``` ```
```js ```js
var Decimal = require('decimal.js'); var Decimal = require('decimal.js');
``` ```
or
ES module:
```js ```js
//import Decimal from 'decimal.js'; import Decimal from 'decimal.js';
import {Decimal} from 'decimal.js';
``` ```
or
AMD loader libraries such as [requireJS](https://requirejs.org/):
```js ```js
require(['decimal'], function(Decimal) { import {Decimal} from 'decimal.js';
// Use Decimal here in local scope. No global Decimal.
});
``` ```
## Use ## Use
@ -165,11 +158,11 @@ pi.toFraction() // [ '7853982301', '2500000000' ]
pi.toFraction(1000) // [ '355', '113' ] pi.toFraction(1000) // [ '355', '113' ]
``` ```
All calculations are rounded according to the number of significant digits and rounding mode All calculations are rounded according to the number of significant digits and rounding mode specified
specified by the `precision` and `rounding` properties of the Decimal constructor. by the `precision` and `rounding` properties of the Decimal constructor.
For advanced usage, multiple Decimal constructors can be created, each with their own independent configuration which For advanced usage, multiple Decimal constructors can be created, each with their own independent
applies to all Decimal numbers created from it. configuration which applies to all Decimal numbers created from it.
```js ```js
// Set the precision and rounding of the default Decimal constructor // Set the precision and rounding of the default Decimal constructor
@ -206,36 +199,42 @@ and the file *test.html* which runs all the tests when opened in a browser.
To run all the tests, from a command-line at the root directory using npm To run all the tests, from a command-line at the root directory using npm
```bash ```bash
$ npm test npm test
``` ```
or at the *test* directory using Node or at the *test* directory using Node
```bash ```bash
$ node test node test
``` ```
Each separate test module can also be executed individually, for example, at the *test/modules* directory Each separate test module can also be executed individually, for example, at the *test/modules* directory
```bash ```bash
$ node toFraction node toFraction
``` ```
## Build ## Minify
For Node, if [uglify-js](https://github.com/mishoo/UglifyJS2) is installed The minified version of *decimal.js* and its associated source map found in this repository was created with
[uglify-js](https://github.com/mishoo/UglifyJS) using
```bash ```bash
npm install uglify-js -g npm install uglify-js -g
uglifyjs decimal.js --source-map url=decimal.min.js.map --compress --mangle --output decimal.min.js
``` ```
then The minified version of *decimal.mjs* and its associated source map found in this repository was created with
[terser](https://github.com/terser/terser) using
```bash ```bash
npm run build npm install terser -g
terser decimal.mjs --source-map url=decimal.min.mjs.map -c -m --toplevel -o decimal.min.mjs
``` ```
will create *decimal.min.js* and a source map. ```js
import Decimal from './decimal.min.mjs';
```
## Licence ## Licence

2
decimal.d.ts vendored

@ -56,7 +56,7 @@ export declare class Decimal {
readonly d: number[]; readonly d: number[];
readonly e: number; readonly e: number;
readonly s: number; readonly s: number;
private readonly name: string; private readonly toStringTag: string;
constructor(n: Decimal.Value); constructor(n: Decimal.Value);

@ -77,7 +77,7 @@ export declare class Decimal {
readonly d: number[]; readonly d: number[];
readonly e: number; readonly e: number;
readonly s: number; readonly s: number;
private readonly name: string; private readonly toStringTag: string;
constructor(n: DecimalValue); constructor(n: DecimalValue);

@ -3,10 +3,10 @@
/* /*
* decimal.js v10.2.1 * decimal.js v10.3.0
* An arbitrary-precision Decimal type for JavaScript. * An arbitrary-precision Decimal type for JavaScript.
* https://github.com/MikeMcl/decimal.js * https://github.com/MikeMcl/decimal.js
* Copyright (c) 2020 Michael Mclaughlin <M8ch88l@gmail.com> * Copyright (c) 2021 Michael Mclaughlin <M8ch88l@gmail.com>
* MIT Licence * MIT Licence
*/ */
@ -105,6 +105,7 @@
invalidArgument = decimalError + 'Invalid argument: ', invalidArgument = decimalError + 'Invalid argument: ',
precisionLimitExceeded = decimalError + 'Precision limit exceeded', precisionLimitExceeded = decimalError + 'Precision limit exceeded',
cryptoUnavailable = decimalError + 'crypto unavailable', cryptoUnavailable = decimalError + 'crypto unavailable',
tag = '[object Decimal]',
mathfloor = Math.floor, mathfloor = Math.floor,
mathpow = Math.pow, mathpow = Math.pow,
@ -122,7 +123,7 @@
PI_PRECISION = PI.length - 1, PI_PRECISION = PI.length - 1,
// Decimal.prototype object // Decimal.prototype object
P = { name: '[object Decimal]' }; P = { toStringTag: tag };
// Decimal prototype methods // Decimal prototype methods
@ -227,7 +228,8 @@
Ctor = x.constructor; Ctor = x.constructor;
min = new Ctor(min); min = new Ctor(min);
max = new Ctor(max); max = new Ctor(max);
if (!min.s || !max.s || min.gt(max)) return new Ctor(NaN); if (!min.s || !max.s) return new Ctor(NaN);
if (min.gt(max)) throw Error(invalidArgument + max);
k = x.cmp(min); k = x.cmp(min);
return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x); return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
}; };
@ -3596,7 +3598,7 @@
*/ */
function parseOther(x, str) { function parseOther(x, str) {
var base, Ctor, divisor, i, isFloat, len, p, xd, xe; var base, Ctor, divisor, i, isFloat, len, p, xd, xe;
if (str.indexOf('_') > -1) { if (str.indexOf('_') > -1) {
str = str.replace(/(\d)_(?=\d)/g, '$1'); str = str.replace(/(\d)_(?=\d)/g, '$1');
if (isDecimal.test(str)) return parseDecimal(x, str); if (isDecimal.test(str)) return parseDecimal(x, str);
@ -4530,7 +4532,7 @@
* *
*/ */
function isDecimalInstance(obj) { function isDecimalInstance(obj) {
return obj instanceof Decimal || obj && obj.name === '[object Decimal]' || false; return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
} }

2
decimal.min.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1,8 +1,8 @@
/* /*
* decimal.js v10.2.1 * decimal.js v10.3.0
* An arbitrary-precision Decimal type for JavaScript. * An arbitrary-precision Decimal type for JavaScript.
* https://github.com/MikeMcl/decimal.js * https://github.com/MikeMcl/decimal.js
* Copyright (c) 2020 Michael Mclaughlin <M8ch88l@gmail.com> * Copyright (c) 2021 Michael Mclaughlin <M8ch88l@gmail.com>
* MIT Licence * MIT Licence
*/ */
@ -101,6 +101,7 @@ var EXP_LIMIT = 9e15, // 0 to 9e15
invalidArgument = decimalError + 'Invalid argument: ', invalidArgument = decimalError + 'Invalid argument: ',
precisionLimitExceeded = decimalError + 'Precision limit exceeded', precisionLimitExceeded = decimalError + 'Precision limit exceeded',
cryptoUnavailable = decimalError + 'crypto unavailable', cryptoUnavailable = decimalError + 'crypto unavailable',
tag = '[object Decimal]',
mathfloor = Math.floor, mathfloor = Math.floor,
mathpow = Math.pow, mathpow = Math.pow,
@ -118,7 +119,7 @@ var EXP_LIMIT = 9e15, // 0 to 9e15
PI_PRECISION = PI.length - 1, PI_PRECISION = PI.length - 1,
// Decimal.prototype object // Decimal.prototype object
P = { name: '[object Decimal]' }; P = { toStringTag: tag };
// Decimal prototype methods // Decimal prototype methods
@ -127,7 +128,7 @@ var EXP_LIMIT = 9e15, // 0 to 9e15
/* /*
* absoluteValue abs * absoluteValue abs
* ceil * ceil
* clampedTo clamp * clampedTo clamp
* comparedTo cmp * comparedTo cmp
* cosine cos * cosine cos
* cubeRoot cbrt * cubeRoot cbrt
@ -223,7 +224,8 @@ P.clampedTo = P.clamp = function (min, max) {
Ctor = x.constructor; Ctor = x.constructor;
min = new Ctor(min); min = new Ctor(min);
max = new Ctor(max); max = new Ctor(max);
if (!min.s || !max.s || min.gt(max)) return new Ctor(NaN); if (!min.s || !max.s) return new Ctor(NaN);
if (min.gt(max)) throw Error(invalidArgument + max);
k = x.cmp(min); k = x.cmp(min);
return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x); return k < 0 ? min : x.cmp(max) > 0 ? max : new Ctor(x);
}; };
@ -2634,7 +2636,7 @@ function convertBase(str, baseIn, baseOut) {
*/ */
function cosine(Ctor, x) { function cosine(Ctor, x) {
var k, len, y; var k, len, y;
if (x.isZero()) return x; if (x.isZero()) return x;
// Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1 // Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
@ -3676,7 +3678,7 @@ function sine(Ctor, x) {
if (len < 3) { if (len < 3) {
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x); return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
} }
// Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x) // Argument reduction: sin(5x) = 16*sin^5(x) - 20*sin^3(x) + 5*sin(x)
// i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5) // i.e. sin(x) = 16*sin^5(x/5) - 20*sin^3(x/5) + 5*sin(x/5)
@ -3942,7 +3944,7 @@ function truncate(arr, len) {
* atan2 * atan2
* cbrt * cbrt
* ceil * ceil
* clamp * clamp
* clone * clone
* config * config
* cos * cos
@ -3968,7 +3970,7 @@ function truncate(arr, len) {
* sinh * sinh
* sqrt * sqrt
* sub * sub
* sum * sum
* tan * tan
* tanh * tanh
* trunc * trunc
@ -4408,7 +4410,7 @@ function clone(obj) {
Decimal.atan2 = atan2; Decimal.atan2 = atan2;
Decimal.cbrt = cbrt; // ES6 Decimal.cbrt = cbrt; // ES6
Decimal.ceil = ceil; Decimal.ceil = ceil;
Decimal.clamp = clamp; Decimal.clamp = clamp;
Decimal.cos = cos; Decimal.cos = cos;
Decimal.cosh = cosh; // ES6 Decimal.cosh = cosh; // ES6
Decimal.div = div; Decimal.div = div;
@ -4431,7 +4433,7 @@ function clone(obj) {
Decimal.sinh = sinh; // ES6 Decimal.sinh = sinh; // ES6
Decimal.sqrt = sqrt; Decimal.sqrt = sqrt;
Decimal.sub = sub; Decimal.sub = sub;
Decimal.sum = sum; Decimal.sum = sum;
Decimal.tan = tan; Decimal.tan = tan;
Decimal.tanh = tanh; // ES6 Decimal.tanh = tanh; // ES6
Decimal.trunc = trunc; // ES6 Decimal.trunc = trunc; // ES6
@ -4526,7 +4528,7 @@ function hypot() {
* *
*/ */
function isDecimalInstance(obj) { function isDecimalInstance(obj) {
return obj instanceof Decimal || obj && obj.name === '[object Decimal]' || false; return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
} }
@ -4887,8 +4889,7 @@ P[Symbol.for('nodejs.util.inspect.custom')] = P.toString;
P[Symbol.toStringTag] = 'Decimal'; P[Symbol.toStringTag] = 'Decimal';
// Create and configure initial Decimal constructor. // Create and configure initial Decimal constructor.
export var Decimal = clone(DEFAULTS); export var Decimal = P.constructor = clone(DEFAULTS);
Decimal.prototype.constructor = Decimal;
// Create the internal constants from their string values. // Create the internal constants from their string values.
LN10 = new Decimal(LN10); LN10 = new Decimal(LN10);

@ -439,7 +439,7 @@ b = new Decimal(x).ceil()
a.equals(b) // true</pre> a.equals(b) // true</pre>
<h5 id="Dclamp">clamp<code class='inset'>.clamp(min, max) <i>&rArr; Decimal</i></code></h5> <h5 id="Dclamp">clamp<code class='inset'>.clamp(min, max) <i>&rArr; Decimal</i></code></h5>
<p> <p>
<code>min</code>: <i>number|string|Decimal</i><br /> <code>min</code>: <i>number|string|Decimal</i><br />
@ -943,8 +943,11 @@ Decimal.precision = 0
All functions which return a Decimal will round the return value to <code>precision</code> All functions which return a Decimal will round the return value to <code>precision</code>
significant digits except <a href='#decimal'><code>Decimal</code></a>, significant digits except <a href='#decimal'><code>Decimal</code></a>,
<a href='#abs'><code>absoluteValue</code></a>, <a href='#abs'><code>absoluteValue</code></a>,
<a href='#ceil'><code>ceil</code></a>, <a href='#floor'><code>floor</code></a>, <a href='#ceil'><code>ceil</code></a>,
<a href='#neg'><code>negated</code></a>, <a href='#round'><code>round</code></a>, <a href='#clamp'><code>clampedTo</code></a>,
<a href='#floor'><code>floor</code></a>,
<a href='#neg'><code>negated</code></a>,
<a href='#round'><code>round</code></a>,
<a href='#toDP'><code>toDecimalPlaces</code></a>, <a href='#toDP'><code>toDecimalPlaces</code></a>,
<a href='#toNearest'><code>toNearest</code></a> and <a href='#toNearest'><code>toNearest</code></a> and
<a href='#trunc'><code>truncated</code></a>. <a href='#trunc'><code>truncated</code></a>.
@ -1668,10 +1671,6 @@ x = new Decimal(1)
x.isFinite() // true x.isFinite() // true
y = new Decimal(Infinity) y = new Decimal(Infinity)
y.isFinite() // false</pre> y.isFinite() // false</pre>
<p>
Note: The native method <code>isFinite()</code> can be used if
<code>n &lt;= Number.MAX_VALUE</code>.
</p>
@ -1698,7 +1697,6 @@ x = new Decimal(NaN)
x.isNaN() // true x.isNaN() // true
y = new Decimal('Infinity') y = new Decimal('Infinity')
y.isNaN() // false</pre> y.isNaN() // false</pre>
<p>Note: The native method <code>isNaN()</code> can also be used.</p>
@ -1712,18 +1710,17 @@ x = new Decimal(-0)
x.isNegative() // true x.isNegative() // true
y = new Decimal(2) y = new Decimal(2)
y.isNeg // false</pre> y.isNeg // false</pre>
<p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p> <p>Note that zero is signed.</p>
<p>Also note that signed zeroes are implemented, following the IEEE Standard
for Floating-Point Arithmetic (IEEE 754).</p>
<pre> <pre>
Decimal(0).valueOf() // '0' new Decimal(0).valueOf() // '0'
Decimal(0).isNegative() // false new Decimal(0).isNegative() // false
Decimal(0).negated().valueOf() // '-0' new Decimal(0).negated().valueOf() // '-0'
Decimal(0).negated().isNegative() // true new Decimal(0).negated().isNegative() // true
new Decimal(-0).isNegative() // true
</pre> </pre>
<h5 id="isPos">isPositive<code class='inset'>.isPos() <i>&rArr; boolean</i></code></h5> <h5 id="isPos">isPositive<code class='inset'>.isPos() <i>&rArr; boolean</i></code></h5>
<p> <p>
Returns <code>true</code> if the value of this Decimal is positive, otherwise returns Returns <code>true</code> if the value of this Decimal is positive, otherwise returns
@ -1734,7 +1731,6 @@ x = new Decimal(0)
x.isPositive() // true x.isPositive() // true
y = new Decimal(-2) y = new Decimal(-2)
y.isPos // false</pre> y.isPos // false</pre>
<p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
@ -1748,7 +1744,6 @@ x = new Decimal(-0)
x.isZero() && x.isNeg() // true x.isZero() && x.isNeg() // true
y = new Decimal(Infinity) y = new Decimal(Infinity)
y.isZero() // false</pre> y.isZero() // false</pre>
<p>Note: <code>n == 0</code> can be used if <code>n &gt;= Number.MIN_VALUE</code>.</p>

@ -1,7 +1,7 @@
{ {
"name": "decimal.js", "name": "decimal.js",
"description": "An arbitrary-precision Decimal type for JavaScript.", "description": "An arbitrary-precision Decimal type for JavaScript.",
"version": "10.2.1", "version": "10.3.0",
"keywords": [ "keywords": [
"arbitrary", "arbitrary",
"precision", "precision",
@ -29,8 +29,12 @@
}, },
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"test": "node ./test/test.js", "test": "node ./test/test.js"
"build": "uglifyjs decimal.js --source-map url=decimal.min.js.map -c -m -o decimal.min.js"
}, },
"types": "decimal.d.ts" "types": "decimal.d.ts",
"files": [
"decimal.js",
"decimal.mjs",
"decimal.d.ts"
]
} }

@ -24,11 +24,9 @@ T('clamp', function () {
t(1, 0, 1, '1'); t(1, 0, 1, '1');
t(2, 0, 1, '1'); t(2, 0, 1, '1');
t(0, 0, -1, 'NaN');
t(1, 1, 1, '1'); t(1, 1, 1, '1');
t(-1, 1, 1, '1'); t(-1, 1, 1, '1');
t(-1, -1, 1, '-1'); t(-1, -1, 1, '-1');
t(1, 1, -1, 'NaN');
t(2, 1, 2, '2'); t(2, 1, 2, '2');
t(3, 1, 2, '2'); t(3, 1, 2, '2');
t(1, 0, 1, '1'); t(1, 0, 1, '1');
@ -39,8 +37,6 @@ T('clamp', function () {
t(-Infinity, 0, 1, '0'); t(-Infinity, 0, 1, '0');
t(-Infinity, -Infinity, Infinity, '-Infinity'); t(-Infinity, -Infinity, Infinity, '-Infinity');
t(Infinity, -Infinity, Infinity, 'Infinity'); t(Infinity, -Infinity, Infinity, 'Infinity');
t(Infinity, Infinity, -Infinity, 'NaN');
t(0, Infinity, 0, 'NaN');
t(0, 1, Infinity, '1'); t(0, 1, Infinity, '1');
t(0, NaN, 1, 'NaN'); t(0, NaN, 1, 'NaN');

Loading…
Cancel
Save