1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00

Compare commits

..

No commits in common. "master" and "v7.4.0" have entirely different histories.

22 changed files with 441 additions and 1478 deletions

View File

@ -1,16 +1,6 @@
language: node_js
node_js:
- "node"
- "18"
- "17"
- "16"
- "15"
- "14"
- "13"
- "12"
- "11"
- "10"
- "9"
- "8"
- "7"
- "6"

View File

@ -1,103 +1,6 @@
#### 10.4.3
* 04/12/2022
* #211 Remove `toStringTag` declaration for type compatibility.
#### 10.4.2
* 12/10/2022
* #209 Correct return type.
#### 10.4.1
* 16/09/2022
* #205 Add './decimal' subpath to *package.json* `exports`.
#### 10.4.0
* 14/08/2022
* #201 Add `exports` field to *package.json*.
* #203 Preserve license comment after bundling.
* #198 Use type predicate on `isDecimal`.
#### 10.3.1
* 25/06/2021
* Remove minified versions. Refresh *README*.
#### 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.
#### 10.2.0
* 08/05/2019
* #128 Workaround V8 `Math.pow` change.
* #93 Accept `+` prefix when parsing string values.
* #129 Fix typo.
#### 10.1.1
* 27/02/2019
* Check `Symbol` properly.
#### 10.1.0
* 26/02/2019
* #122 Add custom `util.inspect()` function.
* Add `Symbol.toStringTag`.
* #121 Constructor: add range check for arguments of type number and Decimal.
* Remove premable from uglifyjs build script.
* Move *decimal.min.js.map* to root directory.
#### 10.0.2
* 13/12/2018
* #114 Remove soureMappingURL from *decimal.min.js*.
* Remove *bower.json*.
#### 10.0.1
* 24/05/2018
* Add `browser` field to *package.json*.
#### 10.0.0
* 10/03/2018
* #88 `toNearest` to return the nearest multiple in the direction of the rounding mode.
* #82 #91 `const` to `var`.
* Add trigonometric precision limit explanantion to documentation.
* Put global ts definitions in separate file (see *bignumber.js* #143).
#### 9.0.1
* 15/12/2017
* #80 Typings: correct return type.
#### 9.0.0
* 14/12/2017
* #78 Typings: remove `toFormat`.
#### 8.0.0
* 10/12/2017
* Correct typings: `toFraction` returns `Decimal[]`.
* Type-checking: add `Decimal.isDecimal` method.
* Enable configuration reset with `defaults: true`.
* Add named export, Decimal, to *decimal.mjs*.
#### 7.5.1
* 03/12/2017
* Remove typo.
#### 7.5.0
* 03/12/2017
* Use TypeScript declarations outside modules.
#### 7.4.0
* 25/11/2017
* Add TypeScript typings.
* Add Typescript typings.
#### 7.3.0
* 26/09/2017

View File

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

167
README.md
View File

@ -2,8 +2,6 @@
An arbitrary-precision Decimal type for JavaScript.
[![npm version](https://img.shields.io/npm/v/decimal.js.svg)](https://www.npmjs.com/package/decimal.js)
[![npm downloads](https://img.shields.io/npm/dw/decimal.js)](https://www.npmjs.com/package/decimal.js)
[![Build Status](https://travis-ci.org/MikeMcl/decimal.js.svg)](https://travis-ci.org/MikeMcl/decimal.js)
[![CDNJS](https://img.shields.io/cdnjs/v/decimal.js.svg)](https://cdnjs.com/libraries/decimal.js)
@ -18,9 +16,7 @@ An arbitrary-precision Decimal type for JavaScript.
- Faster, smaller, and perhaps easier to use than JavaScript versions of Java's BigDecimal
- No dependencies
- Wide platform compatibility: uses JavaScript 1.5 (ECMAScript 3) features only
- Comprehensive [documentation](https://mikemcl.github.io/decimal.js/) and test set
- Used under the hood by [math.js](https://github.com/josdejong/mathjs)
- Includes a TypeScript declaration file: *decimal.d.ts*
- Comprehensive [documentation](http://mikemcl.github.io/decimal.js/) and test set
![API](https://raw.githubusercontent.com/MikeMcl/decimal.js/gh-pages/API.png)
@ -33,43 +29,47 @@ 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 the
[v4.x.x](https://github.com/MikeMcl/decimal.js/tree/v4.x.x) branch where version 4 continues to be supported, or better, see [decimal.js-light](https://github.com/MikeMcl/decimal.js-light/), which is lighter still.
## Load
The library is the single JavaScript file *decimal.js* or ES module *decimal.mjs*.
The library is the single JavaScript file *decimal.js* (or minified, *decimal.min.js*).
Browser:
It can be loaded using a script tag in an HTML document for the browser
```html
<script src='path/to/decimal.js'></script>
<script type="module">
import Decimal from './path/to/decimal.mjs';
...
</script>
```
[Node.js](https://nodejs.org):
or as a [Node.js](http://nodejs.org) module using `require`.
```js
var Decimal = require('decimal.js');
```
For Node, the library is available from the [npm](https://npmjs.org/) registry
```bash
npm install decimal.js
$ npm install decimal.js
```
To load with AMD loader libraries such as [requireJS](http://requirejs.org/):
```js
const Decimal = require('decimal.js');
import Decimal from 'decimal.js';
import {Decimal} from 'decimal.js';
require(['decimal'], function(Decimal) {
// Use Decimal here in local scope. No global Decimal.
});
```
## Use
*In all examples below, semicolons and `toString` calls are not shown.
*In all examples below, `var`, semicolons and `toString` calls are not shown.
If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
The library exports a single constructor function, `Decimal`, which expects a single argument that is a number, string or Decimal instance.
The library exports a single function object, `Decimal`, the constructor of Decimal instances.
It accepts a value of type number, string or Decimal.
```js
x = new Decimal(123.4567)
@ -78,29 +78,7 @@ z = new Decimal(x)
x.equals(y) && y.equals(z) && x.equals(z) // true
```
If using values with more than a few digits, it is recommended to pass strings rather than numbers to avoid a potential loss of precision.
```js
// Precision loss from using numeric literals with more than 15 significant digits.
new Decimal(1.0000000000000001) // '1'
new Decimal(88259496234518.57) // '88259496234518.56'
new Decimal(99999999999999999999) // '100000000000000000000'
// Precision loss from using numeric literals outside the range of Number values.
new Decimal(2e+308) // 'Infinity'
new Decimal(1e-324) // '0'
// Precision loss from the unexpected result of arithmetic with Number values.
new Decimal(0.7 + 0.1) // '0.7999999999999999'
```
As with JavaScript numbers, strings can contain underscores as separators to improve readability.
```js
x = new Decimal('2_147_483_647')
```
String values in binary, hexadecimal or octal notation are also accepted if the appropriate prefix is included.
A value can also be in binary, hexadecimal or octal if the appropriate prefix is included.
```js
x = new Decimal('0xff.f') // '255.9375'
@ -109,13 +87,9 @@ z = x.plus(y) // '427.9375'
z.toBinary() // '0b110101011.1111'
z.toBinary(13) // '0b1.101010111111p+8'
// Using binary exponential notation to create a Decimal with the value of `Number.MAX_VALUE`.
x = new Decimal('0b1.1111111111111111111111111111111111111111111111111111p+1023')
// '1.7976931348623157081e+308'
```
Decimal instances are immutable in the sense that they are not changed by their methods.
A Decimal is immutable in the sense that it is not changed by its methods.
```js
0.3 - 0.1 // 0.19999999999999998
@ -134,28 +108,27 @@ x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.11177
Many method names have a shorter alias.
```js
x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true
x.comparedTo(y.modulo(z).negated() === x.cmp(y.mod(z).neg()) // true
x.squareRoot().dividedBy(y).toPower(3).equals(x.sqrt().div(y).pow(3)) // true
x.cmp(y.mod(z).neg()) == 1 && x.comparedTo(y.modulo(z).negated()) == 1 // true
```
Most of the methods of JavaScript's `Number.prototype` and `Math` objects are replicated.
Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods,
```js
x = new Decimal(255.5)
x.toExponential(5) // '2.55500e+2'
x.toFixed(5) // '255.50000'
x.toPrecision(5) // '255.50'
Decimal.sqrt('6.98372465832e+9823') // '8.3568682281821340204e+4911'
Decimal.pow(2, 0.0979843) // '1.0702770511687781839'
// Using `toFixed()` to avoid exponential notation:
x = new Decimal('0.0000001')
x.toString() // '1e-7'
x.toFixed() // '0.0000001'
x.toExponential(5) // '2.55500e+2'
x.toFixed(5) // '255.50000'
x.toPrecision(5) // '255.50'
```
And there are `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal` values.
and almost all of the methods of JavaScript's Math object are also replicated.
```js
Decimal.sqrt('6.98372465832e+9823') // '8.3568682281821340204e+4911'
Decimal.pow(2, 0.0979843) // '1.0702770511687781839'
```
There are `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal` values,
```js
x = new Decimal(NaN) // 'NaN'
@ -163,7 +136,7 @@ y = new Decimal(Infinity) // 'Infinity'
x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
```
There is also a `toFraction` method with an optional *maximum denominator* argument.
and a `toFraction` method with an optional *maximum denominator* argument
```js
z = new Decimal(355)
@ -172,27 +145,27 @@ 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.
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
Decimal.set({ precision: 5, rounding: 4 })
// Create another Decimal constructor, optionally passing in a configuration object
Dec = Decimal.clone({ precision: 9, rounding: 1 })
Decimal10 = Decimal.clone({ precision: 10, rounding: 1 })
x = new Decimal(5)
y = new Dec(5)
y = new Decimal10(5)
x.div(3) // '1.6667'
y.div(3) // '1.66666666'
y.div(3) // '1.666666666'
```
The value of a Decimal is stored in a floating point format in terms of its digits, exponent and sign, but these properties should be considered read-only.
The value of a Decimal is stored in a floating point format in terms of its digits, exponent and sign.
```js
x = new Decimal(-12345.67);
@ -205,42 +178,54 @@ For further information see the [API](http://mikemcl.github.io/decimal.js/) refe
## Test
To run the tests using Node.js from the root directory:
The library can be tested using Node.js or a browser.
The *test* directory contains the file *test.js* which runs all the tests when executed by Node,
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
```
Each separate test module can also be executed individually, for example:
or at the *test* directory using Node
```bash
node test/modules/toFraction
$ node test
```
To run the tests in a browser, open *test/test.html*.
Each separate test module can also be executed individually, for example, at the *test/modules* directory
## Minify
```bash
$ node toFraction
```
Two minification examples:
## Build
Using [uglify-js](https://github.com/mishoo/UglifyJS) to minify the *decimal.js* file:
For Node, if [uglify-js](https://github.com/mishoo/UglifyJS2) is installed
```bash
npm install uglify-js -g
uglifyjs decimal.js --source-map url=decimal.min.js.map -c -m -o decimal.min.js
```
Using [terser](https://github.com/terser/terser) to minify the ES module version, *decimal.mjs*:
then
```bash
npm install terser -g
terser decimal.mjs --source-map url=decimal.min.mjs.map -c -m --toplevel -o decimal.min.mjs
npm run build
```
```js
import Decimal from './decimal.min.mjs';
```
will create *decimal.min.js*, and a source map will also be added to the *doc* directory.
## Feedback
<a href='mailto:M8ch88l@gmail.com'>M8ch88l@gmail.com</a>
**BTC** 16MjxmTB5EZxY5Uk9xyhfsu4n9gYxEJYkY
## Licence
[The MIT Licence (Expat).](LICENCE.md)
MIT.
See *LICENCE.md*

33
bower.json Normal file
View File

@ -0,0 +1,33 @@
{
"name": "decimal.js",
"main": "decimal.js",
"version": "7.4.0",
"homepage": "https://github.com/MikeMcl/decimal.js",
"authors": [
"Michael Mclaughlin <M8ch88l@gmail.com>"
],
"description": "An arbitrary-precision Decimal type for JavaScript",
"moduleType": [
"amd",
"globals",
"node"
],
"keywords": [
"arbitrary",
"precision",
"arithmetic",
"bignumber",
"decimal",
"float",
"biginteger",
"bigdecimal",
"bignum",
"math"
],
"license": "MIT",
"ignore": [
".*",
"*.json",
"test"
]
}

85
decimal.d.ts vendored
View File

@ -7,39 +7,35 @@
//
// Exports:
//
// class Decimal (default export)
// class Decimal (default)
// type Decimal.Constructor
// type Decimal.Instance
// type Decimal.Modulo
// type Decimal.Rounding
// type Decimal.Value
// type Decimal.Rounding
// type Decimal.Modulo
// interface Decimal.Config
// interface Decimal.Format
//
// Example (alternative syntax commented-out):
// Example:
//
// import {Decimal} from "decimal.js"
// //import Decimal from "decimal.js"
// import Decimal from "decimal.js"
//
// let r: Decimal.Rounding = Decimal.ROUND_UP;
// let c: Decimal.Configuration = {precision: 4, rounding: r};
// Decimal.set(c);
// let v: Decimal.Value = '12345.6789';
// let d: Decimal = new Decimal(v);
// //let d: Decimal.Instance = new Decimal(v);
//
// The use of compiler option `--strictNullChecks` is recommended.
export default Decimal;
export namespace Decimal {
export type Constructor = typeof Decimal;
export type Instance = Decimal;
export type Value = string | number | Decimal;
export type Rounding = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
export type Modulo = Rounding | 9;
export type Value = string | number | Decimal;
// http://mikemcl.github.io/decimal.js/#constructor-properties
export interface Config {
export interface Configuration {
precision?: number;
rounding?: Rounding;
toExpNeg?: number;
@ -48,7 +44,19 @@ export namespace Decimal {
maxE?: number;
crypto?: boolean;
modulo?: Modulo;
defaults?: boolean;
// Requires 'toFormat' <https://github.com/MikeMcl/toFormat>.
format?: Format;
}
// Requires 'toFormat'.
export interface Format {
decimalSeparator?: string;
groupSeparator?: string;
groupSize?: number;
secondaryGroupSize?: number;
fractionGroupSeparator?: string;
fractionGroupSize?: number;
}
}
@ -63,9 +71,6 @@ export declare class Decimal {
abs(): Decimal;
ceil(): Decimal;
clampedTo(min: Decimal.Value, max: Decimal.Value): Decimal;
clamp(min: Decimal.Value, max: Decimal.Value): Decimal;
comparedTo(n: Decimal.Value): number;
cmp(n: Decimal.Value): number;
@ -182,8 +187,8 @@ export declare class Decimal {
times(n: Decimal.Value): Decimal;
mul(n: Decimal.Value) : Decimal;
toBinary(significantDigits?: number): string;
toBinary(significantDigits: number, rounding: Decimal.Rounding): string;
toBinary(significantDigits?: number): Decimal;
toBinary(significantDigits: number, rounding: Decimal.Rounding): Decimal;
toDecimalPlaces(decimalPlaces?: number): Decimal;
toDecimalPlaces(decimalPlaces: number, rounding: Decimal.Rounding): Decimal;
@ -196,12 +201,15 @@ export declare class Decimal {
toFixed(decimalPlaces?: number): string;
toFixed(decimalPlaces: number, rounding: Decimal.Rounding): string;
toFraction(max_denominator?: Decimal.Value): Decimal[];
// Requires 'toFormat'.
toFormat?(decimalPlaces: number, rounding: Decimal.Rounding): string;
toHexadecimal(significantDigits?: number): string;
toHexadecimal(significantDigits: number, rounding: Decimal.Rounding): string;
toHex(significantDigits?: number): string;
toHex(significantDigits: number, rounding?: Decimal.Rounding): string;
toFraction(max_denominator?: Decimal.Value): string[];
toHexadecimal(significantDigits?: number): Decimal;
toHexadecimal(significantDigits: number, rounding: Decimal.Rounding): Decimal;
toHex(significantDigits?: number): Decimal;
toHex(significantDigits: number, rounding: Decimal.Rounding): Decimal;
toJSON(): string;
@ -209,8 +217,8 @@ export declare class Decimal {
toNumber(): number;
toOctal(significantDigits?: number): string;
toOctal(significantDigits: number, rounding: Decimal.Rounding): string;
toOctal(significantDigits?: number): Decimal;
toOctal(significantDigits: number, rounding: Decimal.Rounding): Decimal;
toPower(n: Decimal.Value): Decimal;
pow(n: Decimal.Value): Decimal;
@ -241,41 +249,41 @@ export declare class Decimal {
static atan2(y: Decimal.Value, x: Decimal.Value): Decimal;
static cbrt(n: Decimal.Value): Decimal;
static ceil(n: Decimal.Value): Decimal;
static clamp(n: Decimal.Value, min: Decimal.Value, max: Decimal.Value): Decimal;
static clone(object?: Decimal.Config): Decimal.Constructor;
static config(object: Decimal.Config): Decimal.Constructor;
static clone(object?: Decimal.Configuration): Decimal.Constructor;
static config(object: Decimal.Configuration): Decimal.Constructor;
static cos(n: Decimal.Value): Decimal;
static cosh(n: Decimal.Value): Decimal;
static div(x: Decimal.Value, y: Decimal.Value): Decimal;
static exp(n: Decimal.Value): Decimal;
static floor(n: Decimal.Value): Decimal;
static hypot(...n: Decimal.Value[]): Decimal;
static isDecimal(object: any): object is Decimal;
static ln(n: Decimal.Value): Decimal;
static log(n: Decimal.Value, base?: Decimal.Value): Decimal;
static log(n: Decimal.Value, base?: number): Decimal;
static log2(n: Decimal.Value): Decimal;
static log10(n: Decimal.Value): Decimal;
static max(...n: Decimal.Value[]): Decimal;
static min(...n: Decimal.Value[]): Decimal;
static mod(x: Decimal.Value, y: Decimal.Value): Decimal;
static mul(x: Decimal.Value, y: Decimal.Value): Decimal;
static noConflict(): Decimal.Constructor; // Browser only
// Browser only
static noConflict(): Decimal.Constructor;
static pow(base: Decimal.Value, exponent: Decimal.Value): Decimal;
static random(significantDigits?: number): Decimal;
static round(n: Decimal.Value): Decimal;
static set(object: Decimal.Config): Decimal.Constructor;
static sign(n: Decimal.Value): number;
static set(object: Decimal.Configuration): Decimal.Constructor;
static sign(n: Decimal.Value): Decimal;
static sin(n: Decimal.Value): Decimal;
static sinh(n: Decimal.Value): Decimal;
static sqrt(n: Decimal.Value): Decimal;
static sub(x: Decimal.Value, y: Decimal.Value): Decimal;
static sum(...n: Decimal.Value[]): Decimal;
static tan(n: Decimal.Value): Decimal;
static tanh(n: Decimal.Value): Decimal;
static trunc(n: Decimal.Value): Decimal;
static readonly default?: Decimal.Constructor;
static readonly Decimal?: Decimal.Constructor;
static readonly default: Decimal.Constructor;
static readonly Decimal: Decimal.Constructor;
static readonly precision: number;
static readonly rounding: Decimal.Rounding;
@ -286,6 +294,9 @@ export declare class Decimal {
static readonly crypto: boolean;
static readonly modulo: Decimal.Modulo;
// Requires 'toFormat'.
static readonly format?: Decimal.Format;
static readonly ROUND_UP: 0;
static readonly ROUND_DOWN: 1;
static readonly ROUND_CEIL: 2;

320
decimal.global.d.ts vendored
View File

@ -1,320 +0,0 @@
// Type definitions for decimal.js >=7.0.0
// Project: https://github.com/MikeMcl/decimal.js
// Definitions by: Michael Mclaughlin <https://github.com/MikeMcl>
// Definitions: https://github.com/MikeMcl/decimal.js
//
// Documentation: http://mikemcl.github.io/decimal.js/
//
// Exports (available globally or when using import):
//
// class Decimal (default export)
// type Decimal.Constructor
// type Decimal.Instance
// type Decimal.Modulo
// type Decimal.Rounding
// type Decimal.Value
// interface Decimal.Config
//
// Example (alternative syntax commented-out):
//
// import {Decimal} from "decimal.js"
// //import Decimal from "decimal.js"
//
// let r: Decimal.Rounding = Decimal.ROUND_UP;
// let c: Decimal.Configuration = {precision: 4, rounding: r};
// Decimal.set(c);
// let v: Decimal.Value = '12345.6789';
// let d: Decimal = new Decimal(v);
// //let d: Decimal.Instance = new Decimal(v);
//
// The use of compiler option `--strictNullChecks` is recommended.
export default Decimal;
export namespace Decimal {
export type Config = DecimalConfig;
export type Constructor = DecimalConstructor;
export type Instance = DecimalInstance;
export type Modulo = DecimalModulo;
export type Rounding = DecimalRounding;
export type Value = DecimalValue;
}
declare global {
const Decimal: DecimalConstructor;
type Decimal = DecimalInstance;
namespace Decimal {
type Config = DecimalConfig;
type Constructor = DecimalConstructor;
type Instance = DecimalInstance;
type Modulo = DecimalModulo;
type Rounding = DecimalRounding;
type Value = DecimalValue;
}
}
type DecimalInstance = Decimal;
type DecimalConstructor = typeof Decimal;
type DecimalValue = string | number | Decimal;
type DecimalRounding = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
type DecimalModulo = DecimalRounding | 9;
// http://mikemcl.github.io/decimal.js/#constructor-properties
interface DecimalConfig {
precision?: number;
rounding?: DecimalRounding;
toExpNeg?: number;
toExpPos?: number;
minE?: number;
maxE?: number;
crypto?: boolean;
modulo?: DecimalModulo;
defaults?: boolean;
}
export declare class Decimal {
readonly d: number[];
readonly e: number;
readonly s: number;
constructor(n: DecimalValue);
absoluteValue(): Decimal;
abs(): Decimal;
ceil(): Decimal;
clampedTo(min: Decimal.Value, max: Decimal.Value): Decimal;
clamp(min: Decimal.Value, max: Decimal.Value): Decimal;
comparedTo(n: DecimalValue): number;
cmp(n: DecimalValue): number;
cosine(): Decimal;
cos(): Decimal;
cubeRoot(): Decimal;
cbrt(): Decimal;
decimalPlaces(): number;
dp(): number;
dividedBy(n: DecimalValue): Decimal;
div(n: DecimalValue): Decimal;
dividedToIntegerBy(n: DecimalValue): Decimal;
divToInt(n: DecimalValue): Decimal;
equals(n: DecimalValue): boolean;
eq(n: DecimalValue): boolean;
floor(): Decimal;
greaterThan(n: DecimalValue): boolean;
gt(n: DecimalValue): boolean;
greaterThanOrEqualTo(n: DecimalValue): boolean;
gte(n: DecimalValue): boolean;
hyperbolicCosine(): Decimal;
cosh(): Decimal;
hyperbolicSine(): Decimal;
sinh(): Decimal;
hyperbolicTangent(): Decimal;
tanh(): Decimal;
inverseCosine(): Decimal;
acos(): Decimal;
inverseHyperbolicCosine(): Decimal;
acosh(): Decimal;
inverseHyperbolicSine(): Decimal;
asinh(): Decimal;
inverseHyperbolicTangent(): Decimal;
atanh(): Decimal;
inverseSine(): Decimal;
asin(): Decimal;
inverseTangent(): Decimal;
atan(): Decimal;
isFinite(): boolean;
isInteger(): boolean;
isInt(): boolean;
isNaN(): boolean;
isNegative(): boolean;
isNeg(): boolean;
isPositive(): boolean;
isPos(): boolean;
isZero(): boolean;
lessThan(n: DecimalValue): boolean;
lt(n: DecimalValue): boolean;
lessThanOrEqualTo(n: DecimalValue): boolean;
lte(n: DecimalValue): boolean;
logarithm(n?: DecimalValue): Decimal;
log(n?: DecimalValue): Decimal;
minus(n: DecimalValue): Decimal;
sub(n: DecimalValue): Decimal;
modulo(n: DecimalValue): Decimal;
mod(n: DecimalValue): Decimal;
naturalExponential(): Decimal;
exp(): Decimal;
naturalLogarithm(): Decimal;
ln(): Decimal;
negated(): Decimal;
neg(): Decimal;
plus(n: DecimalValue): Decimal;
add(n: DecimalValue): Decimal;
precision(includeZeros?: boolean): number;
sd(includeZeros?: boolean): number;
round(): Decimal;
sine() : Decimal;
sin() : Decimal;
squareRoot(): Decimal;
sqrt(): Decimal;
tangent() : Decimal;
tan() : Decimal;
times(n: DecimalValue): Decimal;
mul(n: DecimalValue) : Decimal;
toBinary(significantDigits?: number): string;
toBinary(significantDigits: number, rounding: DecimalRounding): string;
toDecimalPlaces(decimalPlaces?: number): Decimal;
toDecimalPlaces(decimalPlaces: number, rounding: DecimalRounding): Decimal;
toDP(decimalPlaces?: number): Decimal;
toDP(decimalPlaces: number, rounding: DecimalRounding): Decimal;
toExponential(decimalPlaces?: number): string;
toExponential(decimalPlaces: number, rounding: DecimalRounding): string;
toFixed(decimalPlaces?: number): string;
toFixed(decimalPlaces: number, rounding: DecimalRounding): string;
toFraction(max_denominator?: DecimalValue): Decimal[];
toHexadecimal(significantDigits?: number): string;
toHexadecimal(significantDigits: number, rounding: DecimalRounding): string;
toHex(significantDigits?: number): string;
toHex(significantDigits: number, rounding?: DecimalRounding): string;
toJSON(): string;
toNearest(n: DecimalValue, rounding?: DecimalRounding): Decimal;
toNumber(): number;
toOctal(significantDigits?: number): string;
toOctal(significantDigits: number, rounding: DecimalRounding): string;
toPower(n: DecimalValue): Decimal;
pow(n: DecimalValue): Decimal;
toPrecision(significantDigits?: number): string;
toPrecision(significantDigits: number, rounding: DecimalRounding): string;
toSignificantDigits(significantDigits?: number): Decimal;
toSignificantDigits(significantDigits: number, rounding: DecimalRounding): Decimal;
toSD(significantDigits?: number): Decimal;
toSD(significantDigits: number, rounding: DecimalRounding): Decimal;
toString(): string;
truncated(): Decimal;
trunc(): Decimal;
valueOf(): string;
static abs(n: DecimalValue): Decimal;
static acos(n: DecimalValue): Decimal;
static acosh(n: DecimalValue): Decimal;
static add(x: DecimalValue, y: DecimalValue): Decimal;
static asin(n: DecimalValue): Decimal;
static asinh(n: DecimalValue): Decimal;
static atan(n: DecimalValue): Decimal;
static atanh(n: DecimalValue): Decimal;
static atan2(y: DecimalValue, x: DecimalValue): Decimal;
static cbrt(n: DecimalValue): Decimal;
static ceil(n: DecimalValue): Decimal;
static clamp(n: Decimal.Value, min: Decimal.Value, max: Decimal.Value): Decimal;
static clone(object?: DecimalConfig): DecimalConstructor;
static config(object: DecimalConfig): DecimalConstructor;
static cos(n: DecimalValue): Decimal;
static cosh(n: DecimalValue): Decimal;
static div(x: DecimalValue, y: DecimalValue): Decimal;
static exp(n: DecimalValue): Decimal;
static floor(n: DecimalValue): Decimal;
static hypot(...n: DecimalValue[]): Decimal;
static isDecimal(object: any): object is Decimal;
static ln(n: DecimalValue): Decimal;
static log(n: DecimalValue, base?: DecimalValue): Decimal;
static log2(n: DecimalValue): Decimal;
static log10(n: DecimalValue): Decimal;
static max(...n: DecimalValue[]): Decimal;
static min(...n: DecimalValue[]): Decimal;
static mod(x: DecimalValue, y: DecimalValue): Decimal;
static mul(x: DecimalValue, y: DecimalValue): Decimal;
static noConflict(): DecimalConstructor; // Browser only
static pow(base: DecimalValue, exponent: DecimalValue): Decimal;
static random(significantDigits?: number): Decimal;
static round(n: DecimalValue): Decimal;
static set(object: DecimalConfig): DecimalConstructor;
static sign(n: DecimalValue): number;
static sin(n: DecimalValue): Decimal;
static sinh(n: DecimalValue): Decimal;
static sqrt(n: DecimalValue): Decimal;
static sub(x: DecimalValue, y: DecimalValue): Decimal;
static sum(...n: Decimal.Value[]): Decimal;
static tan(n: DecimalValue): Decimal;
static tanh(n: DecimalValue): Decimal;
static trunc(n: DecimalValue): Decimal;
static readonly default?: DecimalConstructor;
static readonly Decimal?: DecimalConstructor;
static readonly precision: number;
static readonly rounding: DecimalRounding;
static readonly toExpNeg: number;
static readonly toExpPos: number;
static readonly minE: number;
static readonly maxE: number;
static readonly crypto: boolean;
static readonly modulo: DecimalModulo;
static readonly ROUND_UP: 0;
static readonly ROUND_DOWN: 1;
static readonly ROUND_CEIL: 2;
static readonly ROUND_FLOOR: 3;
static readonly ROUND_HALF_UP: 4;
static readonly ROUND_HALF_DOWN: 5;
static readonly ROUND_HALF_EVEN: 6;
static readonly ROUND_HALF_CEIL: 7;
static readonly ROUND_HALF_FLOOR: 8;
static readonly EUCLID: 9;
}

View File

@ -1,12 +1,13 @@
/*! decimal.js v7.4.0 https://github.com/MikeMcl/decimal.js/LICENCE */
;(function (globalScope) {
'use strict';
/*!
* decimal.js v10.4.3
/*
* decimal.js v7.4.0
* An arbitrary-precision Decimal type for JavaScript.
* https://github.com/MikeMcl/decimal.js
* Copyright (c) 2022 Michael Mclaughlin <M8ch88l@gmail.com>
* Copyright (c) 2017 Michael Mclaughlin <M8ch88l@gmail.com>
* MIT Licence
*/
@ -33,7 +34,7 @@
// The initial configuration properties of the Decimal constructor.
DEFAULTS = {
Decimal = {
// These values must be integers within the stated ranges (inclusive).
// Most of these values can be changed at run-time using the `Decimal.config` method.
@ -98,14 +99,13 @@
// ----------------------------------- END OF EDITABLE DEFAULTS ------------------------------- //
Decimal, inexact, noConflict, quadrant,
inexact, noConflict, quadrant,
external = true,
decimalError = '[DecimalError] ',
invalidArgument = decimalError + 'Invalid argument: ',
precisionLimitExceeded = decimalError + 'Precision limit exceeded',
cryptoUnavailable = decimalError + 'crypto unavailable',
tag = '[object Decimal]',
mathfloor = Math.floor,
mathpow = Math.pow,
@ -123,7 +123,7 @@
PI_PRECISION = PI.length - 1,
// Decimal.prototype object
P = { toStringTag: tag };
P = {};
// Decimal prototype methods
@ -132,7 +132,6 @@
/*
* absoluteValue abs
* ceil
* clampedTo clamp
* comparedTo cmp
* cosine cos
* cubeRoot cbrt
@ -214,27 +213,6 @@
};
/*
* Return a new Decimal whose value is the value of this Decimal clamped to the range
* delineated by `min` and `max`.
*
* min {number|string|Decimal}
* max {number|string|Decimal}
*
*/
P.clampedTo = P.clamp = function (min, max) {
var k,
x = this,
Ctor = x.constructor;
min = new Ctor(min);
max = new Ctor(max);
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);
};
/*
* Return
* 1 if the value of this Decimal is greater than the value of `y`,
@ -340,7 +318,7 @@
external = false;
// Initial estimate.
s = x.s * mathpow(x.s * x, 1 / 3);
s = x.s * Math.pow(x.s * x, 1 / 3);
// Math.cbrt underflow/overflow?
// Pass x to Math.pow as integer, then adjust the exponent of the result.
@ -350,7 +328,7 @@
// Adjust n exponent so it is a multiple of 3 away from x exponent.
if (s = (e - n.length + 1) % 3) n += (s == 1 || s == -2 ? '0' : '00');
s = mathpow(n, 1 / 3);
s = Math.pow(n, 1 / 3);
// Rarely, e may be one less than the result exponent value.
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
@ -569,7 +547,7 @@
// TODO? Estimation reused from cosine() and may not be optimal here.
if (len < 32) {
k = Math.ceil(len / 3);
n = (1 / tinyPow(4, k)).toString();
n = Math.pow(4, -k).toString();
} else {
k = 16;
n = '2.3283064365386962890625e-10';
@ -649,7 +627,8 @@
k = 1.4 * Math.sqrt(len);
k = k > 16 ? 16 : k | 0;
x = x.times(1 / tinyPow(5, k));
x = x.times(Math.pow(5, -k));
x = taylorSeries(Ctor, 2, x, x, true);
// Reverse argument reduction
@ -1751,7 +1730,7 @@
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
if (s == 1 / 0) {
n = '5e' + e;
n = '1e' + e;
} else {
n = s.toExponential();
n = n.slice(0, n.indexOf('e') + 1) + e;
@ -2133,9 +2112,16 @@
};
/*
* Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding
* mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal.
* Returns a new Decimal whose value is the nearest multiple of the magnitude of `y` to the value
* of this Decimal.
*
* If the value of this Decimal is equidistant from two multiples of `y`, the rounding mode `rm`,
* or `Decimal.rounding` if `rm` is omitted, determines the direction of the nearest multiple.
*
* In the context of this method, rounding mode 4 (ROUND_HALF_UP) is the same as rounding mode 0
* (ROUND_UP), and so on.
*
* The return value will always have the same sign as this Decimal, unless either this Decimal
* or `y` is NaN, in which case the return value will be also be NaN.
@ -2164,11 +2150,7 @@
rm = Ctor.rounding;
} else {
y = new Ctor(y);
if (rm === void 0) {
rm = Ctor.rounding;
} else {
checkInt32(rm, 0, 8);
}
if (rm !== void 0) checkInt32(rm, 0, 8);
// If x is not finite, return x if y is not NaN, else NaN.
if (!x.d) return y.s ? x : y;
@ -2183,6 +2165,7 @@
// If y is not zero, calculate the nearest multiple of y to x.
if (y.d[0]) {
external = false;
if (rm < 4) rm = [4, 5, 7, 8][rm];
x = divide(x, y, 0, rm, 1).times(y);
external = true;
finalise(x);
@ -2468,6 +2451,18 @@
};
/*
// Add aliases to match BigDecimal method names.
// P.add = P.plus;
P.subtract = P.minus;
P.multiply = P.times;
P.divide = P.div;
P.remainder = P.mod;
P.compareTo = P.cmp;
P.negate = P.neg;
*/
// Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers.
@ -2639,18 +2634,16 @@
*
*/
function cosine(Ctor, x) {
var k, len, y;
if (x.isZero()) return x;
var k, y,
len = x.d.length;
// Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
// i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1
// Estimate the optimum number of times to use the argument reduction.
len = x.d.length;
if (len < 32) {
k = Math.ceil(len / 3);
y = (1 / tinyPow(4, k)).toString();
y = Math.pow(4, -k).toString();
} else {
k = 16;
y = '2.3283064365386962890625e-10';
@ -3599,10 +3592,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);
} else if (str === 'Infinity' || str === 'NaN') {
if (str === 'Infinity' || str === 'NaN') {
if (!+str) x.s = NaN;
x.e = NaN;
x.d = null;
@ -3664,7 +3654,7 @@
if (isFloat) x = divide(x, divisor, len * 4);
// Multiply by the binary exponent part if present.
if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
if (p) x = x.times(Math.abs(p) < 54 ? Math.pow(2, p) : Decimal.pow(2, p));
external = true;
return x;
@ -3680,9 +3670,7 @@
var k,
len = x.d.length;
if (len < 3) {
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
}
if (len < 3) return 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)
@ -3692,7 +3680,8 @@
k = 1.4 * Math.sqrt(len);
k = k > 16 ? 16 : k | 0;
x = x.times(1 / tinyPow(5, k));
// Max k before Math.pow precision loss is 22
x = x.times(Math.pow(5, -k));
x = taylorSeries(Ctor, 2, x, x);
// Reverse argument reduction
@ -3745,14 +3734,6 @@
}
// Exponent e must be positive and non-zero.
function tinyPow(b, e) {
var n = b;
while (--e) n *= b;
return n;
}
// Return the absolute value of `x` reduced to less than or equal to half pi.
function toLessThanHalfPi(Ctor, x) {
var t,
@ -3948,7 +3929,6 @@
* atan2
* cbrt
* ceil
* clamp
* clone
* config
* cos
@ -3974,7 +3954,6 @@
* sinh
* sqrt
* sub
* sum
* tan
* tanh
* trunc
@ -4168,19 +4147,6 @@
}
/*
* Return a new Decimal whose value is `x` clamped to the range delineated by `min` and `max`.
*
* x {number|string|Decimal}
* min {number|string|Decimal}
* max {number|string|Decimal}
*
*/
function clamp(x, min, max) {
return new this(x).clamp(min, max);
}
/*
* Configure global settings for a Decimal constructor.
*
@ -4194,7 +4160,6 @@
* minE {number}
* modulo {number}
* crypto {boolean|number}
* defaults {true}
*
* E.g. Decimal.config({ precision: 20, rounding: 4 })
*
@ -4202,7 +4167,6 @@
function config(obj) {
if (!obj || typeof obj !== 'object') throw Error(decimalError + 'Object expected');
var i, p, v,
useDefaults = obj.defaults === true,
ps = [
'precision', 1, MAX_DIGITS,
'rounding', 0, 8,
@ -4214,15 +4178,13 @@
];
for (i = 0; i < ps.length; i += 3) {
if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
if ((v = obj[p]) !== void 0) {
if ((v = obj[p = ps[i]]) !== void 0) {
if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
else throw Error(invalidArgument + p + ': ' + v);
}
}
if (p = 'crypto', useDefaults) this[p] = DEFAULTS[p];
if ((v = obj[p]) !== void 0) {
if ((v = obj[p = 'crypto']) !== void 0) {
if (v === true || v === false || v === 0 || v === 1) {
if (v) {
if (typeof crypto != 'undefined' && crypto &&
@ -4294,29 +4256,10 @@
x.constructor = Decimal;
// Duplicate.
if (isDecimalInstance(v)) {
if (v instanceof Decimal) {
x.s = v.s;
if (external) {
if (!v.d || v.e > Decimal.maxE) {
// Infinity.
x.e = NaN;
x.d = null;
} else if (v.e < Decimal.minE) {
// Zero.
x.e = 0;
x.d = [0];
} else {
x.e = v.e;
x.d = v.d.slice();
}
} else {
x.e = v.e;
x.d = v.d ? v.d.slice() : v.d;
}
x.e = v.e;
x.d = (v = v.d) ? v.slice() : v;
return;
}
@ -4340,23 +4283,8 @@
// Fast path for small integers.
if (v === ~~v && v < 1e7) {
for (e = 0, i = v; i >= 10; i /= 10) e++;
if (external) {
if (e > Decimal.maxE) {
x.e = NaN;
x.d = null;
} else if (e < Decimal.minE) {
x.e = 0;
x.d = [0];
} else {
x.e = e;
x.d = [v];
}
} else {
x.e = e;
x.d = [v];
}
x.e = e;
x.d = [v];
return;
// Infinity, NaN.
@ -4374,12 +4302,10 @@
}
// Minus sign?
if ((i = v.charCodeAt(0)) === 45) {
if (v.charCodeAt(0) === 45) {
v = v.slice(1);
x.s = -1;
} else {
// Plus sign?
if (i === 43) v = v.slice(1);
x.s = 1;
}
@ -4401,7 +4327,6 @@
Decimal.config = Decimal.set = config;
Decimal.clone = clone;
Decimal.isDecimal = isDecimalInstance;
Decimal.abs = abs;
Decimal.acos = acos;
@ -4414,7 +4339,6 @@
Decimal.atan2 = atan2;
Decimal.cbrt = cbrt; // ES6
Decimal.ceil = ceil;
Decimal.clamp = clamp;
Decimal.cos = cos;
Decimal.cosh = cosh; // ES6
Decimal.div = div;
@ -4437,17 +4361,14 @@
Decimal.sinh = sinh; // ES6
Decimal.sqrt = sqrt;
Decimal.sub = sub;
Decimal.sum = sum;
Decimal.tan = tan;
Decimal.tanh = tanh; // ES6
Decimal.trunc = trunc; // ES6
if (obj === void 0) obj = {};
if (obj) {
if (obj.defaults !== true) {
ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto'];
for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
}
ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto'];
for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
}
Decimal.config(obj);
@ -4498,8 +4419,6 @@
*
* hypot(a, b, ...) = sqrt(a^2 + b^2 + ...)
*
* arguments {number|string|Decimal}
*
*/
function hypot() {
var i, n,
@ -4526,16 +4445,6 @@
}
/*
* Return true if object is a Decimal instance (where Decimal is any Decimal constructor),
* otherwise return false.
*
*/
function isDecimalInstance(obj) {
return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
}
/*
* Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision`
* significant digits using rounding mode `rounding`.
@ -4774,8 +4683,6 @@
* -0 if x is -0,
* NaN otherwise
*
* x {number|string|Decimal}
*
*/
function sign(x) {
x = new this(x);
@ -4832,28 +4739,6 @@
}
/*
* Return a new Decimal whose value is the sum of the arguments, rounded to `precision`
* significant digits using rounding mode `rounding`.
*
* Only the result is rounded, not the intermediate calculations.
*
* arguments {number|string|Decimal}
*
*/
function sum() {
var i = 0,
args = arguments,
x = new this(args[i]);
external = false;
for (; x.s && ++i < args.length;) x = x.plus(args[i]);
external = true;
return finalise(x, this.precision, this.rounding);
}
/*
* Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
* digits using rounding mode `rounding`.
@ -4890,8 +4775,8 @@
// Create and configure initial Decimal constructor.
Decimal = clone(DEFAULTS);
Decimal.prototype.constructor = Decimal;
Decimal = clone(Decimal);
Decimal['default'] = Decimal.Decimal = Decimal;
// Create the internal constants from their string values.
@ -4910,17 +4795,13 @@
// Node and other environments that support module.exports.
} else if (typeof module != 'undefined' && module.exports) {
if (typeof Symbol == 'function' && typeof Symbol.iterator == 'symbol') {
P[Symbol['for']('nodejs.util.inspect.custom')] = P.toString;
P[Symbol.toStringTag] = 'Decimal';
}
module.exports = Decimal;
// Browser.
} else {
if (!globalScope) {
globalScope = typeof self != 'undefined' && self && self.self == self ? self : window;
globalScope = typeof self != 'undefined' && self && self.self == self
? self : Function('return this')();
}
noConflict = globalScope.Decimal;

3
decimal.min.js vendored Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,9 +1,12 @@
/*!
* decimal.js v10.4.3
/*
*
* decimal.js v7.4.0
* An arbitrary-precision Decimal type for JavaScript.
* https://github.com/MikeMcl/decimal.js
* Copyright (c) 2022 Michael Mclaughlin <M8ch88l@gmail.com>
* Copyright (c) 2017 Michael Mclaughlin <M8ch88l@gmail.com>
* MIT Licence
* https://github.com/MikeMcl/decimal.js/LICENCE
*
*/
@ -22,14 +25,14 @@ var EXP_LIMIT = 9e15, // 0 to 9e15
NUMERALS = '0123456789abcdef',
// The natural logarithm of 10 (1025 digits).
LN10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058',
ln10 = '2.3025850929940456840179914546843642076011014886287729760333279009675726096773524802359972050895982983419677840422862486334095254650828067566662873690987816894829072083255546808437998948262331985283935053089653777326288461633662222876982198867465436674744042432743651550489343149393914796194044002221051017141748003688084012647080685567743216228355220114804663715659121373450747856947683463616792101806445070648000277502684916746550586856935673420670581136429224554405758925724208241314695689016758940256776311356919292033376587141660230105703089634572075440370847469940168269282808481184289314848524948644871927809676271275775397027668605952496716674183485704422507197965004714951050492214776567636938662976979522110718264549734772662425709429322582798502585509785265383207606726317164309505995087807523710333101197857547331541421808427543863591778117054309827482385045648019095610299291824318237525357709750539565187697510374970888692180205189339507238539205144634197265287286965110862571492198849978748873771345686209167058',
// Pi (1025 digits).
PI = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789',
pi = '3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632789',
// The initial configuration properties of the Decimal constructor.
DEFAULTS = {
defaults = {
// These values must be integers within the stated ranges (inclusive).
// Most of these values can be changed at run-time using the `Decimal.config` method.
@ -94,14 +97,13 @@ var EXP_LIMIT = 9e15, // 0 to 9e15
// ----------------------------------- END OF EDITABLE DEFAULTS ------------------------------- //
inexact, quadrant,
Decimal, LN10, PI, inexact, quadrant,
external = true,
decimalError = '[DecimalError] ',
invalidArgument = decimalError + 'Invalid argument: ',
precisionLimitExceeded = decimalError + 'Precision limit exceeded',
cryptoUnavailable = decimalError + 'crypto unavailable',
tag = '[object Decimal]',
mathfloor = Math.floor,
mathpow = Math.pow,
@ -115,11 +117,11 @@ var EXP_LIMIT = 9e15, // 0 to 9e15
LOG_BASE = 7,
MAX_SAFE_INTEGER = 9007199254740991,
LN10_PRECISION = LN10.length - 1,
PI_PRECISION = PI.length - 1,
LN10_PRECISION = ln10.length - 1,
PI_PRECISION = pi.length - 1,
// Decimal.prototype object
P = { toStringTag: tag };
P = {};
// Decimal prototype methods
@ -128,7 +130,6 @@ var EXP_LIMIT = 9e15, // 0 to 9e15
/*
* absoluteValue abs
* ceil
* clampedTo clamp
* comparedTo cmp
* cosine cos
* cubeRoot cbrt
@ -210,27 +211,6 @@ P.ceil = function () {
};
/*
* Return a new Decimal whose value is the value of this Decimal clamped to the range
* delineated by `min` and `max`.
*
* min {number|string|Decimal}
* max {number|string|Decimal}
*
*/
P.clampedTo = P.clamp = function (min, max) {
var k,
x = this,
Ctor = x.constructor;
min = new Ctor(min);
max = new Ctor(max);
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);
};
/*
* Return
* 1 if the value of this Decimal is greater than the value of `y`,
@ -336,7 +316,7 @@ P.cubeRoot = P.cbrt = function () {
external = false;
// Initial estimate.
s = x.s * mathpow(x.s * x, 1 / 3);
s = x.s * Math.pow(x.s * x, 1 / 3);
// Math.cbrt underflow/overflow?
// Pass x to Math.pow as integer, then adjust the exponent of the result.
@ -346,7 +326,7 @@ P.cubeRoot = P.cbrt = function () {
// Adjust n exponent so it is a multiple of 3 away from x exponent.
if (s = (e - n.length + 1) % 3) n += (s == 1 || s == -2 ? '0' : '00');
s = mathpow(n, 1 / 3);
s = Math.pow(n, 1 / 3);
// Rarely, e may be one less than the result exponent value.
e = mathfloor((e + 1) / 3) - (e % 3 == (e < 0 ? -1 : 2));
@ -565,7 +545,7 @@ P.hyperbolicCosine = P.cosh = function () {
// TODO? Estimation reused from cosine() and may not be optimal here.
if (len < 32) {
k = Math.ceil(len / 3);
n = (1 / tinyPow(4, k)).toString();
n = Math.pow(4, -k).toString();
} else {
k = 16;
n = '2.3283064365386962890625e-10';
@ -645,7 +625,8 @@ P.hyperbolicSine = P.sinh = function () {
k = 1.4 * Math.sqrt(len);
k = k > 16 ? 16 : k | 0;
x = x.times(1 / tinyPow(5, k));
x = x.times(Math.pow(5, -k));
x = taylorSeries(Ctor, 2, x, x, true);
// Reverse argument reduction
@ -1747,7 +1728,7 @@ P.squareRoot = P.sqrt = function () {
e = mathfloor((e + 1) / 2) - (e < 0 || e % 2);
if (s == 1 / 0) {
n = '5e' + e;
n = '1e' + e;
} else {
n = s.toExponential();
n = n.slice(0, n.indexOf('e') + 1) + e;
@ -2129,9 +2110,16 @@ P.toHexadecimal = P.toHex = function (sd, rm) {
};
/*
* Returns a new Decimal whose value is the nearest multiple of `y` in the direction of rounding
* mode `rm`, or `Decimal.rounding` if `rm` is omitted, to the value of this Decimal.
* Returns a new Decimal whose value is the nearest multiple of the magnitude of `y` to the value
* of this Decimal.
*
* If the value of this Decimal is equidistant from two multiples of `y`, the rounding mode `rm`,
* or `Decimal.rounding` if `rm` is omitted, determines the direction of the nearest multiple.
*
* In the context of this method, rounding mode 4 (ROUND_HALF_UP) is the same as rounding mode 0
* (ROUND_UP), and so on.
*
* The return value will always have the same sign as this Decimal, unless either this Decimal
* or `y` is NaN, in which case the return value will be also be NaN.
@ -2160,11 +2148,7 @@ P.toNearest = function (y, rm) {
rm = Ctor.rounding;
} else {
y = new Ctor(y);
if (rm === void 0) {
rm = Ctor.rounding;
} else {
checkInt32(rm, 0, 8);
}
if (rm !== void 0) checkInt32(rm, 0, 8);
// If x is not finite, return x if y is not NaN, else NaN.
if (!x.d) return y.s ? x : y;
@ -2179,6 +2163,7 @@ P.toNearest = function (y, rm) {
// If y is not zero, calculate the nearest multiple of y to x.
if (y.d[0]) {
external = false;
if (rm < 4) rm = [4, 5, 7, 8][rm];
x = divide(x, y, 0, rm, 1).times(y);
external = true;
finalise(x);
@ -2464,6 +2449,18 @@ P.valueOf = P.toJSON = function () {
};
/*
// Add aliases to match BigDecimal method names.
// P.add = P.plus;
P.subtract = P.minus;
P.multiply = P.times;
P.divide = P.div;
P.remainder = P.mod;
P.compareTo = P.cmp;
P.negate = P.neg;
*/
// Helper functions for Decimal.prototype (P) and/or Decimal methods, and their callers.
@ -2635,18 +2632,16 @@ function convertBase(str, baseIn, baseOut) {
*
*/
function cosine(Ctor, x) {
var k, len, y;
if (x.isZero()) return x;
var k, y,
len = x.d.length;
// Argument reduction: cos(4x) = 8*(cos^4(x) - cos^2(x)) + 1
// i.e. cos(x) = 8*(cos^4(x/4) - cos^2(x/4)) + 1
// Estimate the optimum number of times to use the argument reduction.
len = x.d.length;
if (len < 32) {
k = Math.ceil(len / 3);
y = (1 / tinyPow(4, k)).toString();
y = Math.pow(4, -k).toString();
} else {
k = 16;
y = '2.3283064365386962890625e-10';
@ -3595,10 +3590,7 @@ function parseDecimal(x, str) {
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);
} else if (str === 'Infinity' || str === 'NaN') {
if (str === 'Infinity' || str === 'NaN') {
if (!+str) x.s = NaN;
x.e = NaN;
x.d = null;
@ -3660,7 +3652,7 @@ function parseOther(x, str) {
if (isFloat) x = divide(x, divisor, len * 4);
// Multiply by the binary exponent part if present.
if (p) x = x.times(Math.abs(p) < 54 ? mathpow(2, p) : Decimal.pow(2, p));
if (p) x = x.times(Math.abs(p) < 54 ? Math.pow(2, p) : Decimal.pow(2, p));
external = true;
return x;
@ -3676,9 +3668,7 @@ function sine(Ctor, x) {
var k,
len = x.d.length;
if (len < 3) {
return x.isZero() ? x : taylorSeries(Ctor, 2, x, x);
}
if (len < 3) return 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)
@ -3688,7 +3678,8 @@ function sine(Ctor, x) {
k = 1.4 * Math.sqrt(len);
k = k > 16 ? 16 : k | 0;
x = x.times(1 / tinyPow(5, k));
// Max k before Math.pow precision loss is 22
x = x.times(Math.pow(5, -k));
x = taylorSeries(Ctor, 2, x, x);
// Reverse argument reduction
@ -3741,14 +3732,6 @@ function taylorSeries(Ctor, n, x, y, isHyperbolic) {
}
// Exponent e must be positive and non-zero.
function tinyPow(b, e) {
var n = b;
while (--e) n *= b;
return n;
}
// Return the absolute value of `x` reduced to less than or equal to half pi.
function toLessThanHalfPi(Ctor, x) {
var t,
@ -3944,7 +3927,6 @@ function truncate(arr, len) {
* atan2
* cbrt
* ceil
* clamp
* clone
* config
* cos
@ -3970,7 +3952,6 @@ function truncate(arr, len) {
* sinh
* sqrt
* sub
* sum
* tan
* tanh
* trunc
@ -4164,19 +4145,6 @@ function ceil(x) {
}
/*
* Return a new Decimal whose value is `x` clamped to the range delineated by `min` and `max`.
*
* x {number|string|Decimal}
* min {number|string|Decimal}
* max {number|string|Decimal}
*
*/
function clamp(x, min, max) {
return new this(x).clamp(min, max);
}
/*
* Configure global settings for a Decimal constructor.
*
@ -4190,7 +4158,6 @@ function clamp(x, min, max) {
* minE {number}
* modulo {number}
* crypto {boolean|number}
* defaults {true}
*
* E.g. Decimal.config({ precision: 20, rounding: 4 })
*
@ -4198,7 +4165,6 @@ function clamp(x, min, max) {
function config(obj) {
if (!obj || typeof obj !== 'object') throw Error(decimalError + 'Object expected');
var i, p, v,
useDefaults = obj.defaults === true,
ps = [
'precision', 1, MAX_DIGITS,
'rounding', 0, 8,
@ -4210,15 +4176,13 @@ function config(obj) {
];
for (i = 0; i < ps.length; i += 3) {
if (p = ps[i], useDefaults) this[p] = DEFAULTS[p];
if ((v = obj[p]) !== void 0) {
if ((v = obj[p = ps[i]]) !== void 0) {
if (mathfloor(v) === v && v >= ps[i + 1] && v <= ps[i + 2]) this[p] = v;
else throw Error(invalidArgument + p + ': ' + v);
}
}
if (p = 'crypto', useDefaults) this[p] = DEFAULTS[p];
if ((v = obj[p]) !== void 0) {
if ((v = obj[p = 'crypto']) !== void 0) {
if (v === true || v === false || v === 0 || v === 1) {
if (v) {
if (typeof crypto != 'undefined' && crypto &&
@ -4290,29 +4254,10 @@ function clone(obj) {
x.constructor = Decimal;
// Duplicate.
if (isDecimalInstance(v)) {
if (v instanceof Decimal) {
x.s = v.s;
if (external) {
if (!v.d || v.e > Decimal.maxE) {
// Infinity.
x.e = NaN;
x.d = null;
} else if (v.e < Decimal.minE) {
// Zero.
x.e = 0;
x.d = [0];
} else {
x.e = v.e;
x.d = v.d.slice();
}
} else {
x.e = v.e;
x.d = v.d ? v.d.slice() : v.d;
}
x.e = v.e;
x.d = (v = v.d) ? v.slice() : v;
return;
}
@ -4336,23 +4281,8 @@ function clone(obj) {
// Fast path for small integers.
if (v === ~~v && v < 1e7) {
for (e = 0, i = v; i >= 10; i /= 10) e++;
if (external) {
if (e > Decimal.maxE) {
x.e = NaN;
x.d = null;
} else if (e < Decimal.minE) {
x.e = 0;
x.d = [0];
} else {
x.e = e;
x.d = [v];
}
} else {
x.e = e;
x.d = [v];
}
x.e = e;
x.d = [v];
return;
// Infinity, NaN.
@ -4370,12 +4300,10 @@ function clone(obj) {
}
// Minus sign?
if ((i = v.charCodeAt(0)) === 45) {
if (v.charCodeAt(0) === 45) {
v = v.slice(1);
x.s = -1;
} else {
// Plus sign?
if (i === 43) v = v.slice(1);
x.s = 1;
}
@ -4397,7 +4325,6 @@ function clone(obj) {
Decimal.config = Decimal.set = config;
Decimal.clone = clone;
Decimal.isDecimal = isDecimalInstance;
Decimal.abs = abs;
Decimal.acos = acos;
@ -4410,7 +4337,6 @@ function clone(obj) {
Decimal.atan2 = atan2;
Decimal.cbrt = cbrt; // ES6
Decimal.ceil = ceil;
Decimal.clamp = clamp;
Decimal.cos = cos;
Decimal.cosh = cosh; // ES6
Decimal.div = div;
@ -4433,17 +4359,14 @@ function clone(obj) {
Decimal.sinh = sinh; // ES6
Decimal.sqrt = sqrt;
Decimal.sub = sub;
Decimal.sum = sum;
Decimal.tan = tan;
Decimal.tanh = tanh; // ES6
Decimal.trunc = trunc; // ES6
if (obj === void 0) obj = {};
if (obj) {
if (obj.defaults !== true) {
ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto'];
for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
}
ps = ['precision', 'rounding', 'toExpNeg', 'toExpPos', 'maxE', 'minE', 'modulo', 'crypto'];
for (i = 0; i < ps.length;) if (!obj.hasOwnProperty(p = ps[i++])) obj[p] = this[p];
}
Decimal.config(obj);
@ -4494,8 +4417,6 @@ function floor(x) {
*
* hypot(a, b, ...) = sqrt(a^2 + b^2 + ...)
*
* arguments {number|string|Decimal}
*
*/
function hypot() {
var i, n,
@ -4522,16 +4443,6 @@ function hypot() {
}
/*
* Return true if object is a Decimal instance (where Decimal is any Decimal constructor),
* otherwise return false.
*
*/
function isDecimalInstance(obj) {
return obj instanceof Decimal || obj && obj.toStringTag === tag || false;
}
/*
* Return a new Decimal whose value is the natural logarithm of `x`, rounded to `precision`
* significant digits using rounding mode `rounding`.
@ -4770,8 +4681,6 @@ function round(x) {
* -0 if x is -0,
* NaN otherwise
*
* x {number|string|Decimal}
*
*/
function sign(x) {
x = new this(x);
@ -4828,28 +4737,6 @@ function sub(x, y) {
}
/*
* Return a new Decimal whose value is the sum of the arguments, rounded to `precision`
* significant digits using rounding mode `rounding`.
*
* Only the result is rounded, not the intermediate calculations.
*
* arguments {number|string|Decimal}
*
*/
function sum() {
var i = 0,
args = arguments,
x = new this(args[i]);
external = false;
for (; x.s && ++i < args.length;) x = x.plus(args[i]);
external = true;
return finalise(x, this.precision, this.rounding);
}
/*
* Return a new Decimal whose value is the tangent of `x`, rounded to `precision` significant
* digits using rounding mode `rounding`.
@ -4885,14 +4772,13 @@ function trunc(x) {
}
P[Symbol.for('nodejs.util.inspect.custom')] = P.toString;
P[Symbol.toStringTag] = 'Decimal';
// Create and configure initial Decimal constructor.
export var Decimal = P.constructor = clone(DEFAULTS);
Decimal = clone(defaults);
Decimal['default'] = Decimal.Decimal = Decimal;
// Create the internal constants from their string values.
LN10 = new Decimal(LN10);
PI = new Decimal(PI);
LN10 = new Decimal(ln10);
PI = new Decimal(pi);
export default Decimal;

View File

@ -76,7 +76,6 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#Datan2" >atan2</a></li>
<li><a href="#Dcbrt" >cbrt</a></li>
<li><a href="#Dceil" >ceil</a></li>
<li><a href="#Dclamp" >clamp</a></li>
<li><a href="#Dclone" >clone</a></li>
<li><a href="#Dcos" >cos</a></li>
<li><a href="#Dcosh" >cosh</a></li>
@ -84,7 +83,6 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#Dexp" >exp</a></li>
<li><a href="#Dfloor" >floor</a></li>
<li><a href="#Dhypot" >hypot</a></li>
<li><a href="#DisDecimal" >isDecimal</a></li>
<li><a href="#Dln" >ln</a></li>
<li><a href="#Dlog" >log</a></li>
<li><a href="#Dlog2" >log2</a></li>
@ -103,7 +101,6 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#Dsinh" >sinh</a></li>
<li><a href="#Dsqrt" >sqrt</a></li>
<li><a href="#Dsub" >sub</a></li>
<li><a href="#Dsum" >sum</a></li>
<li><a href="#Dtan" >tan</a></li>
<li><a href="#Dtanh" >tanh</a></li>
<li><a href="#Dtrunc" >trunc</a></li>
@ -139,7 +136,6 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<li><a href="#abs" >absoluteValue </a><span>abs</span> </li>
<li><a href="#ceil" >ceil </a> </li>
<li><a href="#cmp" >comparedTo </a><span>cmp</span> </li>
<li><a href="#clamp" >clampedTo </a><span>clamp</span> </li>
<li><a href="#cos" >cosine </a><span>cos</span> </li>
<li><a href="#cbrt" >cubeRoot </a><span>cbrt</span> </li>
<li><a href="#dp" >decimalPlaces </a><span>dp</span> </li>
@ -199,9 +195,9 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
<a href="#instance-properties">Properties</a>
<ul>
<li><a href="#digits" >d</a><span>digits</span></li>
<li><a href="#exponent" >e</a><span>exponent</span></li>
<li><a href="#sign" >s</a><span>sign</span></li>
<li><a href="#digits" >d</a><span>digits</span></li>
<li><a href="#exponent">e</a><span>exponent</span></li>
<li><a href="#sign" >s</a><span>sign</span></li>
</ul>
<a href="#zero-nan-infinity">Zero, NaN &amp; Infinity</a>
@ -301,7 +297,6 @@ new Decimal('0xff.8') // '255.5'
new Decimal(0.046875) // '0.046875'
new Decimal('0.046875000000') // '0.046875'
new Decimal('0.046_875_000_000') // '0.046875'
new Decimal(4.6875e-2) // '0.046875'
new Decimal('468.75e-4') // '0.046875'
@ -440,16 +435,6 @@ 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 />
<code>max</code>: <i>number|string|Decimal</i>
</p>
<p>See <code><a href='#clamp'>clampedTo</a></code>.</p>
<pre>Decimal.clamp(10.1, 0, 10) // '10'</pre>
<h5 id="Dclone">
clone
<code class='inset'>.clone([object]) <i>&rArr; Decimal constructor</i></code>
@ -461,32 +446,24 @@ a.equals(b) // true</pre>
settings as <code>this</code> Decimal constructor if <code>object</code> is omitted.
</p>
<pre>Decimal.set({ precision: 5 })
Decimal9 = Decimal.clone({ precision: 9 })
D9 = Decimal.clone({ precision: 9 })
a = new Decimal(1)
b = new Decimal9(1)
b = new D9(1)
a.div(3) // 0.33333
b.div(3) // 0.333333333
// Decimal9 = Decimal.clone({ precision: 9 }) is equivalent to:
Decimal9 = Decimal.clone()
Decimal9.set({ precision: 9 })</pre>
<p>
If <code>object</code> has a <code>'defaults'</code> property with value <code>true</code>
then the new constructor will use the default configuration.
</p>
<pre>
D1 = Decimal.clone({ defaults: true })
// Use the defaults except for precision
D2 = Decimal.clone({ defaults: true, precision: 50 })</pre>
// D9 = Decimal.clone({ precision: 9 }) is equivalent to:
D9 = Decimal.clone()
D9.set({ precision: 9 })</pre>
<p>
It is not inefficient in terms of memory usage to use multiple Decimal constructors as
functions are shared between them.
</p>
<h5 id="Dcos">cos<code class='inset'>.cos(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#cos'>cosine</a></code>.</p>
@ -560,22 +537,6 @@ a.equals(b) // true</pre>
<h5 id="DisDecimal">
isDecimal<code class='inset'>.isDecimal(object) <i>&rArr; boolean</i></code>
</h5>
<p><code>object</code>: <i>any</i></p>
<p>
Returns <code>true</code> if <code>object</code> is a Decimal instance (where Decimal is any
Decimal constructor), or <code>false</code> if it is not.
</p>
<pre>a = new Decimal(1)
b = {}
a instanceof Decimal // true
Decimal.isDecimal(a) // true
Decimal.isDecimal(b) // false</pre>
<h5 id="Dlog">log<code class='inset'>.log(x [, base]) <i>&rArr; Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
@ -615,7 +576,7 @@ a.equals(b) // true</pre>
<h5 id="Dmax">
max<code class='inset'>.max(x [, y, ...]) <i>&rArr; Decimal</i></code>
max<code class='inset'>.max([x [, y, ...]]) <i>&rArr; Decimal</i></code>
</h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
@ -627,7 +588,7 @@ a.equals(b) // true</pre>
<h5 id="Dmin">
min<code class='inset'>.min(x [, y, ...]) <i>&rArr; Decimal</i></code>
min<code class='inset'>.min([x [, y, ...]]) <i>&rArr; Decimal</i></code>
</h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
@ -759,10 +720,6 @@ a.equals(b) // true</pre>
The values of the configuration object properties are checked for validity and then stored as
equivalently-named properties of <code>this</code> Decimal constructor.
</p>
<p>
If <code>object</code> has a <code>'defaults'</code> property with value <code>true</code>
then any unspecified properties will be reset to their default values.
</p>
<p>Throws on an invalid <code>object</code> or configuration property value.</p>
<pre>
// Defaults
@ -775,13 +732,7 @@ Decimal.set({
minE: -9e15,
modulo: 1,
crypto: false
})
// Reset all properties to their default values
Decimal.set({ defaults: true })
// Set precision to 50 and all other properties to their default values
Decimal.set({ precision: 50, defaults: true })</pre>
})</pre>
<p>
The properties of a Decimal constructor can also be set by direct assignment, but that will
by-pass the validity checking that this method performs - this is not a problem if the user
@ -847,7 +798,7 @@ a.equals(b) // true</pre>
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5>
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>&rArr; Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
@ -859,26 +810,7 @@ a.equals(b) // true</pre>
<h5 id="Dsum">sum<code class='inset'>.sum(x [, y, ...]) <i>&rArr; Decimal</i></code></h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>y</code>: <i>number|string|Decimal</i>
</p>
<p>
Returns a new Decimal whose value is the sum of the <code>arguments</code>,
rounded to <a href='#precision'><code>precision</code></a> significant digits using
rounding mode <a href='#rounding'><code>rounding</code></a>.<br />
Only the result is rounded, not the intermediate summations.
</p>
<pre>
x = 5
y = '16'
z = new Decimal(-11)
Decimal.sum(x, y, z) // '10'</pre>
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; Decimal</i></code></h5>
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>&rArr; Decimal</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<p>See <code><a href='#tan'>tangent</a></code>.</p>
<pre>a = Decimal.tan(x)
@ -943,18 +875,12 @@ 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='#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='#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='#toDP'><code>toDecimalPlaces</code></a>,
<a href='#toNearest'><code>toNearest</code></a> and
<a href='#trunc'><code>truncated</code></a>.
</p>
<p>
See <code><a href='#Pi'>Pi</a></code> for the precision limit of the trigonometric methods.
</p>
<pre>Decimal.set({ precision: 5 })
Decimal.precision // 5</pre>
@ -1009,7 +935,7 @@ new Decimal('1e-500') // '1e-500'
new Decimal('9.9e-501') // '0'
Decimal.set({ minE: -3 })
new Decimal(0.001) // '0.001' e is -3
new Decimal(0.001) // '0.01' e is -3
new Decimal(0.0001) // '0' e is -4</pre>
<p>
The smallest possible magnitude of a non-zero Decimal is <code>1e-9000000000000000</code>
@ -1280,28 +1206,6 @@ y.ceil() // '-1'</pre>
<h5 id="clamp">clampedTo<code class='inset'>.clamp(min, max) <i>&rArr; Decimal</i></code></h5>
<p>
<code>min</code>: <i>number|string|Decimal</i><br />
<code>max</code>: <i>number|string|Decimal</i>
</p>
<p>
Returns a new Decimal whose value is the value of this Decimal clamped to the range
delineated by <code>min</code> and <code>max</code>.
</p>
<p>
The return value is not affected by the value of the
<a href='#precision'><code>precision</code></a> setting.
</p>
<pre>
x = new Decimal(5)
min = new Decimal(100)
max = new Decimal(Infinity)
x.clampedTo(min, max) // '100'
x.clamp(-10, -0.1) // '-0.1'</pre>
<h5 id="cmp">comparedTo<code class='inset'>.cmp(x) <i>&rArr; number</i></code></h5>
<p><code>x</code>: <i>number|string|Decimal</i></p>
<table>
@ -1342,7 +1246,7 @@ y.cmp(NaN) // NaN</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-1, 1</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.25)
x.cosine() // '0.96891242171064478414'
@ -1492,7 +1396,7 @@ new Decimal(1).gte(x) // true</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>1, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(1)
x.hyperbolicCosine() // '1.5430806348152437785'
@ -1511,7 +1415,7 @@ y.cosh() // '1.1276259652063807852'</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(1)
x.hyperbolicSine() // '1.1752011936438014569'
@ -1530,7 +1434,7 @@ y.sinh() // '0.52109530549374736162'</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-1, 1</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(1)
x.hyperbolicTangent() // '0.76159415595576488812'
@ -1549,7 +1453,7 @@ y.tanh() // '0.4621171572600097585'</pre>
Domain: [<code>-1, 1</code>]<br />
Range: [<code>0, pi</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0)
x.inverseCosine() // '1.5707963267948966192'
@ -1570,7 +1474,7 @@ y.acos() // '1.0471975511965977462'</pre>
Domain: [<code>1, Infinity</code>]<br />
Range: [<code>0, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(5)
x.inverseHyperbolicCosine() // '2.2924316695611776878'
@ -1591,7 +1495,7 @@ y.acosh() // '4.6050701709847571595'</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(5)
x.inverseHyperbolicSine() // '2.3124383412727526203'
@ -1612,7 +1516,7 @@ y.asinh() // '4.6052701709914238266'</pre>
Domain: [<code>-1, 1</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.inverseHyperbolicTangent() // '0.5493061443340548457'
@ -1631,7 +1535,7 @@ y.atanh() // '0.97295507452765665255'</pre>
Domain: [<code>-1, 1</code>]<br />
Range: [<code>-pi/2, pi/2</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.inverseSine() // '0.52359877559829887308'
@ -1650,7 +1554,7 @@ y.asin() // '0.84806207898148100805'</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-pi/2, pi/2</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.inverseTangent() // '0.46364760900080611621'
@ -1671,6 +1575,10 @@ 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>
@ -1697,6 +1605,7 @@ 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>
@ -1710,14 +1619,7 @@ x = new Decimal(-0)
x.isNegative() // true
y = new Decimal(2)
y.isNeg // false</pre>
<p>Note that zero is signed.</p>
<pre>
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>
<p>Note: <code>n &lt; 0</code> can be used if <code>n &lt;= -Number.MIN_VALUE</code>.</p>
@ -1731,6 +1633,7 @@ 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>
@ -1744,6 +1647,7 @@ 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>
@ -1802,8 +1706,7 @@ y.log(2) // '8'</pre>
<p>Logarithms to base <code>2</code> or <code>10</code> will always be correctly rounded.</p>
<p>
See <a href='#pow'><code>toPower</code></a> for the circumstances in which this method may
return an incorrectly rounded result, and see <a href='#ln'><code>naturalLogarithm</code></a>
for the precision limit.
return an incorrectly rounded result.
</p>
<p>The performance of this method degrades exponentially with increasing digits.</p>
@ -1990,7 +1893,7 @@ x // '1234.5'</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-1, 1</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.sine() // '0.47942553860420300027'
@ -2032,7 +1935,7 @@ y.sqrt().eq( y.pow(0.5) ) // true</pre>
Domain: [<code>-Infinity, Infinity</code>]<br />
Range: [<code>-Infinity, Infinity</code>]
</p>
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
<pre>
x = new Decimal(0.5)
x.tangent() // '0.54630248984379051326'
@ -2106,9 +2009,9 @@ x.toBinary(1) // '0b1p+8'</pre>
</p>
<p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
<pre>
x = new Decimal(12.34567)
x.toDecimalPlaces(0) // '12'
x.toDecimalPlaces(1, Decimal.ROUND_UP) // '12.4'
x = new Decimal(12.24567)
x.toDecimalPlaces(0) // '12'
x.toDecimalPlaces(1, 0) // '12.3'
y = new Decimal(9876.54321)
y.toDP(3) // '9876.543'
@ -2144,16 +2047,16 @@ y.toDP(1, Decimal.ROUND_DOWN) // '9876.5'</pre>
<p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
<pre>
x = 45.6
y = new Decimal(x)
x.toExponential() // '4.56e+1'
y.toExponential() // '4.56e+1'
x.toExponential(0) // '5e+1'
y.toExponential(0) // '5e+1'
x.toExponential(1) // '4.6e+1'
y.toExponential(1) // '4.6e+1'
y.toExponential(1, Decimal.ROUND_DOWN) // '4.5e+1'
x.toExponential(3) // '4.560e+1'
y.toExponential(3) // '4.560e+1'</pre>
b = new Decimal(x)
x.toExponential() // '4.56e+1'
y.toExponential() // '4.56e+1'
x.toExponential(0) // '5e+1'
y.toExponential(0) // '5e+1'
x.toExponential(1) // '4.6e+1'
y.toExponential(1) // '4.6e+1'
y.toExponential(1, 1) // '4.5e+1' (ROUND_DOWN)
x.toExponential(3) // '4.560e+1'
y.toExponential(3) // '4.560e+1'</pre>
@ -2192,15 +2095,15 @@ y.toExponential(3) // '4.560e+1'</pre>
<p>Throws on an invalid <code>dp</code> or <code>rm</code> value.</p>
<pre>
x = 3.456
y = new Decimal(x)
x.toFixed() // '3'
y.toFixed() // '3.456'
y.toFixed(0) // '3'
x.toFixed(2) // '3.46'
y.toFixed(2) // '3.46'
y.toFixed(2, Decimal.ROUND_DOWN) // '3.45'
x.toFixed(5) // '3.45600'
y.toFixed(5) // '3.45600'</pre>
b = new Decimal(x)
x.toFixed() // '3'
y.toFixed() // '3.456'
y.toFixed(0) // '3'
x.toFixed(2) // '3.46'
y.toFixed(2) // '3.46'
y.toFixed(2, 1) // '3.45' (ROUND_DOWN)
x.toFixed(5) // '3.45600'
y.toFixed(5) // '3.45600'</pre>
@ -2272,16 +2175,25 @@ x.toHex(1) // '0x1p+8'</pre>
<h5 id="toNearest">
toNearest<code class='inset'>.toNearest(x [, rm]) <i>&rArr; Decimal</i></code>
toNearest<code class='inset'>.toNearest(n [, rm]) <i>&rArr; Decimal</i></code>
</h5>
<p>
<code>x</code>: <i>number|string|Decimal</i><br />
<code>rm</code>: <i>number</i>: integer, <code>0</code> to <code>8</code> inclusive
</p>
<p>
Returns a new Decimal whose value is the nearest multiple of <code>x</code> in the direction
of rounding mode <code>rm</code>, or <a href='#rounding'><code>rounding</code></a> if
<code>rm</code> is omitted, to the value of this Decimal.
Returns a new Decimal whose value is the nearest multiple of <code>x</code> to the value of
this Decimal.
</p>
<p>
If the value of this Decimal is equidistant from two multiples of <code>x</code>, the rounding
mode <code>rm</code>, or <a href='#rounding'><code>rounding</code></a> if <code>rm</code> is
omitted, determines the direction of the nearest.
</p>
<p>
In this context, rounding mode <a href='#rounding'><code>ROUND_HALF_UP</code></a> is
interpreted the same as rounding mode <a href='#rounding'><code>ROUND_UP</code></a>, and so
on, i.e. the rounding is either up, down, to ceil, to floor or to even.
</p>
<p>
The return value will always have the same sign as this Decimal, unless either this Decimal
@ -2294,11 +2206,11 @@ x.toHex(1) // '0x1p+8'</pre>
</p>
<pre>
x = new Decimal(1.39)
x.toNearest(0.25) // '1.5'
x.toNearest(0.25) // '1.5'
y = new Decimal(9.499)
y.toNearest(0.5, Decimal.ROUND_UP) // '9.5'
y.toNearest(0.5, Decimal.ROUND_DOWN) // '9'</pre>
y = new Decimal(0.75) // equidistant from 0.5 and 1
y.toNearest(0.5, 0) // '1' (ROUND_UP)
y.toNearest(0.5, 1) // '0.5' (ROUND_DOWN)</pre>
@ -2441,13 +2353,13 @@ new Decimal(28).pow('6.166675020000903537297764507632802193308677149')
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
<pre>
x = 45.6
y = new Decimal(x)
b = new Decimal(x)
x.toPrecision() // '45.6'
y.toPrecision() // '45.6'
x.toPrecision(1) // '5e+1'
y.toPrecision(1) // '5e+1'
y.toPrecision(2, Decimal.ROUND_UP) // '46'
y.toPrecision(2, Decimal.ROUND_DOWN) // '45'
y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP)
y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN)
x.toPrecision(5) // '45.600'
y.toPrecision(5) // '45.600'</pre>
@ -2566,9 +2478,9 @@ x.valueOf() // '-0'</pre>
<td><code>-1</code>, <code>1</code>, or <code>NaN</code></td>
</tr>
</table>
<p>All the properties are best considered to be read-only.</p>
<p>The properties are best considered to be read-only.</p>
<p>
As with JavaScript numbers, the original exponent and fractional trailing zeros of a value
As with JavaScript numbers, the original exponent and fractional trailing zeros of a number
are not preserved.
</p>
<pre>
@ -2656,27 +2568,13 @@ try {
</p>
<p>
It has a precision of <code>1025</code> digits, meaning that the trigonometric methods
can calculate up to just over <code>1000</code> digits, but the actual figure depends on the
precision of the argument passed to them. To calculate the actual figure use:
</p>
<p><b>maximum_result_precision = 1000 - argument_precision</b></p>
For example, the following both work fine:
<pre>
Decimal.set({precision: 991}).tan(123456789)
Decimal.set({precision: 9}).tan(991_digit_number)</pre>
<p>
as, for each, the result precision plus the argument precision, i.e. <code>991 + 9</code> and
<code>9 + 991</code>, is less than or equal to <code>1000</code>.
can accurately calculate up to at least <code>1000</code> digits.
</p>
<p>
If greater precision is required then the value of <code>PI</code> will need to be extended to
about <code>25</code> digits more than the precision required. The time taken by the methods
will then be the limiting factor.
</p>
<p>
The value can also be shortened to reduce the size of the source file if such high precision
is not required.
about <code>25</code> digits more than the precision required.
</p>
<p>The value can also be shortened to reduce the size of the source file.</p>
<p>To get the value of pi:</p>
<pre>
pi = Decimal.acos(-1)</pre>

1
doc/decimal.js.map Normal file

File diff suppressed because one or more lines are too long

View File

@ -1,7 +1,7 @@
{
"name": "decimal.js",
"description": "An arbitrary-precision Decimal type for JavaScript.",
"version": "10.4.3",
"version": "7.4.0",
"keywords": [
"arbitrary",
"precision",
@ -20,36 +20,16 @@
"type": "git",
"url": "https://github.com/MikeMcl/decimal.js.git"
},
"main": "decimal",
"main": "decimal.js",
"module": "decimal.mjs",
"browser": "decimal.js",
"exports": {
".": {
"types": "./decimal.d.ts",
"import": "./decimal.mjs",
"require": "./decimal.js"
},
"./decimal.mjs": "./decimal.mjs",
"./decimal.js": "./decimal.js",
"./package.json": "./package.json",
"./decimal": {
"types": "./decimal.d.ts",
"import": "./decimal.mjs",
"require": "./decimal.js"
}
},
"author": {
"name": "Michael Mclaughlin",
"email": "M8ch88l@gmail.com"
},
"license": "MIT",
"scripts": {
"test": "node ./test/test.js"
"test": "node ./test/test.js",
"build": "uglifyjs decimal.js --source-map doc/decimal.js.map -c -m -o decimal.min.js --preamble \"/* decimal.js v7.4.0 https://github.com/MikeMcl/decimal.js/LICENCE */\""
},
"types": "decimal.d.ts",
"files": [
"decimal.js",
"decimal.mjs",
"decimal.d.ts"
]
"types": "decimal.d.ts"
}

View File

@ -124,7 +124,6 @@ T('Decimal', function () {
t('0', '0b0');
t('0', '0B0');
t('-5', '-0b101');
t('5', '+0b101');
t('1.5', '0b1.1');
t('-1.5', '-0b1.1');
@ -140,7 +139,6 @@ T('Decimal', function () {
// Octal.
t('8', '0o10');
t('-8.5', '-0O010.4');
t('8.5', '+0O010.4');
t('-262144.000000059604644775390625', '-0o1000000.00000001');
t('572315667420.390625', '0o10250053005734.31');
@ -148,7 +146,6 @@ T('Decimal', function () {
t('1', '0x00001');
t('255', '0xff');
t('-15.5', '-0Xf.8');
t('15.5', '+0Xf.8');
t('-16777216.00000000023283064365386962890625', '-0x1000000.00000001');
t('325927753012307620476767402981591827744994693483231017778102969592507', '0xc16de7aa5bf90c3755ef4dea45e982b351b6e00cd25a82dcfe0646abb');
@ -159,18 +156,14 @@ T('Decimal', function () {
}
t('NaN', NaN);
t('NaN', -NaN);
t('NaN', 'NaN');
t('NaN', '-NaN');
t('NaN', '+NaN');
tx(function () {new Decimal(' NaN')}, "' NaN'");
tx(function () {new Decimal('NaN ')}, "'NaN '");
tx(function () {new Decimal(' NaN ')}, "' NaN '");
tx(function () {new Decimal(' -NaN')}, "' -NaN'");
tx(function () {new Decimal('+NaN')}, "'+NaN'");
tx(function () {new Decimal(' +NaN')}, "' +NaN'");
tx(function () {new Decimal('-NaN ')}, "'-NaN '");
tx(function () {new Decimal('+NaN ')}, "'+NaN '");
tx(function () {new Decimal('.NaN')}, "'.NaN'");
tx(function () {new Decimal('NaN.')}, "'NaN.'");
@ -178,12 +171,11 @@ T('Decimal', function () {
t('-Infinity', -Infinity);
t('Infinity', 'Infinity');
t('-Infinity', '-Infinity');
t('Infinity', '+Infinity');
tx(function () {new Decimal(' Infinity')}, "' Infinity '");
tx(function () {new Decimal('Infinity ')}, "'Infinity '");
tx(function () {new Decimal(' Infinity ')}, "' Infinity '");
tx(function () {new Decimal(' -Infinity')}, "' -Infinity'");
tx(function () {new Decimal('+Infinity')}, "'+Infinity'");
tx(function () {new Decimal(' +Infinity')}, "' +Infinity'");
tx(function () {new Decimal('.Infinity')}, "'.Infinity'");
tx(function () {new Decimal('Infinity.')}, "'Infinity.'");
@ -205,7 +197,7 @@ T('Decimal', function () {
tx(function () {new Decimal('0-')}, "'0-'");
tx(function () {new Decimal(' -0')}, "' -0'");
tx(function () {new Decimal('-0 ')}, "'-0 '");
tx(function () {new Decimal('+0 ')}, "'+0 '");
tx(function () {new Decimal('+0')}, "'+0'");
tx(function () {new Decimal(' +0')}, "' +0'");
tx(function () {new Decimal(' .0')}, "' .0'");
tx(function () {new Decimal('0. ')}, "'0. '");
@ -230,16 +222,12 @@ T('Decimal', function () {
t('0.1', '.1');
t('0.1', '.1');
t('-0.1', '-.1');
t('0.1', '+.1');
t('1', '1.');
t('1', '1.0');
t('-1', '-1.');
t('1', '+1.');
t('-1', '-1.0000');
t('1', '1.0000');
t('1', '1.00000000');
t('-1', '-1.000000000000000000000000');
t('1', '+1.000000000000000000000000');
tx(function () {new Decimal(' 1')}, "' 1'");
tx(function () {new Decimal('1 ')}, "'1 '");
@ -247,8 +235,8 @@ T('Decimal', function () {
tx(function () {new Decimal('1-')}, "'1-'");
tx(function () {new Decimal(' -1')}, "' -1'");
tx(function () {new Decimal('-1 ')}, "'-1 '");
tx(function () {new Decimal('+1')}, "'+1'");
tx(function () {new Decimal(' +1')}, "' +1'");
tx(function () {new Decimal('+1 ')}, "'+1'");
tx(function () {new Decimal('.1.')}, "'.1.'");
tx(function () {new Decimal('+-1')}, "'+-1'");
tx(function () {new Decimal('-+1')}, "'-+1'");
@ -274,9 +262,8 @@ T('Decimal', function () {
t('123.456789', 123.456789);
t('-123.456789', -123.456789);
t('-123.456789', '-123.456789');
t('123.456789', '123.456789');
t('123.456789', '+123.456789');
t('-123.456789', '-123.456789');
tx(function () {new Decimal(void 0)}, "void 0");
tx(function () {new Decimal('undefined')}, "'undefined'");
@ -295,9 +282,7 @@ T('Decimal', function () {
tx(function () {new Decimal('ff')}, "'ff'");
tx(function () {new Decimal('0xg')}, "'oxg'");
tx(function () {new Decimal('0Xfi')}, "'0Xfi'");
tx(function () {new Decimal('++45')}, "'++45'");
tx(function () {new Decimal('--45')}, "'--45'");
tx(function () {new Decimal('9.99--')}, "'9.99--'");
tx(function () {new Decimal('9.99++')}, "'9.99++'");
tx(function () {new Decimal('0 0')}, "'0 0'");
});

View File

@ -1,45 +0,0 @@
if (typeof T === 'undefined') require('../setup');
T('clamp', function () {
function t(x, min, max, expected) {
//T.assertEqual(expected, new Decimal(x).clampedTo(min, max).valueOf());
T.assertEqual(expected, new Decimal(x).clamp(min, max).valueOf());
//T.assertEqual(expected, Decimal.clamp(x, min, max).valueOf());
}
t('-0', '0', '0', '-0');
t('-0', '-0', '0', '-0');
t('-0', '0', '-0', '-0');
t('-0', '-0', '-0', '-0');
t('0', '0', '0', '0');
t('0', '-0', '0', '0');
t('0', '0', '-0', '0');
t('0', '-0', '-0', '0');
t(0, 0, 1, '0');
t(-1, 0, 1, '0');
t(-2, 0, 1, '0');
t(1, 0, 1, '1');
t(2, 0, 1, '1');
t(1, 1, 1, '1');
t(-1, 1, 1, '1');
t(-1, -1, 1, '-1');
t(2, 1, 2, '2');
t(3, 1, 2, '2');
t(1, 0, 1, '1');
t(2, 0, 1, '1');
t(Infinity, 0, 1, '1');
t(0, -Infinity, 0, '0');
t(-Infinity, 0, 1, '0');
t(-Infinity, -Infinity, Infinity, '-Infinity');
t(Infinity, -Infinity, Infinity, 'Infinity');
t(0, 1, Infinity, '1');
t(0, NaN, 1, 'NaN');
t(0, 0, NaN, 'NaN');
t(NaN, 0, 1, 'NaN');
});

View File

@ -105,40 +105,5 @@ T('clone', function () {
t(new D8(1).constructor !== new D9(1).constructor);
T.assertException(function () { Decimal.clone(null) }, "Decimal.clone(null)");
// defaults: true
Decimal.config({
precision: 100,
rounding: 2,
toExpNeg: -100,
toExpPos: 200,
defaults: true
});
t(Decimal.precision === 100);
t(Decimal.rounding === 2);
t(Decimal.toExpNeg === -100);
t(Decimal.toExpPos === 200);
t(Decimal.defaults === undefined);
D1 = Decimal.clone({ defaults: true });
t(D1.precision === 20);
t(D1.rounding === 4);
t(D1.toExpNeg === -7);
t(D1.toExpPos === 21);
t(D1.defaults === undefined);
D2 = Decimal.clone({ defaults: true, rounding: 5 });
t(D2.precision === 20);
t(D2.rounding === 5);
t(D2.toExpNeg === -7);
t(D2.toExpPos === 21);
D3 = Decimal.clone({ defaults: false });
t(D3.rounding === 2);
});

View File

@ -338,36 +338,5 @@ T('config', function () {
t(9, {modulo: void 0});
// defaults
t = function (actual) {
T.assert(actual);
}
Decimal.config({
precision: 100,
rounding: 2,
toExpNeg: -100,
toExpPos: 200,
});
t(Decimal.precision === 100);
Decimal.config({ defaults: true });
t(Decimal.precision === 20);
t(Decimal.rounding === 4);
t(Decimal.toExpNeg === -7);
t(Decimal.toExpPos === 21);
t(Decimal.defaults === undefined);
Decimal.rounding = 3;
Decimal.config({ precision: 50, defaults: true });
t(Decimal.precision === 50);
t(Decimal.rounding === 4);
// Decimal.set is an alias for Decimal.config
T.assertEqual(Decimal.set, Decimal.config);
});

View File

@ -1,6 +1,6 @@
if (typeof T === 'undefined') require('../setup');
T('isFinite, isInteger, isNaN, isNegative, isZero, isDecimal', function () {
T('isFinite, isInteger, isNaN, isNegative, isZero', function () {
function t(actual) {
T.assert(actual);
@ -257,22 +257,4 @@ T('isFinite, isInteger, isNaN, isNegative, isZero, isDecimal', function () {
t(!new Decimal('0.999999999999999999999').isInteger());
t(new Decimal('4e4').isInteger());
t(new Decimal('-4e4').isInteger());
// Decimal.isDecimal
t(Decimal.isDecimal(new Decimal(1)));
t(Decimal.isDecimal(new Decimal('-2.3')));
t(Decimal.isDecimal(new Decimal(NaN)));
t(Decimal.isDecimal(new Decimal('Infinity')));
t(!Decimal.isDecimal());
t(!Decimal.isDecimal(0));
t(!Decimal.isDecimal(1));
t(!Decimal.isDecimal('-2.3'));
t(!Decimal.isDecimal(NaN));
t(!Decimal.isDecimal(Infinity));
t(!Decimal.isDecimal(undefined));
t(!Decimal.isDecimal({}));
t(!Decimal.isDecimal({isDecimal: true}));
t(!Decimal.isDecimal(new Number(4)));
});

View File

@ -1,64 +0,0 @@
if (typeof T === 'undefined') require('../setup');
T('sum', function () {
var expected;
function t() {
T.assertEqualDecimal(expected, Decimal.sum.apply(Decimal, arguments));
}
expected = new Decimal(0);
t('0');
t('0', new Decimal(0));
t(1, 0, '-1');
t(0, new Decimal('-10'), 0, 0, 0, 0, 0, 10);
t(11, -11);
t(1, '2', new Decimal(3), new Decimal('4'), -10);
t(new Decimal(-10), '9', new Decimal(0.01), 0.99);
expected = new Decimal(10);
t('10');
t('0', new Decimal('10'));
t(10, 0);
t(0, 0, 0, 0, 0, 0, 10);
t(11, -1);
t(1, '2', new Decimal(3), new Decimal('4'));
t('9', new Decimal(0.01), 0.99);
expected = new Decimal(600);
t(100, 200, 300);
t('100', '200', '300');
t(new Decimal(100), new Decimal(200), new Decimal(300));
t(100, '200', new Decimal(300));
t(99.9, 200.05, 300.05);
expected = new Decimal(NaN);
t(NaN);
t('1', NaN);
t(100, 200, NaN);
t(NaN, 0, '9', new Decimal(0), 11, Infinity);
t(0, new Decimal('-Infinity'), '9', new Decimal(NaN), 11);
t(4, '-Infinity', 0, '9', new Decimal(0), Infinity, 2);
expected = new Decimal(Infinity);
t(Infinity);
t(1, '1e10000000000000000000000000000000000000000', '4');
t(100, 200, 'Infinity');
t(0, new Decimal('Infinity'), '9', new Decimal(0), 11);
t(0, '9', new Decimal(0), 11, Infinity);
t(4, new Decimal(Infinity), 0, '9', new Decimal(0), Infinity, 2);
expected = new Decimal(-Infinity);
t(-Infinity);
t(1, '-1e10000000000000000000000000000000000000000', '4');
t(100, 200, '-Infinity');
t(0, new Decimal('-Infinity'), '9', new Decimal(0), 11);
t(0, '9', new Decimal(0), 11, -Infinity);
t(4, new Decimal(-Infinity), 0, '9', new Decimal(0), -Infinity, 2);
});

View File

@ -27,7 +27,8 @@ T('toNearest', function () {
t(!isMinusZero(new Decimal(1).toNearest(-3)));
t( isMinusZero(new Decimal(-1).toNearest(-3)));
t = function (expected, n, v, rm) {
t = function (expected, n, v, sd, rm) {
if (sd) Decimal.precision = sd;
T.assertEqual(expected, new Decimal(n).toNearest(v, rm).valueOf());
}
@ -66,9 +67,9 @@ T('toNearest', function () {
t('0', 1, -3);
t('-0', -1, -3);
t('3', 1.5, -3, 0);
t('-0', -1.5, -3, 1);
t('-3', -1.5, -3, 2);
t('3', 1.5, -3, 20, 0);
t('-0', -1.5, -3, 20, 1);
t('-3', -1.5, -3, 20, 2);
t('123', 123.456);
t('123', 123.456, 1);
@ -86,145 +87,68 @@ T('toNearest', function () {
t('123.46', 123.456, '-0.02');
t('123.456', 123.456, '-0.002');
t('83105511540', '83105511539.5', 1, 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 4);
t('83105511539', '83105511539.5', '1', 5);
t('83105511540', '83105511539.5000000000000000000001', 1, 5);
t('83105511540', '83105511539.5', 1, 11, 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 11, 4);
t('83105511539', '83105511539.5', '1', 11, 5);
t('83105511540', '83105511539.5000000000000000000001', 1, 11, 5);
Decimal.precision = 3;
t('83105511540', '83105511539.5', new Decimal(1), 3, 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 3, 4);
t('83105511539', '83105511539.5', new Decimal('1'), 3, 5);
t('83105511540', '83105511539.5000000000000000000001', 1, 3, 5);
t('83105511540', '83105511539.5', new Decimal(1), 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 4);
t('83105511539', '83105511539.5', new Decimal('1'), 5);
t('83105511540', '83105511539.5000000000000000000001', 1, 5);
t('83105511540', '83105511539.5', Decimal.ONE, 30, 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 4);
t('83105511539', '83105511539.5', 1, 30, 5);
t('83105511540', '83105511539.5000000000000000000001', 1, 30, 5);
Decimal.precision = 20;
t('83105511540', '83105511539.5', -1, 11, 4);
t('83105511539', '83105511539.499999999999999999999999999999', -1, 11, 4);
t('83105511539', '83105511539.5', '-1', 11, 5);
t('83105511540', '83105511539.5000000000000000000001', -1, 11, 5);
t('83105511540', '83105511539.5', -1, 4);
t('83105511539', '83105511539.499999999999999999999999999999', -1, 4);
t('83105511539', '83105511539.5', '-1', 5);
t('83105511540', '83105511539.5000000000000000000001', -1, 5);
t('83105511540', '83105511539.5', new Decimal(-1), 3, 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 3, 4);
t('83105511539', '83105511539.5', new Decimal('-1'), 3, 5);
t('83105511540', '83105511539.5000000000000000000001', -1, 3, 5);
t('-83105511540', '-83105511539.5', new Decimal(-1), 4);
t('-83105511539', '-83105511539.499999999999999999999999999999', 1, 4);
t('-83105511539', '-83105511539.5', new Decimal('-1'), 5);
t('-83105511540', '-83105511539.5000000000000000000001', -1, 5);
t('83105511540', '83105511539.5', 1, 0);
t('83105511539', '83105511539.5', 1, 1);
t('83105511540', '83105511539.5', 1, 2);
t('83105511539', '83105511539.5', 1, 3);
t('83105511540', '83105511539.5', 1, 4);
t('83105511539', '83105511539.5', 1, 5);
t('83105511540', '83105511539.5', 1, 6);
t('83105511540', '83105511539.5', 1, 7);
t('83105511539', '83105511539.5', 1, 8);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 0);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 1);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 2);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 3);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 5);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 6);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 7);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 8);
t('83105511540', '83105511539.5000000000000000000001', void 0, 0);
t('83105511539', '83105511539.5000000000000000000001', 1, 1);
t('83105511540', '83105511539.5000000000000000000001', void 0, 2);
t('83105511539', '83105511539.5000000000000000000001', 1, 3);
t('83105511540', '83105511539.5000000000000000000001', void 0, 4);
t('83105511540', '83105511539.5000000000000000000001', 1, 5);
t('83105511540', '83105511539.5000000000000000000001', void 0, 6);
t('83105511540', '83105511539.5000000000000000000001', 1, 7);
t('83105511540', '83105511539.5000000000000000000001', void 0, 8);
t('83105511540', '83105511539.5', 1, 30, 0);
t('83105511539', '83105511539.5', 1, 30, 1);
t('83105511540', '83105511539.5', 1, 30, 2);
t('83105511539', '83105511539.5', 1, 30, 3);
t('83105511540', '83105511539.5', 1, 30, 4);
t('83105511539', '83105511539.5', 1, 30, 5);
t('83105511540', '83105511539.5', 1, 30, 6);
t('83105511540', '83105511539.5', 1, 30, 7);
t('83105511539', '83105511539.5', 1, 30, 8);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 0);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 1);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 2);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 3);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 4);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 5);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 6);
t('83105511539', '83105511539.499999999999999999999999999999', 1, 30, 7);
t('83105511539', '83105511539.499999999999999999999999999999', void 0, 30, 8);
t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 0);
t('83105511540', '83105511539.5000000000000000000001', 1, 30, 1);
t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 2);
t('83105511540', '83105511539.5000000000000000000001', 1, 30, 3);
t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 4);
t('83105511540', '83105511539.5000000000000000000001', 1, 30, 5);
t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 6);
t('83105511540', '83105511539.5000000000000000000001', 1, 30, 7);
t('83105511540', '83105511539.5000000000000000000001', void 0, 30, 8);
Decimal.rounding = 0;
t('83105511540', '83105511539.5');
t('83105511540', '83105511539.5', void 0, 11);
Decimal.rounding = 1;
t('83105511539', '83105511539.5');
t('83105511539', '83105511539.5', void 0, 11);
t('3847570', '3847561.00000749', 10, 0);
t('42840000000000000', '42835000000000001', '1e+13', 0);
t('42830000000000000', '42835000000000001', '1e+13', 1);
t('42840000000000000', '42835000000000000.0002', '1e+13', 0);
t('42830000000000000', '42835000000000000.0002', '1e+13', 1);
t('500', '449.999', 100, 0);
t('400', '449.999', 100, 1);
t('500', '449.999', 100, 2);
t('400', '449.999', 100, 3);
t('400', '449.999', 100, 4);
t('400', '449.999', 100, 5);
t('400', '449.999', 100, 6);
t('400', '449.999', 100, 7);
t('400', '449.999', 100, 8);
t('-500', '-449.999', 100, 0);
t('-400', '-449.999', 100, 1);
t('-400', '-449.999', 100, 2);
t('-500', '-449.999', 100, 3);
t('-400', '-449.999', 100, 4);
t('-400', '-449.999', 100, 5);
t('-400', '-449.999', 100, 6);
t('-400', '-449.999', 100, 7);
t('-400', '-449.999', 100, 8);
t('500', '450', 100, 0);
t('400', '450', 100, 1);
t('500', '450', 100, 2);
t('400', '450', 100, 3);
t('500', '450', 100, 4);
t('400', '450', 100, 5);
t('400', '450', 100, 6);
t('500', '450', 100, 7);
t('400', '450', 100, 8);
t('-500', '-450', 100, 0);
t('-400', '-450', 100, 1);
t('-400', '-450', 100, 2);
t('-500', '-450', 100, 3);
t('-500', '-450', 100, 4);
t('-400', '-450', 100, 5);
t('-400', '-450', 100, 6);
t('-400', '-450', 100, 7);
t('-500', '-450', 100, 8);
Decimal.rounding = 0;
t('500', '450.001', 100);
Decimal.rounding = 1;
t('400', '450.001', 100);
Decimal.rounding = 2;
t('500', '450.001', 100);
Decimal.rounding = 3;
t('400', '450.001', 100);
Decimal.rounding = 4;
t('500', '450.001', 100);
Decimal.rounding = 5;
t('500', '450.001', 100);
Decimal.rounding = 6;
t('500', '450.001', 100);
Decimal.rounding = 7;
t('500', '450.001', 100);
Decimal.rounding = 8;
t('500', '450.001', 100);
Decimal.rounding = 0;
t('-500', '-450.001', 100);
Decimal.rounding = 1;
t('-400', '-450.001', 100);
Decimal.rounding = 2;
t('-400', '-450.001', 100);
Decimal.rounding = 3;
t('-500', '-450.001', 100);
Decimal.rounding = 4;
t('-500', '-450.001', 100);
Decimal.rounding = 5;
t('-500', '-450.001', 100);
Decimal.rounding = 6;
t('-500', '-450.001', 100);
Decimal.rounding = 7;
t('-500', '-450.001', 100);
Decimal.rounding = 8;
t('-500', '-450.001', 100);
t('3847560', '3847561.00000749', 10, 11, 0);
t('42840000000000000', '42835000000000001', '1e+13', 2, 0);
t('42840000000000000', '42835000000000001', '1e+13', 2, 1);
t('42840000000000000', '42835000000000000.0002', '1e+13', 200, 0);
t('42840000000000000', '42835000000000000.0002', '1e+13', 200, 1);
});

View File

@ -28,7 +28,6 @@
'atanh',
'cbrt',
'ceil',
'clamp',
'clone',
'cmp',
'config',
@ -60,7 +59,6 @@
'sin',
'sinh',
'sqrt',
'sum',
'tan',
'tanh',
'times',

View File

@ -15,7 +15,6 @@ console.log('\n Testing decimal.js\n');
'atanh',
'cbrt',
'ceil',
'clamp',
'clone',
'cmp',
'config',
@ -47,7 +46,6 @@ console.log('\n Testing decimal.js\n');
'sin',
'sinh',
'sqrt',
'sum',
'tan',
'tanh',
'times',