mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2024-10-27 20:34:12 +00:00
v10.3.0
This commit is contained in:
parent
c29c80c6e3
commit
220f11c498
@ -1,6 +1,7 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "node"
|
||||
- "15"
|
||||
- "14"
|
||||
- "13"
|
||||
- "12"
|
||||
|
14
CHANGELOG.md
14
CHANGELOG.md
@ -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.
|
||||
|
@ -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
|
||||
|
53
README.md
53
README.md
@ -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
2
decimal.d.ts
vendored
@ -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
2
decimal.global.d.ts
vendored
@ -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);
|
||||
|
||||
|
12
decimal.js
12
decimal.js
@ -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);
|
||||
};
|
||||
@ -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
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
2
decimal.min.mjs
Normal file
File diff suppressed because one or more lines are too long
1
decimal.min.mjs.map
Normal file
1
decimal.min.mjs.map
Normal file
File diff suppressed because one or more lines are too long
15
decimal.mjs
15
decimal.mjs
@ -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
|
||||
@ -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);
|
||||
};
|
||||
@ -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);
|
||||
|
31
doc/API.html
31
doc/API.html
@ -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 <= 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 < 0</code> can be used if <code>n <= -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>⇒ 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 < 0</code> can be used if <code>n <= -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 >= Number.MIN_VALUE</code>.</p>
|
||||
|
||||
|
||||
|
||||
|
12
package.json
12
package.json
@ -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"
|
||||
]
|
||||
}
|
||||
|
@ -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');
|
||||
|
Loading…
Reference in New Issue
Block a user