1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00
This commit is contained in:
Michael Mclaughlin 2021-06-22 19:20:14 +01:00
parent c29c80c6e3
commit 220f11c498
15 changed files with 94 additions and 79 deletions

View File

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

View File

@ -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
* 28/09/2020
* Correct `sqrt` initial estimate.

View File

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

View File

@ -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
[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
@ -43,7 +44,7 @@ Browser:
```html
<script src='path/to/decimal.js'></script>
```
or
```html
<script type="module">
import Decimal from './path/to/decimal.mjs';
@ -54,26 +55,18 @@ import Decimal from './path/to/decimal.mjs';
[Node.js](https://nodejs.org):
```bash
$ npm install decimal.js
npm install decimal.js
```
```js
var Decimal = require('decimal.js');
```
ES module:
or
```js
//import Decimal from 'decimal.js';
import {Decimal} from 'decimal.js';
import Decimal from 'decimal.js';
```
AMD loader libraries such as [requireJS](https://requirejs.org/):
or
```js
require(['decimal'], function(Decimal) {
// Use Decimal here in local scope. No global Decimal.
});
import {Decimal} from 'decimal.js';
```
## Use
@ -165,11 +158,11 @@ pi.toFraction() // [ '7853982301', '2500000000' ]
pi.toFraction(1000) // [ '355', '113' ]
```
All calculations are rounded according to the number of significant digits and rounding mode
specified by the `precision` and `rounding` properties of the Decimal constructor.
All calculations are rounded according to the number of significant digits and rounding mode specified
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
applies to all Decimal numbers created from it.
For advanced usage, multiple Decimal constructors can be created, each with their own independent
configuration which applies to all Decimal numbers created from it.
```js
// 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
```bash
$ npm test
npm test
```
or at the *test* directory using Node
```bash
$ node test
node test
```
Each separate test module can also be executed individually, for example, at the *test/modules* directory
```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
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
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

2
decimal.d.ts vendored
View File

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

2
decimal.global.d.ts vendored
View File

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

View File

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

2
decimal.min.mjs Normal file

File diff suppressed because one or more lines are too long

1
decimal.min.mjs.map Normal file

File diff suppressed because one or more lines are too long

View File

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

View File

@ -439,7 +439,7 @@ b = new Decimal(x).ceil()
a.equals(b) // true</pre>
<h5 id="Dclamp">clamp<code class='inset'>.clamp(min, max) <i>&rArr; Decimal</i></code></h5>
<p>
<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>
significant digits except <a href='#decimal'><code>Decimal</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='#neg'><code>negated</code></a>, <a href='#round'><code>round</code></a>,
<a href='#ceil'><code>ceil</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='#toNearest'><code>toNearest</code></a> and
<a href='#trunc'><code>truncated</code></a>.
@ -1668,10 +1671,6 @@ x = new Decimal(1)
x.isFinite() // true
y = new Decimal(Infinity)
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
y = new Decimal('Infinity')
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
y = new Decimal(2)
y.isNeg // false</pre>
<p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
<p>Also note that signed zeroes are implemented, following the IEEE Standard
for Floating-Point Arithmetic (IEEE 754).</p>
<p>Note that zero is signed.</p>
<pre>
Decimal(0).valueOf() // '0'
Decimal(0).isNegative() // false
Decimal(0).negated().valueOf() // '-0'
Decimal(0).negated().isNegative() // true
new Decimal(0).valueOf() // '0'
new Decimal(0).isNegative() // false
new Decimal(0).negated().valueOf() // '-0'
new Decimal(0).negated().isNegative() // true
new Decimal(-0).isNegative() // true
</pre>
<h5 id="isPos">isPositive<code class='inset'>.isPos() <i>&rArr; boolean</i></code></h5>
<p>
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
y = new Decimal(-2)
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
y = new Decimal(Infinity)
y.isZero() // false</pre>
<p>Note: <code>n == 0</code> can be used if <code>n &gt;= Number.MIN_VALUE</code>.</p>

View File

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

View File

@ -24,11 +24,9 @@ T('clamp', function () {
t(1, 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, 'NaN');
t(2, 1, 2, '2');
t(3, 1, 2, '2');
t(1, 0, 1, '1');
@ -39,8 +37,6 @@ T('clamp', function () {
t(-Infinity, 0, 1, '0');
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, NaN, 1, 'NaN');