mirror of
https://github.com/MikeMcl/decimal.js.git
synced 2024-10-27 20:34:12 +00:00
Compare commits
130 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7f01abd83d | ||
|
0f148f9787 | ||
|
858501020d | ||
|
25c4a107aa | ||
|
063e5fb7e9 | ||
|
65e63cedc4 | ||
|
3dcadc7351 | ||
|
d2e36c6524 | ||
|
f8b93e0bfa | ||
|
f03f8686b6 | ||
|
61eef6711d | ||
|
1e72c7b1f8 | ||
|
0bc3fdbdaa | ||
|
edadaf8f93 | ||
|
31ebcc8250 | ||
|
a699f6b256 | ||
|
1bcf067429 | ||
|
00d5e300d7 | ||
|
220f11c498 | ||
|
c29c80c6e3 | ||
|
69e91fecd2 | ||
|
d102ead7af | ||
|
ac0711318f | ||
|
66a21ee9d1 | ||
|
0e0dcaec27 | ||
|
04c6c6f115 | ||
|
6e5a599fcc | ||
|
498327d8d4 | ||
|
6545e24c24 | ||
|
26b348f383 | ||
|
be7d6b05ab | ||
|
e70384ea7e | ||
|
11699c1d1c | ||
|
023981df23 | ||
|
24726f5950 | ||
|
b53a593401 | ||
|
39991b8249 | ||
|
a56f15f374 | ||
|
5a6e1384d8 | ||
|
b75e2fdc45 | ||
|
30828c01a6 | ||
|
3f9eeb0897 | ||
|
f468de8e79 | ||
|
fe487dc39e | ||
|
0a81a6758a | ||
|
f0dc751067 | ||
|
6c6fbe7cac | ||
|
b041339e2a | ||
|
8262235084 | ||
|
7818dff1ca | ||
|
50dc74c3cb | ||
|
bc2979c96a | ||
|
9106f0a010 | ||
|
0c83e6a804 | ||
|
846b9e6c57 | ||
|
bf504624f8 | ||
|
c6fb6a87b3 | ||
|
903b98877d | ||
|
2e75623cc3 | ||
|
93930f8a23 | ||
|
a821788a7c | ||
|
ba99e657d2 | ||
|
6016146ef7 | ||
|
c99bdef459 | ||
|
caed629d5a | ||
|
2c3198d555 | ||
|
4949869b5e | ||
|
caaba6d513 | ||
|
875f6d0f7a | ||
|
8a7b6a3068 | ||
|
dbba3d5b18 | ||
|
eb2efa06fb | ||
|
dbf681be4a | ||
|
fd668fea70 | ||
|
17a358f2f4 | ||
|
1cbf5ffb44 | ||
|
84bf971160 | ||
|
6e91027ee0 | ||
|
35a8cddde9 | ||
|
244138f8c2 | ||
|
d477d566ea | ||
|
1a552b2d6c | ||
|
074a97d06a | ||
|
a0b78b6275 | ||
|
df5ed821f3 | ||
|
433759294a | ||
|
4ca749e352 | ||
|
9f3875a54d | ||
|
2ed8a040c7 | ||
|
9ae7ca7088 | ||
|
3cbfd6da08 | ||
|
784a38a8db | ||
|
22ac9377c6 | ||
|
32f0a63095 | ||
|
4d05267ba8 | ||
|
3566299686 | ||
|
f2410a712a | ||
|
981ba5c749 | ||
|
2be14dfdd7 | ||
|
85a499eb86 | ||
|
eec7273c19 | ||
|
5547cc805d | ||
|
d199c97af4 | ||
|
45eec0b863 | ||
|
27de48c4c3 | ||
|
e6c234f109 | ||
|
148be72e6f | ||
|
4df9158a1a | ||
|
e48bddf45a | ||
|
46b1aad320 | ||
|
fb37ca6bde | ||
|
28451fb05c | ||
|
8a631cc327 | ||
|
45e9752280 | ||
|
ff621c1e35 | ||
|
6ec2449a07 | ||
|
7d9b530aa2 | ||
|
6785556026 | ||
|
03cf981dcf | ||
|
5a916a1de2 | ||
|
dc642c0429 | ||
|
c029df316a | ||
|
6a663287f0 | ||
|
f3777d8baf | ||
|
6effe2db27 | ||
|
1a1a0ace51 | ||
|
8df4a72aaa | ||
|
50f63bec5c | ||
|
c512eb9a5a | ||
|
948d368527 |
@ -1,2 +1,3 @@
|
||||
test
|
||||
excluded
|
||||
excluded
|
||||
.*
|
||||
|
18
.travis.yml
18
.travis.yml
@ -1,11 +1,23 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "node"
|
||||
- "18"
|
||||
- "17"
|
||||
- "16"
|
||||
- "15"
|
||||
- "14"
|
||||
- "13"
|
||||
- "12"
|
||||
- "11"
|
||||
- "10"
|
||||
- "9"
|
||||
- "8"
|
||||
- "7"
|
||||
- "6"
|
||||
- "5"
|
||||
- "4"
|
||||
- "4.1"
|
||||
- "4.0"
|
||||
- "0.12"
|
||||
- "0.11"
|
||||
- "0.10"
|
||||
- "0.8"
|
||||
- "0.6"
|
||||
- "iojs"
|
||||
|
271
CHANGELOG.md
271
CHANGELOG.md
@ -1,76 +1,249 @@
|
||||
####5.0.3
|
||||
* 06/02/2015
|
||||
* Refactor tests.
|
||||
#### 10.4.3
|
||||
* 04/12/2022
|
||||
* #211 Remove `toStringTag` declaration for type compatibility.
|
||||
|
||||
####5.0.2
|
||||
* 05/02/2015
|
||||
* Added immutability tests.
|
||||
* Minor *decimal.js* clean-up.
|
||||
#### 10.4.2
|
||||
* 12/10/2022
|
||||
* #209 Correct return type.
|
||||
|
||||
####5.0.1
|
||||
* 28/01/2015
|
||||
* Bugfix: #20 cos mutates value.
|
||||
* Add pi info to docs.
|
||||
#### 10.4.1
|
||||
* 16/09/2022
|
||||
* #205 Add './decimal' subpath to *package.json* `exports`.
|
||||
|
||||
####5.0.0
|
||||
#### 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.
|
||||
|
||||
#### 7.3.0
|
||||
* 26/09/2017
|
||||
* Rename *decimal.es6.js* to *decimal.mjs*.
|
||||
* Amend *.travis.yml*.
|
||||
|
||||
#### 7.2.4
|
||||
* 09/09/2017
|
||||
* Update docs regarding `global.crypto`.
|
||||
* Fix `import` issues.
|
||||
|
||||
#### 7.2.3
|
||||
* 27/06/2017
|
||||
* Bugfix: #58 `pow` sometimes throws when result is `Infinity`.
|
||||
|
||||
#### 7.2.2
|
||||
* 25/06/2017
|
||||
* Bugfix: #57 Powers of -1 for integers over `Number.MAX_SAFE_INTEGER`.
|
||||
|
||||
#### 7.2.1
|
||||
* 04/05/2017
|
||||
* Fix *README* badges.
|
||||
|
||||
#### 7.2.0
|
||||
* 09/04/2017
|
||||
* Add *decimal.es6.js*
|
||||
|
||||
#### 7.1.2
|
||||
* 05/04/2017
|
||||
* `Decimal.default` to `Decimal['default']` IE8 issue
|
||||
|
||||
#### 7.1.1
|
||||
* 10/01/2017
|
||||
* Remove duplicated for-loop
|
||||
* Minor refactoring
|
||||
|
||||
#### 7.1.0
|
||||
* 09/11/2016
|
||||
* Support ES6 imports.
|
||||
|
||||
#### 7.0.0
|
||||
* 09/11/2016
|
||||
* Remove `require('crypto')` - leave it to the user
|
||||
* Default `Decimal.crypto` to `false`
|
||||
* Add `Decimal.set` as `Decimal.config` alias
|
||||
|
||||
#### 6.0.0
|
||||
* 30/06/2016
|
||||
* Removed base-88 serialization format
|
||||
* Amended `toJSON` and removed `Decimal.fromJSON` accordingly
|
||||
|
||||
#### 5.0.8
|
||||
* 09/03/2016
|
||||
* Add newline to single test results
|
||||
* Correct year
|
||||
|
||||
#### 5.0.7
|
||||
* 29/02/2016
|
||||
* Add decimal.js-light link
|
||||
* Remove outdated example from docs
|
||||
|
||||
#### 5.0.6
|
||||
* 22/02/2016
|
||||
* Add bower.json
|
||||
|
||||
#### 5.0.5
|
||||
* 20/02/2016
|
||||
* Bugfix: #26 wrong precision applied
|
||||
|
||||
#### 5.0.4
|
||||
* 14/02/2016
|
||||
* Bugfix: #26 clone
|
||||
|
||||
#### 5.0.3
|
||||
* 06/02/2016
|
||||
* Refactor tests
|
||||
|
||||
#### 5.0.2
|
||||
* 05/02/2016
|
||||
* Added immutability tests
|
||||
* Minor *decimal.js* clean-up
|
||||
|
||||
#### 5.0.1
|
||||
* 28/01/2016
|
||||
* Bugfix: #20 cos mutates value
|
||||
* Add pi info to docs
|
||||
|
||||
#### 5.0.0
|
||||
* 25/01/2016
|
||||
* Added trigonometric functions and `cubeRoot` method.
|
||||
* Added most of JavaScript's `Math` object methods as Decimal methods.
|
||||
* Added `toBinary`, `toHexadecimal` and `toOctal` methods.
|
||||
* Added `isPositive` method.
|
||||
* Removed the 15 significant digit limit for numbers.
|
||||
* `toFraction` now returns an array of two Decimals, not two strings.
|
||||
* String values containing whitespace or a plus sign are no longer accepted.
|
||||
* `valueOf` now returns `'-0'` for minus zero.
|
||||
* `comparedTo` now returns `NaN` not `null` for comparisons with `NaN`.
|
||||
* `Decimal.max` and `Decimal.min` no longer accept an array.
|
||||
* The Decimal constructor and `toString` no longer accept a base argument.
|
||||
* Binary, hexadecimal and octal prefixes are now recognised for string values.
|
||||
* Removed `Decimal.errors` configuration property.
|
||||
* Removed `toFormat` method.
|
||||
* Removed `Decimal.ONE`.
|
||||
* Renamed `exponential` method to `naturalExponential`.
|
||||
* Renamed `Decimal.constructor` method to `Decimal.clone`.
|
||||
* Simplified error handling and amended error messages.
|
||||
* Refactored the test suite.
|
||||
* `Decimal.crypto` is now `undefined` by default, and the `crypto` object will be used if available.
|
||||
* Major internal refactoring.
|
||||
* Removed *bower.json*.
|
||||
* Added trigonometric functions and `cubeRoot` method
|
||||
* Added most of JavaScript's `Math` object methods as Decimal methods
|
||||
* Added `toBinary`, `toHexadecimal` and `toOctal` methods
|
||||
* Added `isPositive` method
|
||||
* Removed the 15 significant digit limit for numbers
|
||||
* `toFraction` now returns an array of two Decimals, not two strings
|
||||
* String values containing whitespace or a plus sign are no longer accepted
|
||||
* `valueOf` now returns `'-0'` for minus zero
|
||||
* `comparedTo` now returns `NaN` not `null` for comparisons with `NaN`
|
||||
* `Decimal.max` and `Decimal.min` no longer accept an array
|
||||
* The Decimal constructor and `toString` no longer accept a base argument
|
||||
* Binary, hexadecimal and octal prefixes are now recognised for string values
|
||||
* Removed `Decimal.errors` configuration property
|
||||
* Removed `toFormat` method
|
||||
* Removed `Decimal.ONE`
|
||||
* Renamed `exponential` method to `naturalExponential`
|
||||
* Renamed `Decimal.constructor` method to `Decimal.clone`
|
||||
* Simplified error handling and amended error messages
|
||||
* Refactored the test suite
|
||||
* `Decimal.crypto` is now `undefined` by default, and the `crypto` object will be used if available
|
||||
* Major internal refactoring
|
||||
* Removed *bower.json*
|
||||
|
||||
####4.0.2
|
||||
* 20/02/2015 Add bower.json. Add source map. Amend travis CI. Amend doc/comments.
|
||||
#### 4.0.2
|
||||
* 20/02/2015 Add bower.json. Add source map. Amend travis CI. Amend doc/comments
|
||||
|
||||
####4.0.1
|
||||
* 11/12/2014 Assign correct constructor when duplicating a Decimal.
|
||||
#### 4.0.1
|
||||
* 11/12/2014 Assign correct constructor when duplicating a Decimal
|
||||
|
||||
####4.0.0
|
||||
* 10/11/2014 `toFormat` amended to use `Decimal.format` object for more flexible configuration.
|
||||
#### 4.0.0
|
||||
* 10/11/2014 `toFormat` amended to use `Decimal.format` object for more flexible configuration
|
||||
|
||||
####3.0.1
|
||||
#### 3.0.1
|
||||
* 8/06/2014 Surround crypto require in try catch. See issue #5
|
||||
|
||||
####3.0.0
|
||||
#### 3.0.0
|
||||
* 4/06/2014 `random` simplified. Major internal changes mean the properties of a Decimal must now be considered read-only
|
||||
|
||||
####2.1.0
|
||||
#### 2.1.0
|
||||
* 4/06/2014 Amend UMD
|
||||
|
||||
####2.0.3
|
||||
#### 2.0.3
|
||||
* 8/05/2014 Fix NaN toNumber
|
||||
|
||||
####2.0.2
|
||||
#### 2.0.2
|
||||
* 30/04/2014 Correct doc links
|
||||
|
||||
####2.0.1
|
||||
#### 2.0.1
|
||||
* 10/04/2014 Update npmignore
|
||||
|
||||
####2.0.0
|
||||
#### 2.0.0
|
||||
* 10/04/2014 Add `toSignificantDigits`
|
||||
* Remove `toInteger`
|
||||
* No arguments to `ceil`, `floor`, `round` and `trunc`
|
||||
|
||||
####1.0.1
|
||||
#### 1.0.1
|
||||
* 07/04/2014 Minor documentation clean-up
|
||||
|
||||
####1.0.0
|
||||
#### 1.0.0
|
||||
* 02/04/2014 Initial release
|
||||
|
@ -1,6 +1,6 @@
|
||||
The MIT Expat Licence.
|
||||
The MIT Licence.
|
||||
|
||||
Copyright (c) 2016 Michael Mclaughlin
|
||||
Copyright (c) 2022 Michael Mclaughlin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
271
README.md
271
README.md
@ -2,188 +2,245 @@
|
||||
|
||||
An arbitrary-precision Decimal type for JavaScript.
|
||||
|
||||
*WARNING: Version 5 brings significant API changes (see CHANGELOG). Version 4 will continue to be supported.*
|
||||
<br>
|
||||
[](https://www.npmjs.com/package/decimal.js)
|
||||
[](https://www.npmjs.com/package/decimal.js)
|
||||
[](https://travis-ci.org/MikeMcl/decimal.js)
|
||||
[](https://cdnjs.com/libraries/decimal.js)
|
||||
|
||||
<br>
|
||||
|
||||
## Features
|
||||
|
||||
- Integers and floats
|
||||
- Integers and floats
|
||||
- Simple but full-featured API
|
||||
- Replicates many of the methods of JavaScript's `Number.prototype` and `Math` objects
|
||||
- Also handles hexadecimal, binary and octal values
|
||||
- Supports serialization of Decimals to a compact base-88 format for transmission or persistence
|
||||
- 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](http://mikemcl.github.io/decimal.js/) and test set
|
||||
- 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*
|
||||
|
||||

|
||||
|
||||
The library is similar to [bignumber.js](https://github.com/MikeMcl/bignumber.js/), but here
|
||||
precision is specified in terms of significant digits instead of decimal places, and all
|
||||
precision is specified in terms of significant digits rather than decimal places, and all
|
||||
calculations are rounded to the precision (similar to Python's decimal module) rather than just
|
||||
those involving division.
|
||||
|
||||
This library also adds the trigonometric functions, among others, and supports non-integer powers.
|
||||
This library also adds the trigonometric functions, among others, and supports non-integer powers,
|
||||
which makes it a significantly larger library than *bignumber.js* and the even smaller
|
||||
[big.js](https://github.com/MikeMcl/big.js/).
|
||||
|
||||
Another major difference is that this library enables multiple Decimal constructors to be created
|
||||
each with their own configuration. This is, however, 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/).
|
||||
|
||||
## Load
|
||||
|
||||
The library is the single JavaScript file *decimal.js* (or minified, *decimal.min.js*).
|
||||
The library is the single JavaScript file *decimal.js* or ES module *decimal.mjs*.
|
||||
|
||||
It can be loaded using a script tag in an HTML document for the browser
|
||||
Browser:
|
||||
|
||||
<script src='path/to/decimal.js'></script>
|
||||
```html
|
||||
<script src='path/to/decimal.js'></script>
|
||||
|
||||
or as a [Node.js](http://nodejs.org) module using `require`.
|
||||
<script type="module">
|
||||
import Decimal from './path/to/decimal.mjs';
|
||||
...
|
||||
</script>
|
||||
```
|
||||
|
||||
var Decimal = require('decimal');
|
||||
[Node.js](https://nodejs.org):
|
||||
|
||||
For Node, the library is available from the [npm](https://npmjs.org/) registry
|
||||
```bash
|
||||
npm install decimal.js
|
||||
```
|
||||
```js
|
||||
const Decimal = require('decimal.js');
|
||||
|
||||
$ npm install decimal.js
|
||||
|
||||
To load with AMD loader libraries such as [requireJS](http://requirejs.org/):
|
||||
import Decimal from 'decimal.js';
|
||||
|
||||
require(['decimal'], function(Decimal) {
|
||||
// Use Decimal here in local scope. No global Decimal.
|
||||
});
|
||||
import {Decimal} from 'decimal.js';
|
||||
```
|
||||
|
||||
## Use
|
||||
|
||||
*In all examples below, `var`, semicolons and `toString` calls are not shown.
|
||||
*In all examples below, 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 function object, `Decimal`, the constructor of Decimal instances.
|
||||
The library exports a single constructor function, `Decimal`, which expects a single argument that is a number, string or Decimal instance.
|
||||
|
||||
It accepts a value of type number, string or Decimal.
|
||||
```js
|
||||
x = new Decimal(123.4567)
|
||||
y = new Decimal('123456.7e-3')
|
||||
z = new Decimal(x)
|
||||
x.equals(y) && y.equals(z) && x.equals(z) // true
|
||||
```
|
||||
|
||||
x = new Decimal(123.4567)
|
||||
y = new Decimal('123456.7e-3')
|
||||
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.
|
||||
|
||||
A value can also be in binary, hexadecimal or octal if the appropriate prefix is included.
|
||||
```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'
|
||||
|
||||
x = new Decimal('0xff.f') // '255.9375'
|
||||
y = new Decimal('0b10101100') // '172'
|
||||
z = x.plus(y) // '427.9375'
|
||||
|
||||
z.toBinary() // '0b110101011.1111'
|
||||
z.toBinary(13) // '0b1.101010111111p+8'
|
||||
// Precision loss from using numeric literals outside the range of Number values.
|
||||
new Decimal(2e+308) // 'Infinity'
|
||||
new Decimal(1e-324) // '0'
|
||||
|
||||
A Decimal is immutable in the sense that it is not changed by its methods.
|
||||
// Precision loss from the unexpected result of arithmetic with Number values.
|
||||
new Decimal(0.7 + 0.1) // '0.7999999999999999'
|
||||
```
|
||||
|
||||
0.3 - 0.1 // 0.19999999999999998
|
||||
x = new Decimal(0.3)
|
||||
x.minus(0.1) // '0.2'
|
||||
x // '0.3'
|
||||
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.
|
||||
|
||||
```js
|
||||
x = new Decimal('0xff.f') // '255.9375'
|
||||
y = new Decimal('0b10101100') // '172'
|
||||
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.
|
||||
|
||||
```js
|
||||
0.3 - 0.1 // 0.19999999999999998
|
||||
x = new Decimal(0.3)
|
||||
x.minus(0.1) // '0.2'
|
||||
x // '0.3'
|
||||
```
|
||||
|
||||
The methods that return a Decimal can be chained.
|
||||
|
||||
x.dividedBy(y).plus(z).times(9).floor()
|
||||
x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()
|
||||
```js
|
||||
x.dividedBy(y).plus(z).times(9).floor()
|
||||
x.times('1.23456780123456789e+9').plus(9876.5432321).dividedBy('4444562598.111772').ceil()
|
||||
```
|
||||
|
||||
Many method names have a shorter alias.
|
||||
|
||||
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
|
||||
```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
|
||||
```
|
||||
|
||||
Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods,
|
||||
Most of the methods of JavaScript's `Number.prototype` and `Math` objects are replicated.
|
||||
|
||||
x = new Decimal(255.5)
|
||||
x.toExponential(5) // '2.55500e+2'
|
||||
x.toFixed(5) // '255.50000'
|
||||
x.toPrecision(5) // '255.50'
|
||||
|
||||
and many of the methods of JavaScript's Math object are also replicated.
|
||||
```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'
|
||||
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,
|
||||
// Using `toFixed()` to avoid exponential notation:
|
||||
x = new Decimal('0.0000001')
|
||||
x.toString() // '1e-7'
|
||||
x.toFixed() // '0.0000001'
|
||||
```
|
||||
|
||||
x = new Decimal(NaN) // 'NaN'
|
||||
y = new Decimal(Infinity) // 'Infinity'
|
||||
x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
|
||||
|
||||
and a `toFraction` method with an optional *maximum denominator* argument
|
||||
And there are `isNaN` and `isFinite` methods, as `NaN` and `Infinity` are valid `Decimal` values.
|
||||
|
||||
z = new Decimal(355)
|
||||
pi = z.dividedBy(113) // '3.1415929204'
|
||||
pi.toFraction() // [ '7853982301', '2500000000' ]
|
||||
pi.toFraction(1000) // [ '355', '113' ]
|
||||
```js
|
||||
x = new Decimal(NaN) // 'NaN'
|
||||
y = new Decimal(Infinity) // 'Infinity'
|
||||
x.isNaN() && !y.isNaN() && !x.isFinite() && !y.isFinite() // true
|
||||
```
|
||||
|
||||
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.
|
||||
There is also a `toFraction` method with an optional *maximum denominator* argument.
|
||||
|
||||
As mentioned above, multiple Decimal constructors can be created, each with their own independent
|
||||
configuration which applies to all Decimal numbers created from it.
|
||||
```js
|
||||
z = new Decimal(355)
|
||||
pi = z.dividedBy(113) // '3.1415929204'
|
||||
pi.toFraction() // [ '7853982301', '2500000000' ]
|
||||
pi.toFraction(1000) // [ '355', '113' ]
|
||||
```
|
||||
|
||||
// Set the precision and rounding of the default Decimal constructor
|
||||
Decimal.config({ precision: 5, rounding: 4 })
|
||||
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.
|
||||
|
||||
// Create another Decimal constructor, optionally passing in a configuration object
|
||||
Decimal10 = Decimal.clone({ precision: 10, rounding: 1 })
|
||||
For advanced usage, multiple Decimal constructors can be created, each with their own independent
|
||||
configuration which applies to all Decimal numbers created from it.
|
||||
|
||||
x = new Decimal(5)
|
||||
y = new Decimal10(5)
|
||||
```js
|
||||
// Set the precision and rounding of the default Decimal constructor
|
||||
Decimal.set({ precision: 5, rounding: 4 })
|
||||
|
||||
x.div(3) // '1.6667'
|
||||
y.div(3) // '1.666666666'
|
||||
// Create another Decimal constructor, optionally passing in a configuration object
|
||||
Dec = Decimal.clone({ precision: 9, rounding: 1 })
|
||||
|
||||
The value of a Decimal is stored in a floating point format in terms of its digits, exponent and sign.
|
||||
x = new Decimal(5)
|
||||
y = new Dec(5)
|
||||
|
||||
x = new Decimal(-12345.67);
|
||||
x.d // [ 12345, 6700000 ] digits (base 10000)
|
||||
x.e // 4 exponent (base 10)
|
||||
x.s // -1 sign
|
||||
x.div(3) // '1.6667'
|
||||
y.div(3) // '1.66666666'
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
```js
|
||||
x = new Decimal(-12345.67);
|
||||
x.d // [ 12345, 6700000 ] digits (base 10000000)
|
||||
x.e // 4 exponent (base 10)
|
||||
x.s // -1 sign
|
||||
```
|
||||
|
||||
For further information see the [API](http://mikemcl.github.io/decimal.js/) reference in the *doc* directory.
|
||||
|
||||
## Test
|
||||
|
||||
The library can be tested using Node.js or a browser.
|
||||
To run the tests using Node.js from the root directory:
|
||||
|
||||
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.
|
||||
```bash
|
||||
npm test
|
||||
```
|
||||
|
||||
To run all the tests, from a command-line at the root directory using npm
|
||||
Each separate test module can also be executed individually, for example:
|
||||
|
||||
$ npm test
|
||||
```bash
|
||||
node test/modules/toFraction
|
||||
```
|
||||
|
||||
or at the *test* directory using Node
|
||||
|
||||
$ node test
|
||||
|
||||
Each separate test module can also be executed individually, for example, at the *test/modules* directory
|
||||
To run the tests in a browser, open *test/test.html*.
|
||||
|
||||
$ node toFraction
|
||||
## Minify
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
npm install uglify-js -g
|
||||
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
|
||||
|
||||
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
|
||||
```js
|
||||
import Decimal from './decimal.min.mjs';
|
||||
```
|
||||
|
||||
## Licence
|
||||
|
||||
MIT Expat.
|
||||
|
||||
See *LICENCE.md*
|
||||
[The MIT Licence (Expat).](LICENCE.md)
|
||||
|
299
decimal.d.ts
vendored
Normal file
299
decimal.d.ts
vendored
Normal file
@ -0,0 +1,299 @@
|
||||
// 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:
|
||||
//
|
||||
// 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 Constructor = typeof Decimal;
|
||||
export type Instance = 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 {
|
||||
precision?: number;
|
||||
rounding?: Rounding;
|
||||
toExpNeg?: number;
|
||||
toExpPos?: number;
|
||||
minE?: number;
|
||||
maxE?: number;
|
||||
crypto?: boolean;
|
||||
modulo?: Modulo;
|
||||
defaults?: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export declare class Decimal {
|
||||
readonly d: number[];
|
||||
readonly e: number;
|
||||
readonly s: number;
|
||||
|
||||
constructor(n: Decimal.Value);
|
||||
|
||||
absoluteValue(): 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;
|
||||
|
||||
cosine(): Decimal;
|
||||
cos(): Decimal;
|
||||
|
||||
cubeRoot(): Decimal;
|
||||
cbrt(): Decimal;
|
||||
|
||||
decimalPlaces(): number;
|
||||
dp(): number;
|
||||
|
||||
dividedBy(n: Decimal.Value): Decimal;
|
||||
div(n: Decimal.Value): Decimal;
|
||||
|
||||
dividedToIntegerBy(n: Decimal.Value): Decimal;
|
||||
divToInt(n: Decimal.Value): Decimal;
|
||||
|
||||
equals(n: Decimal.Value): boolean;
|
||||
eq(n: Decimal.Value): boolean;
|
||||
|
||||
floor(): Decimal;
|
||||
|
||||
greaterThan(n: Decimal.Value): boolean;
|
||||
gt(n: Decimal.Value): boolean;
|
||||
|
||||
greaterThanOrEqualTo(n: Decimal.Value): boolean;
|
||||
gte(n: Decimal.Value): 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: Decimal.Value): boolean;
|
||||
lt(n: Decimal.Value): boolean;
|
||||
|
||||
lessThanOrEqualTo(n: Decimal.Value): boolean;
|
||||
lte(n: Decimal.Value): boolean;
|
||||
|
||||
logarithm(n?: Decimal.Value): Decimal;
|
||||
log(n?: Decimal.Value): Decimal;
|
||||
|
||||
minus(n: Decimal.Value): Decimal;
|
||||
sub(n: Decimal.Value): Decimal;
|
||||
|
||||
modulo(n: Decimal.Value): Decimal;
|
||||
mod(n: Decimal.Value): Decimal;
|
||||
|
||||
naturalExponential(): Decimal;
|
||||
exp(): Decimal;
|
||||
|
||||
naturalLogarithm(): Decimal;
|
||||
ln(): Decimal;
|
||||
|
||||
negated(): Decimal;
|
||||
neg(): Decimal;
|
||||
|
||||
plus(n: Decimal.Value): Decimal;
|
||||
add(n: Decimal.Value): 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: Decimal.Value): Decimal;
|
||||
mul(n: Decimal.Value) : Decimal;
|
||||
|
||||
toBinary(significantDigits?: number): string;
|
||||
toBinary(significantDigits: number, rounding: Decimal.Rounding): string;
|
||||
|
||||
toDecimalPlaces(decimalPlaces?: number): Decimal;
|
||||
toDecimalPlaces(decimalPlaces: number, rounding: Decimal.Rounding): Decimal;
|
||||
toDP(decimalPlaces?: number): Decimal;
|
||||
toDP(decimalPlaces: number, rounding: Decimal.Rounding): Decimal;
|
||||
|
||||
toExponential(decimalPlaces?: number): string;
|
||||
toExponential(decimalPlaces: number, rounding: Decimal.Rounding): string;
|
||||
|
||||
toFixed(decimalPlaces?: number): string;
|
||||
toFixed(decimalPlaces: number, rounding: Decimal.Rounding): string;
|
||||
|
||||
toFraction(max_denominator?: Decimal.Value): Decimal[];
|
||||
|
||||
toHexadecimal(significantDigits?: number): string;
|
||||
toHexadecimal(significantDigits: number, rounding: Decimal.Rounding): string;
|
||||
toHex(significantDigits?: number): string;
|
||||
toHex(significantDigits: number, rounding?: Decimal.Rounding): string;
|
||||
|
||||
toJSON(): string;
|
||||
|
||||
toNearest(n: Decimal.Value, rounding?: Decimal.Rounding): Decimal;
|
||||
|
||||
toNumber(): number;
|
||||
|
||||
toOctal(significantDigits?: number): string;
|
||||
toOctal(significantDigits: number, rounding: Decimal.Rounding): string;
|
||||
|
||||
toPower(n: Decimal.Value): Decimal;
|
||||
pow(n: Decimal.Value): Decimal;
|
||||
|
||||
toPrecision(significantDigits?: number): string;
|
||||
toPrecision(significantDigits: number, rounding: Decimal.Rounding): string;
|
||||
|
||||
toSignificantDigits(significantDigits?: number): Decimal;
|
||||
toSignificantDigits(significantDigits: number, rounding: Decimal.Rounding): Decimal;
|
||||
toSD(significantDigits?: number): Decimal;
|
||||
toSD(significantDigits: number, rounding: Decimal.Rounding): Decimal;
|
||||
|
||||
toString(): string;
|
||||
|
||||
truncated(): Decimal;
|
||||
trunc(): Decimal;
|
||||
|
||||
valueOf(): string;
|
||||
|
||||
static abs(n: Decimal.Value): Decimal;
|
||||
static acos(n: Decimal.Value): Decimal;
|
||||
static acosh(n: Decimal.Value): Decimal;
|
||||
static add(x: Decimal.Value, y: Decimal.Value): Decimal;
|
||||
static asin(n: Decimal.Value): Decimal;
|
||||
static asinh(n: Decimal.Value): Decimal;
|
||||
static atan(n: Decimal.Value): Decimal;
|
||||
static atanh(n: Decimal.Value): 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 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 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
|
||||
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 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 precision: number;
|
||||
static readonly rounding: Decimal.Rounding;
|
||||
static readonly toExpNeg: number;
|
||||
static readonly toExpPos: number;
|
||||
static readonly minE: number;
|
||||
static readonly maxE: number;
|
||||
static readonly crypto: boolean;
|
||||
static readonly modulo: Decimal.Modulo;
|
||||
|
||||
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;
|
||||
}
|
320
decimal.global.d.ts
vendored
Normal file
320
decimal.global.d.ts
vendored
Normal file
@ -0,0 +1,320 @@
|
||||
// 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;
|
||||
}
|
697
decimal.js
697
decimal.js
File diff suppressed because it is too large
Load Diff
4
decimal.min.js
vendored
4
decimal.min.js
vendored
File diff suppressed because one or more lines are too long
4898
decimal.mjs
Normal file
4898
decimal.mjs
Normal file
File diff suppressed because it is too large
Load Diff
495
doc/API.html
495
doc/API.html
@ -61,7 +61,7 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
|
||||
<a class='nav-title' href="#">API</a>
|
||||
|
||||
<b>CONSTRUCTOR</b>
|
||||
<ul><li><a href="#decimal">Decimal</a></li></ul>
|
||||
<ul><li><a href="#decimal"><strong>Decimal</strong></a></li></ul>
|
||||
|
||||
<a href="#methods">Methods</a>
|
||||
<ul>
|
||||
@ -76,15 +76,15 @@ 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="#Dconfig" >config</a></li>
|
||||
<li><a href="#Dcos" >cos</a></li>
|
||||
<li><a href="#Dcosh" >cosh</a></li>
|
||||
<li><a href="#Ddiv" >div</a></li>
|
||||
<li><a href="#Dexp" >exp</a></li>
|
||||
<li><a href="#Dfloor" >floor</a></li>
|
||||
<li><a href="#DfromJSON" >fromJSON</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>
|
||||
@ -97,11 +97,13 @@ li span{float:right;margin-right:10px;color:#c0c0c0}
|
||||
<li><a href="#Dpow" >pow</a></li>
|
||||
<li><a href="#Drandom" >random</a></li>
|
||||
<li><a href="#Dround" >round</a></li>
|
||||
<li><a href="#Dset" >set</a></li>
|
||||
<li><a href="#Dsign" >sign</a></li>
|
||||
<li><a href="#Dsin" >sin</a></li>
|
||||
<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>
|
||||
@ -137,6 +139,7 @@ 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>
|
||||
@ -196,9 +199,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 & Infinity</a>
|
||||
@ -298,6 +301,7 @@ 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'
|
||||
@ -436,6 +440,16 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dclamp">clamp<code class='inset'>.clamp(min, max) <i>⇒ 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>⇒ Decimal constructor</i></code>
|
||||
@ -443,67 +457,36 @@ a.equals(b) // true</pre>
|
||||
<p><code>object</code>: <i>object</i></p>
|
||||
<p>
|
||||
Returns a new independent Decimal constructor with configuration settings as described by
|
||||
<code>object</code> (see <a href='#Dconfig'><code>config</code></a>), or with the same
|
||||
<code>object</code> (see <a href='#Dset'><code>set</code></a>), or with the same
|
||||
settings as <code>this</code> Decimal constructor if <code>object</code> is omitted.
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 5 })
|
||||
D9 = Decimal.clone({ precision: 9 })
|
||||
<pre>Decimal.set({ precision: 5 })
|
||||
Decimal9 = Decimal.clone({ precision: 9 })
|
||||
|
||||
a = new Decimal(1)
|
||||
b = new D9(1)
|
||||
b = new Decimal9(1)
|
||||
|
||||
a.div(3) // 0.33333
|
||||
b.div(3) // 0.333333333
|
||||
|
||||
// D9 = Decimal.clone({ precision: 9 }) is equivalent to:
|
||||
D9 = Decimal.clone()
|
||||
D9.config({ precision: 9 })</pre>
|
||||
// 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>
|
||||
<p>
|
||||
It is not inefficient in terms of memory usage to use multiple Decimal constructors as
|
||||
functions are shared between them.
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dconfig">
|
||||
config<code class='inset'>.config(object) <i>⇒ Decimal constructor</i></code>
|
||||
</h5>
|
||||
<p><code>object</code>: <i>object</i></p>
|
||||
<p>
|
||||
Configures the 'global' settings for <code>this</code> particular Decimal constructor, i.e.
|
||||
the settings which apply to operations performed on the Decimal instances created by it.
|
||||
</p>
|
||||
<p>Returns <code>this</code> Decimal constructor.</p>
|
||||
<p>
|
||||
The configuration object, <code>object</code>, can contain some or all of the properties
|
||||
described in detail at <a href="#constructor-properties">Properties</a> and shown in the
|
||||
example below.
|
||||
</p>
|
||||
<p>
|
||||
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>Throws on an invalid <code>object</code> or configuration property value.</p>
|
||||
<pre>
|
||||
// Defaults
|
||||
Decimal.config({
|
||||
precision: 20,
|
||||
rounding: 4,
|
||||
toExpNeg: -7,
|
||||
toExpPos: 21,
|
||||
maxE: 9e15,
|
||||
minE: -9e15,
|
||||
modulo: 1,
|
||||
crypto: undefined
|
||||
})</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 - which is not a problem if the user
|
||||
knows that the checks are unnecessary.
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dcos">cos<code class='inset'>.cos(x) <i>⇒ Decimal</i></code></h5>
|
||||
<p><code>x</code>: <i>number|string|Decimal</i></p>
|
||||
<p>See <code><a href='#cos'>cosine</a></code>.</p>
|
||||
@ -552,21 +535,6 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="DfromJSON">fromJSON<code class='inset'>.fromJSON(s) <i>⇒ Decimal</i></code></h5>
|
||||
<p><code>s</code>: <i>string</i><br /></p>
|
||||
<p>
|
||||
Returns a new Decimal whose value is the same as the value of the Decimal that created string
|
||||
<code>s</code> from a <a href='#toJSON'><code>toJSON</code></a> call.
|
||||
</p>
|
||||
<p>See <a href='#toJSON'><code>toJSON</code></a>.</p>
|
||||
<pre>
|
||||
a = new Decimal(x)
|
||||
s = a.toJSON()
|
||||
b = Decimal.fromJSON(s)
|
||||
a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dhypot">
|
||||
hypot<code class='inset'>.hypot([x [, y, ...]]) <i>⇒ Decimal</i></code>
|
||||
</h5>
|
||||
@ -592,6 +560,22 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="DisDecimal">
|
||||
isDecimal<code class='inset'>.isDecimal(object) <i>⇒ 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>⇒ Decimal</i></code></h5>
|
||||
<p>
|
||||
<code>x</code>: <i>number|string|Decimal</i><br />
|
||||
@ -631,7 +615,7 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
<h5 id="Dmax">
|
||||
max<code class='inset'>.max([x [, y, ...]]) <i>⇒ Decimal</i></code>
|
||||
max<code class='inset'>.max(x [, y, ...]) <i>⇒ Decimal</i></code>
|
||||
</h5>
|
||||
<p>
|
||||
<code>x</code>: <i>number|string|Decimal</i><br />
|
||||
@ -643,7 +627,7 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
<h5 id="Dmin">
|
||||
min<code class='inset'>.min([x [, y, ...]]) <i>⇒ Decimal</i></code>
|
||||
min<code class='inset'>.min(x [, y, ...]) <i>⇒ Decimal</i></code>
|
||||
</h5>
|
||||
<p>
|
||||
<code>x</code>: <i>number|string|Decimal</i><br />
|
||||
@ -726,14 +710,15 @@ a.equals(b) // true</pre>
|
||||
</p>
|
||||
<p>
|
||||
If the value of <code>this</code> Decimal constructor's
|
||||
<a href='#crypto'><code>crypto</code></a> property is <code>undefined</code> or
|
||||
<code>true</code>, and the <code>crypto</code> object is available in the host environment,
|
||||
the random digits of the return value are generated by either
|
||||
<code>crypto.getRandomValues</code> (Web Cryptography API in modern browsers) or
|
||||
<code>crypto.randomBytes</code> (Node.js), otherwise, if the the value of the property is
|
||||
<code>false</code>, or the <code>crypto</code> object is not available, the return value is
|
||||
generated by <code>Math.random</code> (fastest).
|
||||
<a href='#crypto'><code>crypto</code></a> property is <code>true</code>, and the
|
||||
<code>crypto</code> object is available globally in the host environment, the random digits of
|
||||
the return value are generated by either <code>crypto.getRandomValues</code> (Web Cryptography
|
||||
API in modern browsers) or <code>crypto.randomBytes</code> (Node.js), otherwise, if the the
|
||||
value of the property is <code>false</code> the return value is generated by
|
||||
<code>Math.random</code> (fastest).
|
||||
</p>
|
||||
<p>To make the <code>crypto</code> object available globally in Node.js use</p>
|
||||
<pre>global.crypto = require('crypto')</pre>
|
||||
<p>
|
||||
If the value of <code>this</code> Decimal constructor's
|
||||
<a href='#crypto'><code>crypto</code></a> property is set <code>true</code> and the
|
||||
@ -744,12 +729,11 @@ a.equals(b) // true</pre>
|
||||
If one of the <code>crypto</code> methods is used, the value of the returned Decimal should be
|
||||
cryptographically-secure and statistically indistinguishable from a random value.
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 10 })
|
||||
<pre>Decimal.set({ precision: 10 })
|
||||
Decimal.random() // '0.4117936847'
|
||||
Decimal.random(20) // '0.78193327636914089009'</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dround">round<code class='inset'>.round(x) <i>⇒ Decimal</i></code></h5>
|
||||
<p><code>x</code>: <i>number|string|Decimal</i></p>
|
||||
<p>See <code><a href='#round'>round</a></code>.</p>
|
||||
@ -759,6 +743,54 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dset">set<code class='inset'>.set(object) <i>⇒ Decimal constructor</i></code></h5>
|
||||
<p><code>object</code>: <i>object</i></p>
|
||||
<p>
|
||||
Configures the 'global' settings for <code>this</code> particular Decimal constructor, i.e.
|
||||
the settings which apply to operations performed on the Decimal instances created by it.
|
||||
</p>
|
||||
<p>Returns <code>this</code> Decimal constructor.</p>
|
||||
<p>
|
||||
The configuration object, <code>object</code>, can contain some or all of the properties
|
||||
described in detail at <a href="#constructor-properties">Properties</a> and shown in the
|
||||
example below.
|
||||
</p>
|
||||
<p>
|
||||
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
|
||||
Decimal.set({
|
||||
precision: 20,
|
||||
rounding: 4,
|
||||
toExpNeg: -7,
|
||||
toExpPos: 21,
|
||||
maxE: 9e15,
|
||||
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>
|
||||
<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
|
||||
knows that the assignment is valid.
|
||||
</p>
|
||||
<pre>Decimal.precision = 40</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dsign">sign<code class='inset'>.sign(x) <i>⇒ number</i></code></h5>
|
||||
<p><code>x</code>: <i>number|string|Decimal</i></p>
|
||||
<table>
|
||||
@ -811,11 +843,11 @@ a.equals(b) // true</pre>
|
||||
<p>See <a href='#sqrt'>squareRoot</a>.</p>
|
||||
<pre>a = Decimal.sqrt(x)
|
||||
b = new Decimal(x).sqrt()
|
||||
a.equals(b) // true</pre>
|
||||
a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>⇒ Decimal</i></code></h5>
|
||||
<h5 id="Dsub">sub<code class='inset'>.sub(x, y) <i>⇒ Decimal</i></code></h5>
|
||||
<p>
|
||||
<code>x</code>: <i>number|string|Decimal</i><br />
|
||||
<code>y</code>: <i>number|string|Decimal</i>
|
||||
@ -827,7 +859,26 @@ a.equals(b) // true</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="Dtan">tan<code class='inset'>.tan(x) <i>⇒ Decimal</i></code></h5>
|
||||
<h5 id="Dsum">sum<code class='inset'>.sum(x [, y, ...]) <i>⇒ 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>⇒ 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)
|
||||
@ -867,14 +918,14 @@ a.equals(b) // true</pre>
|
||||
<a href='#maxE'><code>maxE</code></a>, <a href='#toExpNeg'><code>toExpNeg</code></a>,
|
||||
<a href='#toExpPos'><code>toExpPos</code></a>, <a href='#modulo'><code>modulo</code></a>, and
|
||||
<a href='#crypto'><code>crypto</code></a> are set using the
|
||||
<a href='#Dconfig'><code>config</code></a> method.
|
||||
<a href='#Dset'><code>set</code></a> method.
|
||||
</p>
|
||||
<p>
|
||||
As simple object properties they can be set directly without using
|
||||
<a href='#Dconfig'><code>config</code></a>, and it is fine to do so, but the values assigned
|
||||
<a href='#Dset'><code>set</code></a>, and it is fine to do so, but the values assigned
|
||||
will not then be checked for validity. For example:
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 0 })
|
||||
<pre>Decimal.set({ precision: 0 })
|
||||
// '[DecimalError] Invalid argument: precision: 0'
|
||||
|
||||
Decimal.precision = 0
|
||||
@ -892,13 +943,19 @@ Decimal.precision = 0
|
||||
All functions which return a Decimal will round the return value to <code>precision</code>
|
||||
significant digits except <a href='#decimal'><code>Decimal</code></a>,
|
||||
<a href='#abs'><code>absoluteValue</code></a>,
|
||||
<a href='#ceil'><code>ceil</code></a>, <a href='#floor'><code>floor</code></a>,
|
||||
<a href='#neg'><code>negated</code></a>, <a href='#round'><code>round</code></a>,
|
||||
<a href='#ceil'><code>ceil</code></a>,
|
||||
<a href='#clamp'><code>clampedTo</code></a>,
|
||||
<a href='#floor'><code>floor</code></a>,
|
||||
<a href='#neg'><code>negated</code></a>,
|
||||
<a href='#round'><code>round</code></a>,
|
||||
<a href='#toDP'><code>toDecimalPlaces</code></a>,
|
||||
<a href='#toNearest'><code>toNearest</code></a> and
|
||||
<a href='#trunc'><code>truncated</code></a>.
|
||||
</p>
|
||||
<pre>Decimal.config({ precision: 5 })
|
||||
<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>
|
||||
|
||||
|
||||
@ -906,7 +963,7 @@ Decimal.precision // 5</pre>
|
||||
<h5 id="rounding">rounding</h5>
|
||||
<p>
|
||||
<i>number</i>: integer, <code>0</code> to <code>8</code> inclusive<br />
|
||||
Default value: <code>4</code> <a href="#h-up">(<code>ROUND_HALF_UP</code>)</a>
|
||||
Default value: <code>4</code> <a href="#modes">(<code>ROUND_HALF_UP</code>)</a>
|
||||
</p>
|
||||
<p>
|
||||
The default rounding mode used when rounding the result of an operation to
|
||||
@ -926,8 +983,8 @@ Decimal.precision // 5</pre>
|
||||
The <a href='#modes'>rounding modes</a> are available as enumerated properties of the
|
||||
constructor.
|
||||
</p>
|
||||
<pre>Decimal.config({ rounding: Decimal.ROUND_UP })
|
||||
Decimal.config({ rounding: 0 }) // equivalent
|
||||
<pre>Decimal.set({ rounding: Decimal.ROUND_UP })
|
||||
Decimal.set({ rounding: 0 }) // equivalent
|
||||
Decimal.rounding // 0</pre>
|
||||
|
||||
|
||||
@ -946,13 +1003,13 @@ Decimal.rounding // 0</pre>
|
||||
<p>
|
||||
JavaScript numbers underflow to zero for exponents below <code>-324</code>.
|
||||
</p>
|
||||
<pre>Decimal.config({ minE: -500 })
|
||||
<pre>Decimal.set({ minE: -500 })
|
||||
Decimal.minE // -500
|
||||
new Decimal('1e-500') // '1e-500'
|
||||
new Decimal('9.9e-501') // '0'
|
||||
|
||||
Decimal.config({ minE: -3 })
|
||||
new Decimal(0.001) // '0.01' e is -3
|
||||
Decimal.set({ minE: -3 })
|
||||
new Decimal(0.001) // '0.001' 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>
|
||||
@ -975,12 +1032,12 @@ new Decimal(0.0001) // '0' e is -4</pre>
|
||||
<p>
|
||||
JavaScript numbers overflow to <code>Infinity</code> for exponents above <code>308</code>.
|
||||
</p>
|
||||
<pre>Decimal.config({ maxE: 500 })
|
||||
<pre>Decimal.set({ maxE: 500 })
|
||||
Decimal.maxE // 500
|
||||
new Decimal('9.999e500') // '9.999e+500'
|
||||
new Decimal('1e501') // 'Infinity'
|
||||
|
||||
Decimal.config({ maxE: 4 })
|
||||
Decimal.set({ maxE: 4 })
|
||||
new Decimal(99999) // '99999' e is 4
|
||||
new Decimal(100000) // 'Infinity'</pre>
|
||||
<p>
|
||||
@ -998,13 +1055,13 @@ new Decimal(100000) // 'Infinity'</pre>
|
||||
The negative exponent value at and below which <a href='#toString'><code>toString</code></a>
|
||||
returns exponential notation.
|
||||
</p>
|
||||
<pre>Decimal.config({ toExpNeg: -7 })
|
||||
<pre>Decimal.set({ toExpNeg: -7 })
|
||||
Decimal.toExpNeg // -7
|
||||
new Decimal(0.00000123) // '0.00000123' e is -6
|
||||
new Decimal(0.000000123) // '1.23e-7'
|
||||
|
||||
// Always return exponential notation:
|
||||
Decimal.config({ toExpNeg: 0 })</pre>
|
||||
Decimal.set({ toExpNeg: 0 })</pre>
|
||||
<p>
|
||||
JavaScript numbers use exponential notation for negative exponents of <code>-7</code> and
|
||||
below.
|
||||
@ -1027,13 +1084,13 @@ Decimal.config({ toExpNeg: 0 })</pre>
|
||||
The positive exponent value at and above which <a href='#toString'><code>toString</code></a>
|
||||
returns exponential notation.
|
||||
</p>
|
||||
<pre>Decimal.config({ toExpPos: 2 })
|
||||
<pre>Decimal.set({ toExpPos: 2 })
|
||||
Decimal.toExpPos // 2
|
||||
new Decimal(12.3) // '12.3' e is 1
|
||||
new Decimal(123) // '1.23e+2'
|
||||
|
||||
// Always return exponential notation:
|
||||
Decimal.config({ toExpPos: 0 })</pre>
|
||||
Decimal.set({ toExpPos: 0 })</pre>
|
||||
<p>
|
||||
JavaScript numbers use exponential notation for positive exponents of <code>20</code> and
|
||||
above.
|
||||
@ -1100,23 +1157,27 @@ Decimal.config({ toExpPos: 0 })</pre>
|
||||
<p>
|
||||
The rounding/modulo modes are available as enumerated properties of the Decimal constructor.
|
||||
</p>
|
||||
<pre>Decimal.config({ modulo: Decimal.EUCLID })
|
||||
Decimal.config({ modulo: 9 }) // equivalent
|
||||
<pre>Decimal.set({ modulo: Decimal.EUCLID })
|
||||
Decimal.set({ modulo: 9 }) // equivalent
|
||||
Decimal.modulo // 9</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="crypto">crypto</h5>
|
||||
<p>
|
||||
<i>boolean</i>: <code>true/false/undefined</code><br /> Default value: <code>undefined</code>
|
||||
<i>boolean</i>: <code>true/false</code><br /> Default value: <code>false</code>
|
||||
</p>
|
||||
<p>
|
||||
The value that determines whether cryptographically-secure pseudo-random number generation is
|
||||
used.
|
||||
</p>
|
||||
<p>See <a href='#Drandom'><code>random</code></a>.</p>
|
||||
<pre>Decimal.crypto // undefined
|
||||
Decimal.config({ crypto: true })
|
||||
<pre>
|
||||
// Node.js
|
||||
global.crypto = require('crypto')
|
||||
|
||||
Decimal.crypto // false
|
||||
Decimal.set({ crypto: true })
|
||||
Decimal.crypto // true</pre>
|
||||
|
||||
|
||||
@ -1160,8 +1221,8 @@ Decimal.crypto // true</pre>
|
||||
<td>Not a rounding mode, see <a href='#modulo'>modulo</a></td>
|
||||
</tr>
|
||||
</table>
|
||||
<pre>Decimal.config({ rounding: Decimal.ROUND_CEIL })
|
||||
Decimal.config({ rounding: 2 }) // equivalent
|
||||
<pre>Decimal.set({ rounding: Decimal.ROUND_CEIL })
|
||||
Decimal.set({ rounding: 2 }) // equivalent
|
||||
Decimal.rounding // 2</pre>
|
||||
|
||||
|
||||
@ -1219,6 +1280,28 @@ y.ceil() // '-1'</pre>
|
||||
|
||||
|
||||
|
||||
<h5 id="clamp">clampedTo<code class='inset'>.clamp(min, max) <i>⇒ 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>⇒ number</i></code></h5>
|
||||
<p><code>x</code>: <i>number|string|Decimal</i></p>
|
||||
<table>
|
||||
@ -1236,7 +1319,7 @@ y.ceil() // '-1'</pre>
|
||||
<td>if this Decimal and <code>x</code> have the same value</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class='centre'><code>null</code></td>
|
||||
<td class='centre'><code>NaN</code></td>
|
||||
<td>if the value of either this Decimal or <code>x</code> is <code>NaN</code> </td>
|
||||
</tr>
|
||||
</table>
|
||||
@ -1245,8 +1328,7 @@ x = new Decimal(Infinity)
|
||||
y = new Decimal(5)
|
||||
x.comparedTo(y) // 1
|
||||
x.comparedTo(x.minus(1)) // 0
|
||||
y.cmp(NaN) // null
|
||||
y.cmp('110', 2) // -1</pre>
|
||||
y.cmp(NaN) // NaN</pre>
|
||||
|
||||
|
||||
|
||||
@ -1260,7 +1342,7 @@ y.cmp('110', 2) // -1</pre>
|
||||
Domain: [<code>-Infinity, Infinity</code>]<br />
|
||||
Range: [<code>-1, 1</code>]
|
||||
</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(0.25)
|
||||
x.cosine() // '0.96891242171064478414'
|
||||
@ -1410,7 +1492,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(1)
|
||||
x.hyperbolicCosine() // '1.5430806348152437785'
|
||||
@ -1429,7 +1511,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(1)
|
||||
x.hyperbolicSine() // '1.1752011936438014569'
|
||||
@ -1448,7 +1530,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(1)
|
||||
x.hyperbolicTangent() // '0.76159415595576488812'
|
||||
@ -1467,7 +1549,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(0)
|
||||
x.inverseCosine() // '1.5707963267948966192'
|
||||
@ -1488,7 +1570,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(5)
|
||||
x.inverseHyperbolicCosine() // '2.2924316695611776878'
|
||||
@ -1509,7 +1591,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(5)
|
||||
x.inverseHyperbolicSine() // '2.3124383412727526203'
|
||||
@ -1530,7 +1612,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(0.5)
|
||||
x.inverseHyperbolicTangent() // '0.5493061443340548457'
|
||||
@ -1549,7 +1631,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(0.5)
|
||||
x.inverseSine() // '0.52359877559829887308'
|
||||
@ -1568,7 +1650,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(0.5)
|
||||
x.inverseTangent() // '0.46364760900080611621'
|
||||
@ -1589,10 +1671,6 @@ x = new Decimal(1)
|
||||
x.isFinite() // true
|
||||
y = new Decimal(Infinity)
|
||||
y.isFinite() // false</pre>
|
||||
<p>
|
||||
Note: The native method <code>isFinite()</code> can be used if
|
||||
<code>n <= Number.MAX_VALUE</code>.
|
||||
</p>
|
||||
|
||||
|
||||
|
||||
@ -1619,7 +1697,6 @@ x = new Decimal(NaN)
|
||||
x.isNaN() // true
|
||||
y = new Decimal('Infinity')
|
||||
y.isNaN() // false</pre>
|
||||
<p>Note: The native method <code>isNaN()</code> can also be used.</p>
|
||||
|
||||
|
||||
|
||||
@ -1633,7 +1710,14 @@ x = new Decimal(-0)
|
||||
x.isNegative() // true
|
||||
y = new Decimal(2)
|
||||
y.isNeg // false</pre>
|
||||
<p>Note: <code>n < 0</code> can be used if <code>n <= -Number.MIN_VALUE</code>.</p>
|
||||
<p>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>
|
||||
|
||||
|
||||
|
||||
@ -1647,7 +1731,6 @@ x = new Decimal(0)
|
||||
x.isPositive() // true
|
||||
y = new Decimal(-2)
|
||||
y.isPos // false</pre>
|
||||
<p>Note: <code>n < 0</code> can be used if <code>n <= -Number.MIN_VALUE</code>.</p>
|
||||
|
||||
|
||||
|
||||
@ -1661,7 +1744,6 @@ x = new Decimal(-0)
|
||||
x.isZero() && x.isNeg() // true
|
||||
y = new Decimal(Infinity)
|
||||
y.isZero() // false</pre>
|
||||
<p>Note: <code>n == 0</code> can be used if <code>n >= Number.MIN_VALUE</code>.</p>
|
||||
|
||||
|
||||
|
||||
@ -1720,7 +1802,8 @@ 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.
|
||||
return an incorrectly rounded result, and see <a href='#ln'><code>naturalLogarithm</code></a>
|
||||
for the precision limit.
|
||||
</p>
|
||||
<p>The performance of this method degrades exponentially with increasing digits.</p>
|
||||
|
||||
@ -1887,7 +1970,7 @@ y.sd(true) // '6'</pre>
|
||||
<code>7</code>, i.e. <a href='#modes'><code>ROUND_HALF_CEIL</code></a>.
|
||||
</p>
|
||||
<pre>
|
||||
Decimal.config({ rounding: 4 })
|
||||
Decimal.set({ rounding: 4 })
|
||||
x = 1234.5
|
||||
x.round() // '1235'
|
||||
|
||||
@ -1907,7 +1990,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(0.5)
|
||||
x.sine() // '0.47942553860420300027'
|
||||
@ -1949,7 +2032,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>.</p>
|
||||
<p>See <a href='#Pi'><code>Pi</code></a> for the precision limit of this method.</p>
|
||||
<pre>
|
||||
x = new Decimal(0.5)
|
||||
x.tangent() // '0.54630248984379051326'
|
||||
@ -2023,9 +2106,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.24567)
|
||||
x.toDecimalPlaces(0) // '12'
|
||||
x.toDecimalPlaces(1, 0) // '12.3'
|
||||
x = new Decimal(12.34567)
|
||||
x.toDecimalPlaces(0) // '12'
|
||||
x.toDecimalPlaces(1, Decimal.ROUND_UP) // '12.4'
|
||||
|
||||
y = new Decimal(9876.54321)
|
||||
y.toDP(3) // '9876.543'
|
||||
@ -2061,16 +2144,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
|
||||
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>
|
||||
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>
|
||||
|
||||
|
||||
|
||||
@ -2109,15 +2192,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
|
||||
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>
|
||||
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>
|
||||
|
||||
|
||||
|
||||
@ -2184,69 +2267,21 @@ x.toHex(1) // '0x1p+8'</pre>
|
||||
|
||||
|
||||
<h5 id="toJSON">toJSON<code class='inset'>.toJSON() <i>⇒ string</i></code></h5>
|
||||
<p>
|
||||
Returns a string representing the exact value of this Decimal in a compact base-88 based
|
||||
format.
|
||||
</p>
|
||||
<p>
|
||||
A Decimal instance with the same value as this Decimal can be created by passing the return
|
||||
value to the <code><a href='#fromJSON'>fromJSON</a></code> method of a Decimal constructor.
|
||||
</p>
|
||||
<p>
|
||||
The number of characters of the return value will always be equal to or less than the number
|
||||
of characters returned by <a href='#toString'><code>toString</code></a> - usually just over
|
||||
half as many.
|
||||
</p>
|
||||
<p>
|
||||
The 7 printable ASCII characters, <code>(space) \ " & ' < ></code>, are not used in
|
||||
the return value, so it should be safe for inclusion in HTML, JSON and XML.
|
||||
</p>
|
||||
<pre>
|
||||
x = new Decimal('177.7e+457')
|
||||
x.toJSON() // '25jkh'
|
||||
y = new Decimal(235.4325)
|
||||
y.toJSON() // '/3E1Z'
|
||||
z = new Decimal('0.0098074')
|
||||
z.toJSON() // '*cWG'
|
||||
|
||||
// Serialize an array of three Decimals
|
||||
str = JSON.stringify( [x, y, z] ) // "["25jkh","/3E1Z","*cWG"]"
|
||||
|
||||
// Return an array of three Decimals
|
||||
JSON.parse(str, function (key, val) {
|
||||
return key === '' ? val : Decimal.fromJSON(val)
|
||||
})</pre>
|
||||
<p>If the <code>toJSON</code> method was not present, deserialization would be difficult as the
|
||||
array would be serialized as:</p>
|
||||
<pre>
|
||||
/*
|
||||
"[{"s":1,"e":459,"c":[17770]},
|
||||
{"s":1,"e":2,"c":[235,4325000]},
|
||||
{"s":1,"e":-3,"c":[98074]}]"
|
||||
*/</pre>
|
||||
<p>As <a href='#valueOf'><code>valueOf</code></a>.</p>
|
||||
|
||||
|
||||
|
||||
<h5 id="toNearest">
|
||||
toNearest<code class='inset'>.toNearest(n [, rm]) <i>⇒ Decimal</i></code>
|
||||
toNearest<code class='inset'>.toNearest(x [, rm]) <i>⇒ 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> 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.
|
||||
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.
|
||||
</p>
|
||||
<p>
|
||||
The return value will always have the same sign as this Decimal, unless either this Decimal
|
||||
@ -2259,11 +2294,11 @@ JSON.parse(str, function (key, val) {
|
||||
</p>
|
||||
<pre>
|
||||
x = new Decimal(1.39)
|
||||
x.toNearest(0.25) // '1.5'
|
||||
x.toNearest(0.25) // '1.5'
|
||||
|
||||
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>
|
||||
y = new Decimal(9.499)
|
||||
y.toNearest(0.5, Decimal.ROUND_UP) // '9.5'
|
||||
y.toNearest(0.5, Decimal.ROUND_DOWN) // '9'</pre>
|
||||
|
||||
|
||||
|
||||
@ -2367,7 +2402,7 @@ new Decimal(1217652.23).pow('98765.489305603941')
|
||||
An example of incorrect rounding:
|
||||
</p>
|
||||
<pre>
|
||||
Decimal.config({ precision: 20, rounding: 1 })
|
||||
Decimal.set({ precision: 20, rounding: 1 })
|
||||
new Decimal(28).pow('6.166675020000903537297764507632802193308677149')
|
||||
// 839756321.64088511</pre>
|
||||
<p>As the exact mathematical result begins</p>
|
||||
@ -2406,13 +2441,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
|
||||
b = new Decimal(x)
|
||||
y = new Decimal(x)
|
||||
x.toPrecision() // '45.6'
|
||||
y.toPrecision() // '45.6'
|
||||
x.toPrecision(1) // '5e+1'
|
||||
y.toPrecision(1) // '5e+1'
|
||||
y.toPrecision(2, 0) // '4.6e+1' (ROUND_UP)
|
||||
y.toPrecision(2, 1) // '4.5e+1' (ROUND_DOWN)
|
||||
y.toPrecision(2, Decimal.ROUND_UP) // '46'
|
||||
y.toPrecision(2, Decimal.ROUND_DOWN) // '45'
|
||||
x.toPrecision(5) // '45.600'
|
||||
y.toPrecision(5) // '45.600'</pre>
|
||||
|
||||
@ -2439,7 +2474,7 @@ y.toPrecision(5) // '45.600'</pre>
|
||||
</p>
|
||||
<p>Throws on an invalid <code>sd</code> or <code>rm</code> value.</p>
|
||||
<pre>
|
||||
Decimal.config({ precision: 5, rounding: 4 })
|
||||
Decimal.set({ precision: 5, rounding: 4 })
|
||||
x = new Decimal(9876.54321)
|
||||
|
||||
x.toSignificantDigits() // '9876.5'
|
||||
@ -2461,10 +2496,10 @@ x // '9876.54321'</pre>
|
||||
<pre>
|
||||
x = new Decimal(750000)
|
||||
x.toString() // '750000'
|
||||
Decimal.config({ toExpPos: 5 })
|
||||
Decimal.set({ toExpPos: 5 })
|
||||
x.toString() // '7.5e+5'
|
||||
|
||||
Decimal.config({ precision: 4 });
|
||||
Decimal.set({ precision: 4 })
|
||||
y = new Decimal('1.23456789')
|
||||
y.toString() // '1.23456789'</pre>
|
||||
|
||||
@ -2531,9 +2566,9 @@ x.valueOf() // '-0'</pre>
|
||||
<td><code>-1</code>, <code>1</code>, or <code>NaN</code></td>
|
||||
</tr>
|
||||
</table>
|
||||
<p>The properties are best considered to be read-only.</p>
|
||||
<p>All the properties are best considered to be read-only.</p>
|
||||
<p>
|
||||
As with JavaScript numbers, the original exponent and fractional trailing zeros of a number
|
||||
As with JavaScript numbers, the original exponent and fractional trailing zeros of a value
|
||||
are not preserved.
|
||||
</p>
|
||||
<pre>
|
||||
@ -2621,13 +2656,27 @@ try {
|
||||
</p>
|
||||
<p>
|
||||
It has a precision of <code>1025</code> digits, meaning that the trigonometric methods
|
||||
can accurately calculate up to at least <code>1000</code> digits.
|
||||
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>.
|
||||
</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.
|
||||
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.
|
||||
</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>
|
||||
|
File diff suppressed because one or more lines are too long
32
package.json
32
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "decimal.js",
|
||||
"description": "An arbitrary-precision Decimal type for JavaScript.",
|
||||
"version": "5.0.3",
|
||||
"version": "10.4.3",
|
||||
"keywords": [
|
||||
"arbitrary",
|
||||
"precision",
|
||||
@ -21,13 +21,35 @@
|
||||
"url": "https://github.com/MikeMcl/decimal.js.git"
|
||||
},
|
||||
"main": "decimal",
|
||||
"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",
|
||||
"build": "uglifyjs decimal.js --source-map doc/decimal.js.map -c -m -o decimal.min.js --preamble \"/* decimal.js v5.0.3 https://github.com/MikeMcl/decimal.js/LICENCE */\""
|
||||
}
|
||||
}
|
||||
"test": "node ./test/test.js"
|
||||
},
|
||||
"types": "decimal.d.ts",
|
||||
"files": [
|
||||
"decimal.js",
|
||||
"decimal.mjs",
|
||||
"decimal.d.ts"
|
||||
]
|
||||
}
|
||||
|
@ -124,6 +124,7 @@ 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');
|
||||
|
||||
@ -139,6 +140,7 @@ 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');
|
||||
|
||||
@ -146,6 +148,7 @@ 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');
|
||||
|
||||
@ -156,14 +159,18 @@ 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.'");
|
||||
|
||||
@ -171,11 +178,12 @@ 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.'");
|
||||
@ -197,7 +205,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. '");
|
||||
@ -222,12 +230,16 @@ 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 '");
|
||||
@ -235,8 +247,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'");
|
||||
@ -262,8 +274,9 @@ 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'");
|
||||
@ -282,7 +295,9 @@ 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'");
|
||||
});
|
||||
|
45
test/modules/clamp.js
Normal file
45
test/modules/clamp.js
Normal file
@ -0,0 +1,45 @@
|
||||
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');
|
||||
});
|
@ -105,5 +105,40 @@ 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);
|
||||
});
|
||||
|
||||
|
@ -61,7 +61,7 @@ T('config', function () {
|
||||
toExpPos: 1000,
|
||||
minE: -1e9,
|
||||
maxE: 1e9,
|
||||
crypto: true,
|
||||
//crypto: true, // requires crypto object
|
||||
modulo: 4
|
||||
});
|
||||
|
||||
@ -71,7 +71,7 @@ T('config', function () {
|
||||
t(Decimal.toExpPos === 1000);
|
||||
t(Decimal.minE === -1e9);
|
||||
t(Decimal.maxE === 1e9);
|
||||
t(Decimal.crypto === true || Decimal.crypto === false);
|
||||
//t(Decimal.crypto === true); // requires crypto object
|
||||
t(Decimal.modulo === 4);
|
||||
|
||||
Decimal.config({
|
||||
@ -277,15 +277,11 @@ T('config', function () {
|
||||
T.assertEqual(expected, Decimal.crypto);
|
||||
}
|
||||
|
||||
// crypto was set true above, but it will have remained false if
|
||||
// there is no crypto.getRandomValues or crypto.randomBytes.
|
||||
var cryptoVal = Decimal.crypto;
|
||||
|
||||
t(void 0, {crypto: void 0});
|
||||
t(false, {crypto: void 0});
|
||||
t(false, {crypto: 0});
|
||||
t(cryptoVal, {crypto: 1});
|
||||
//t(true, {crypto: 1}); // requires crypto object
|
||||
t(false, {crypto: false});
|
||||
t(cryptoVal, {crypto: true});
|
||||
//t(true, {crypto: true}); // requires crypto object
|
||||
|
||||
tx(function () {Decimal.config({crypto: 'hiya'})}, "crypto: 'hiya'");
|
||||
tx(function () {Decimal.config({crypto: 'true'})}, "crypto: 'true'");
|
||||
@ -301,7 +297,7 @@ T('config', function () {
|
||||
tx(function () {Decimal.config({crypto: NaN})}, "crypto: NaN");
|
||||
tx(function () {Decimal.config({crypto: Infinity})}, "crypto: Infinity");
|
||||
|
||||
t(void 0, {crypto: void 0});
|
||||
T.assertEqual(false, Decimal.crypto);
|
||||
|
||||
// modulo
|
||||
|
||||
@ -341,4 +337,37 @@ T('config', function () {
|
||||
tx(function () {Decimal.config({modulo: Infinity})}, "modulo: Infinity");
|
||||
|
||||
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);
|
||||
});
|
||||
|
@ -1,43 +0,0 @@
|
||||
if (typeof T === 'undefined') require('../setup');
|
||||
|
||||
T('fromJSON', function () {
|
||||
|
||||
Decimal.config({
|
||||
precision: Math.random() * 40 + 1 | 0,
|
||||
rounding: Math.random() * 9 | 0,
|
||||
toExpNeg: -7,
|
||||
toExpPos: 21,
|
||||
minE: -9e15,
|
||||
maxE: 9e15
|
||||
});
|
||||
|
||||
var e, fromJ, i, r, toJ;
|
||||
var maxDigits = 100;
|
||||
|
||||
for ( i = 0; i < 100000; ) {
|
||||
|
||||
// Get a random value in the range [0,1) with a random number of significant digits
|
||||
// in the range [1, maxDigits], as a string in exponential format.
|
||||
e = Decimal.random( Math.random() * maxDigits + 1 | 0 ).toExponential();
|
||||
|
||||
// Change exponent to a non-zero value of random length in the range (-9e15, 9e15).
|
||||
r = new Decimal(e.slice(0, e.indexOf('e') + 1) + ( Math.random() < 0.5 ? '-' : '' ) +
|
||||
( n = Math.floor( Math.random() * 9e15 ) + '' ).slice( Math.random() * n.length | 0 ));
|
||||
//console.log(r.toString());
|
||||
|
||||
toJ = r.toJSON();
|
||||
//console.log(' toJSON: ' + toJ);
|
||||
|
||||
fromJ = Decimal.fromJSON(toJ);
|
||||
//console.log(' fromJSON: ' + fromJ);
|
||||
|
||||
/*
|
||||
if (!r.eq(fromJ)) {
|
||||
console.error(' r: ' + r);
|
||||
console.error(' toJSON: ' + toJ);
|
||||
console.error(' fromJSON: ' + fromJ);
|
||||
}
|
||||
*/
|
||||
T.assert(r.eq(fromJ));
|
||||
}
|
||||
});
|
@ -552,7 +552,6 @@ T('immutability', function () {
|
||||
// All methods tested above except:
|
||||
Decimal.clone();
|
||||
Decimal.config();
|
||||
Decimal.fromJSON();
|
||||
Decimal.noConflict();
|
||||
Decimal.random();
|
||||
*/
|
||||
|
@ -1,6 +1,6 @@
|
||||
if (typeof T === 'undefined') require('../setup');
|
||||
|
||||
T('isFinite, isInteger, isNaN, isNegative, isZero', function () {
|
||||
T('isFinite, isInteger, isNaN, isNegative, isZero, isDecimal', function () {
|
||||
|
||||
function t(actual) {
|
||||
T.assert(actual);
|
||||
@ -257,4 +257,22 @@ T('isFinite, isInteger, isNaN, isNegative, isZero', 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)));
|
||||
});
|
||||
|
@ -43,7 +43,6 @@ T('ln', function () {
|
||||
t('96363417.9729392663575231665', '18.3836372059508935007674191717', 30, 0);
|
||||
t('210401674.5643546532430157', '19.1645289977522148145584372801492541489626022147443118341', 57, 3);
|
||||
t('952417276.8651321118737418', '20.67451381275802954644121920485653995422386303532469652920020456933991073071', 76, 3);
|
||||
t('98184005.146079977', '18.4023538796720926822257754284842101571428491604633542324254915295389687459768262219847229005105726462911110014778700105379820806407462050611141071631637279703966033404550534991584986282367905158391520156932087898507160577425969648962582513228749121815449650331654608429478028655815089', 285, 6);
|
||||
|
||||
t('142322563.253900033190831713543268828109981967888490291406527346535501984011990242137300064766425179727442507442591862989037644368590904104621357145575632132596634560836335843482822061685451360570260600926717066081883739008412790434377893984866700040372917562269733068809727985812968781985268403920729907489962116060023323078359102582924017315196797171078484331048743611378544575282691634826207', '18.77360661165643483904346', 25, 4);
|
||||
t('371400293.53826507744653292410472631048281764405036030167939649508120465472946573115185753424640793913438158589233749086894500415028950753895660006551599374916249772634013307822672548085573611750200419176163913242813545244921337312493040312898991903677477383181667469789752847529551033664230833747974881304800992388176411266053764', '19.732790997238693', 17, 5);
|
||||
|
@ -109,6 +109,11 @@ T('pow', function () {
|
||||
t('8.97', '-1', '0.111482720178', 12, 3);
|
||||
t('61766796871807246.3278075', '-1', '0.00000000000000001618993', 7, 0);
|
||||
|
||||
t('-1', '101', '-1', 100, 1);
|
||||
t('-1', '9999999999999999999999999999999999999999999999999999999999999999999999999', '-1', 100, 1);
|
||||
t('-1', '1e307', '1', 100, 1);
|
||||
t('-1', '1e309', '1', 100, 1);
|
||||
|
||||
Decimal.toExpNeg = Decimal.toExpPos = 0;
|
||||
|
||||
t('9.9999999999999', '2220.75', '5.623413251778e+2220', 13, 1);
|
||||
@ -126,4 +131,9 @@ T('pow', function () {
|
||||
t('908948247.896330216349750387912923575076135766138', '11.38907521122213262858256836', '1.0702278292293091784680297675223031e+102', 35, 3);
|
||||
t('4.485925762349120387154391E+47', '1677945.16766265206929939', '8.53959030215133943e+79957194', 18, 5);
|
||||
t('2.8448989811706207675566E+89', '2.368592228588521845032068137267440272102614', '7.58940197453762187722508511706932e+211', 33, 5);
|
||||
|
||||
t('0.9999999999999999', '-1e+30', '1.530863912e+43429448190325', 10, 1);
|
||||
t('0.9999999999999999999999999999999999999999999999999', '-1e+32', '1.00000000000000001000000000000000005e+0', 36, 1);
|
||||
t('0.9999999999999999', '-1e+50', 'Infinity', 40, 1);
|
||||
t('0.9999999999999999999999999999999899999999999999994403269002375809806554775739676251993670310626872684', '-1.49181945463118148622657269735650603014891811120124843379694396257337810020127409048127397077199569e+271', 'Infinity', 100, 1);
|
||||
});
|
||||
|
64
test/modules/sum.js
Normal file
64
test/modules/sum.js
Normal file
@ -0,0 +1,64 @@
|
||||
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);
|
||||
});
|
@ -1,90 +0,0 @@
|
||||
if (typeof T === 'undefined') require('../setup');
|
||||
|
||||
T('toJSON', function () {
|
||||
|
||||
function t(n, expected) {
|
||||
T.assertEqual(expected, new Decimal(n).toJSON());
|
||||
}
|
||||
|
||||
Decimal.config({
|
||||
toExpNeg: -9e15,
|
||||
toExpPos: 9e15,
|
||||
minE: -9e15,
|
||||
maxE: 9e15
|
||||
});
|
||||
|
||||
// Base 88
|
||||
// 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%()*+,-./:;=?@[]^_`{|}~
|
||||
|
||||
// 0 0 g 16 w 32 M 48 $ 64 ] 80
|
||||
// 1 1 h 17 x 33 N 49 % 65 ^ 81
|
||||
// 2 2 i 18 y 34 O 50 ( 66 _ 82
|
||||
// 3 3 j 19 z 35 P 51 ) 67 ` 83
|
||||
// 4 4 k 20 A 36 Q 52 * 68 { 84
|
||||
// 5 5 l 21 B 37 R 53 + 69 | 85
|
||||
// 6 6 m 22 C 38 S 54 , 70 } 86
|
||||
// 7 7 n 23 D 39 T 55 - 71 ~ 87
|
||||
// 8 8 o 24 E 40 U 56 . 72
|
||||
// 9 9 p 25 F 41 V 57 / 73
|
||||
// a 10 q 26 G 42 W 58 : 74
|
||||
// b 11 r 27 H 43 X 59 ; 75
|
||||
// c 12 s 28 I 44 Y 60 = 76
|
||||
// d 13 t 29 J 45 Z 61 ? 77
|
||||
// e 14 u 30 K 46 ! 62 @ 78
|
||||
// f 15 v 31 L 47 # 63 [ 79
|
||||
|
||||
// 0123456789abcdefghijklmnopqrstuvwxyzABCDE [0, 40]
|
||||
// FGHIJKLMNOPQRSTUVWXYZ!#$%()*+,-./:;=?@[]^ [-0, -40]
|
||||
// _ 82 -Infinity
|
||||
// ` 83 Infinity
|
||||
// { 84 NaN
|
||||
// | 85
|
||||
// } 86
|
||||
// ~ 87
|
||||
|
||||
t('-40', '^');
|
||||
t('-1', 'G');
|
||||
t('-0', 'F');
|
||||
t('0', '0');
|
||||
t('1', '1');
|
||||
t('15', 'f');
|
||||
t('40', 'E');
|
||||
|
||||
t('-Infinity', '_');
|
||||
t('Infinity', '`');
|
||||
t('NaN', '{');
|
||||
|
||||
t('-41', 'w0');
|
||||
t('41', '00');
|
||||
t('-2856', '#~');
|
||||
t('2856', 'v~');
|
||||
|
||||
t('0.1', ',1');
|
||||
t('0.01', '+1');
|
||||
t('0.001', '*1');
|
||||
t('0.0001', ')1');
|
||||
t('1', '1');
|
||||
t('10', 'a');
|
||||
t('100', '0X');
|
||||
t('1000', 'a[');
|
||||
t('1.5', '-f');
|
||||
t('123456789.87654321', '[3C7]NAda1');
|
||||
t('1234567890000.0000000987654321', '1c7yH67}?[lk2mc:%');
|
||||
|
||||
t('-0.1', '_1');
|
||||
t('-0.01', '^1');
|
||||
t('-0.001', ']1');
|
||||
t('-0.0001', 'N41');
|
||||
t('-1', 'G');
|
||||
t('-10', 'P');
|
||||
t('-100', 'wX');
|
||||
t('-1000', 'G[');
|
||||
t('-1.5', '`f');
|
||||
t('-123456789.87654321', 'x83C7]NAda1');
|
||||
t('-1234567890000.0000000987654321', 'xc7yH67}?[lk2mc:%');
|
||||
|
||||
t('0.00000009876543212345678987654321', 'h8J+]nxS}gN-^oN');
|
||||
t('-0.00000009876543212345678987654321', 'N8J+]nxS}gN-^oN');
|
||||
t('1.00000009876543212345678987654321', '-7$yQ@UAPUk2SZ#XQh');
|
||||
});
|
||||
|
@ -27,8 +27,7 @@ T('toNearest', function () {
|
||||
t(!isMinusZero(new Decimal(1).toNearest(-3)));
|
||||
t( isMinusZero(new Decimal(-1).toNearest(-3)));
|
||||
|
||||
t = function (expected, n, v, sd, rm) {
|
||||
if (sd) Decimal.precision = sd;
|
||||
t = function (expected, n, v, rm) {
|
||||
T.assertEqual(expected, new Decimal(n).toNearest(v, rm).valueOf());
|
||||
}
|
||||
|
||||
@ -67,9 +66,9 @@ T('toNearest', function () {
|
||||
|
||||
t('0', 1, -3);
|
||||
t('-0', -1, -3);
|
||||
t('3', 1.5, -3, 20, 0);
|
||||
t('-0', -1.5, -3, 20, 1);
|
||||
t('-3', -1.5, -3, 20, 2);
|
||||
t('3', 1.5, -3, 0);
|
||||
t('-0', -1.5, -3, 1);
|
||||
t('-3', -1.5, -3, 2);
|
||||
|
||||
t('123', 123.456);
|
||||
t('123', 123.456, 1);
|
||||
@ -87,68 +86,145 @@ T('toNearest', function () {
|
||||
t('123.46', 123.456, '-0.02');
|
||||
t('123.456', 123.456, '-0.002');
|
||||
|
||||
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);
|
||||
Decimal.precision = 3;
|
||||
|
||||
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);
|
||||
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, 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 = 20;
|
||||
|
||||
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', -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, 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);
|
||||
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);
|
||||
|
||||
Decimal.rounding = 0;
|
||||
t('83105511540', '83105511539.5', void 0, 11);
|
||||
t('83105511540', '83105511539.5');
|
||||
|
||||
Decimal.rounding = 1;
|
||||
t('83105511539', '83105511539.5', void 0, 11);
|
||||
t('83105511539', '83105511539.5');
|
||||
|
||||
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);
|
||||
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);
|
||||
});
|
||||
|
@ -3,7 +3,7 @@ if (typeof T === 'undefined') require('../setup');
|
||||
T('trunc', function () {
|
||||
|
||||
function t(expected, n) {
|
||||
T.assertEqual(expected, Decimal.trunc(n).valueOf());
|
||||
T.assertEqual(expected, new Decimal(n).trunc().valueOf());
|
||||
}
|
||||
|
||||
Decimal.config({
|
||||
|
@ -5,14 +5,14 @@ T = (function () {
|
||||
|
||||
function T(name, tests) {
|
||||
var time;
|
||||
write('\n Testing ' + name + '...');
|
||||
write(' Testing ' + name + '...');
|
||||
passed = testNumber = 0;
|
||||
time = new Date();
|
||||
tests();
|
||||
time = new Date() - time;
|
||||
T.result = [passed, testNumber, time];
|
||||
if (passed !== testNumber) write('\n');
|
||||
write(' ' + passed + ' of ' + testNumber + ' tests passed in ' + time + ' ms');
|
||||
write(' ' + passed + ' of ' + testNumber + ' tests passed in ' + time + ' ms\n');
|
||||
}
|
||||
|
||||
if (typeof window != 'undefined') {
|
||||
|
@ -28,6 +28,7 @@
|
||||
'atanh',
|
||||
'cbrt',
|
||||
'ceil',
|
||||
'clamp',
|
||||
'clone',
|
||||
'cmp',
|
||||
'config',
|
||||
@ -59,6 +60,7 @@
|
||||
'sin',
|
||||
'sinh',
|
||||
'sqrt',
|
||||
'sum',
|
||||
'tan',
|
||||
'tanh',
|
||||
'times',
|
||||
@ -68,7 +70,6 @@
|
||||
'toFixed',
|
||||
'toFraction',
|
||||
'toHex',
|
||||
'toJSON',
|
||||
'toNearest',
|
||||
'toNumber',
|
||||
'toOctal',
|
||||
@ -84,7 +85,8 @@
|
||||
if (!module) {
|
||||
time = new Date() - time;
|
||||
document.body.innerHTML +=
|
||||
'<br><br> In total, ' + passed + ' of ' + total + ' tests passed in ' + (time / 1e3) + ' secs.<br>';
|
||||
'<br> In total, ' + passed + ' of ' + total + ' tests passed in ' +
|
||||
(time / 1e3) + ' secs.<br>';
|
||||
document.body.scrollIntoView(false);
|
||||
return;
|
||||
}
|
||||
@ -105,7 +107,7 @@
|
||||
head.appendChild(script);
|
||||
}
|
||||
|
||||
document.body.innerHTML += ' Testing decimal.js<br>';
|
||||
document.body.innerHTML += ' Testing decimal.js<br><br>';
|
||||
load();
|
||||
</script>
|
||||
</body>
|
||||
|
@ -2,7 +2,7 @@ var time = new Date(),
|
||||
passed = 0,
|
||||
total = 0;
|
||||
|
||||
console.log('\n Testing decimal.js');
|
||||
console.log('\n Testing decimal.js\n');
|
||||
|
||||
[
|
||||
'abs',
|
||||
@ -15,6 +15,7 @@ console.log('\n Testing decimal.js');
|
||||
'atanh',
|
||||
'cbrt',
|
||||
'ceil',
|
||||
'clamp',
|
||||
'clone',
|
||||
'cmp',
|
||||
'config',
|
||||
@ -46,6 +47,7 @@ console.log('\n Testing decimal.js');
|
||||
'sin',
|
||||
'sinh',
|
||||
'sqrt',
|
||||
'sum',
|
||||
'tan',
|
||||
'tanh',
|
||||
'times',
|
||||
@ -55,7 +57,6 @@ console.log('\n Testing decimal.js');
|
||||
'toFixed',
|
||||
'toFraction',
|
||||
'toHex',
|
||||
'toJSON',
|
||||
'toNearest',
|
||||
'toNumber',
|
||||
'toOctal',
|
||||
@ -63,7 +64,7 @@ console.log('\n Testing decimal.js');
|
||||
'toSD',
|
||||
'toString',
|
||||
'trunc',
|
||||
'valueOf',
|
||||
'valueOf'
|
||||
]
|
||||
.forEach(function (module) {
|
||||
require('./modules/' + module);
|
||||
@ -72,4 +73,4 @@ console.log('\n Testing decimal.js');
|
||||
});
|
||||
|
||||
time = new Date() - time;
|
||||
console.log('\n\n In total, ' + passed + ' of ' + total + ' tests passed in ' + (time / 1e3) + ' secs.\n');
|
||||
console.log('\n In total, ' + passed + ' of ' + total + ' tests passed in ' + (time / 1e3) + ' secs.\n');
|
||||
|
Loading…
Reference in New Issue
Block a user