@universetech-inc/unbig.js 6.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE.md +26 -0
- package/README.md +207 -0
- package/big.js +1043 -0
- package/big.mjs +1027 -0
- package/package.json +64 -0
package/LICENCE.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
=====================
|
|
3
|
+
|
|
4
|
+
Copyright © `<2022>` `Michael Mclaughlin`
|
|
5
|
+
|
|
6
|
+
Permission is hereby granted, free of charge, to any person
|
|
7
|
+
obtaining a copy of this software and associated documentation
|
|
8
|
+
files (the “Software”), to deal in the Software without
|
|
9
|
+
restriction, including without limitation the rights to use,
|
|
10
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the
|
|
12
|
+
Software is furnished to do so, subject to the following
|
|
13
|
+
conditions:
|
|
14
|
+
|
|
15
|
+
The above copyright notice and this permission notice shall be
|
|
16
|
+
included in all copies or substantial portions of the Software.
|
|
17
|
+
|
|
18
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
20
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
22
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
23
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
24
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
25
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
|
26
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# big.js
|
|
2
|
+
|
|
3
|
+
**A small, fast JavaScript library for arbitrary-precision decimal arithmetic.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/big.js)
|
|
6
|
+
[](https://www.npmjs.com/package/big.js)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- Simple API
|
|
11
|
+
- Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
|
|
12
|
+
- Only 6 KB minified
|
|
13
|
+
- Replicates the `toExponential`, `toFixed` and `toPrecision` methods of JavaScript Numbers
|
|
14
|
+
- Stores values in an accessible decimal floating point format
|
|
15
|
+
- Comprehensive [documentation](http://mikemcl.github.io/big.js/) and test set
|
|
16
|
+
- No dependencies
|
|
17
|
+
- Uses ECMAScript 3 only, so works in all browsers
|
|
18
|
+
|
|
19
|
+
The little sister to [bignumber.js](https://github.com/MikeMcl/bignumber.js/) and [decimal.js](https://github.com/MikeMcl/decimal.js/). See [here](https://github.com/MikeMcl/big.js/wiki) for some notes on the difference between them.
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
The library is the single JavaScript file *big.js* or the ES module *big.mjs*.
|
|
24
|
+
|
|
25
|
+
### Browsers
|
|
26
|
+
|
|
27
|
+
Add Big to global scope:
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script src='path/to/big.js'></script>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
ES module:
|
|
34
|
+
|
|
35
|
+
```html
|
|
36
|
+
<script type='module'>
|
|
37
|
+
import Big from './path/to/big.mjs';
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Get a minified version from a CDN:
|
|
41
|
+
|
|
42
|
+
```html
|
|
43
|
+
<script src='https://cdn.jsdelivr.net/npm/big.js@6.2.1/big.min.js'></script>
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### [Node.js](http://nodejs.org)
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
$ npm install big.js
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
CommonJS:
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
const Big = require('big.js');
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
ES module:
|
|
59
|
+
|
|
60
|
+
```javascript
|
|
61
|
+
import Big from 'big.js';
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### [Deno](https://deno.land/)
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
import Big from 'https://raw.githubusercontent.com/mikemcl/big.js/v6.2.1/big.mjs';
|
|
68
|
+
import Big from 'https://unpkg.com/big.js@6.2.1/big.mjs';
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Use
|
|
72
|
+
|
|
73
|
+
*In the code examples below, semicolons and `toString` calls are not shown.*
|
|
74
|
+
|
|
75
|
+
The library exports a single constructor function, `Big`.
|
|
76
|
+
|
|
77
|
+
A Big number is created from a primitive number, string, or other Big number.
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
x = new Big(123.4567)
|
|
81
|
+
y = Big('123456.7e-3') // 'new' is optional
|
|
82
|
+
z = new Big(x)
|
|
83
|
+
x.eq(y) && x.eq(z) && y.eq(z) // true
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
In Big strict mode, creating a Big number from a primitive number is disallowed.
|
|
87
|
+
|
|
88
|
+
```javascript
|
|
89
|
+
Big.strict = true
|
|
90
|
+
x = new Big(1) // TypeError: [big.js] Invalid number
|
|
91
|
+
y = new Big('1.0000000000000001')
|
|
92
|
+
y.toNumber() // Error: [big.js] Imprecise conversion
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
A Big number is immutable in the sense that it is not changed by its methods.
|
|
96
|
+
|
|
97
|
+
```javascript
|
|
98
|
+
0.3 - 0.1 // 0.19999999999999998
|
|
99
|
+
x = new Big(0.3)
|
|
100
|
+
x.minus(0.1) // "0.2"
|
|
101
|
+
x // "0.3"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
The methods that return a Big number can be chained.
|
|
105
|
+
|
|
106
|
+
```javascript
|
|
107
|
+
x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
|
|
108
|
+
x.sqrt().div(y).pow(3).gt(y.mod(z)) // true
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods.
|
|
112
|
+
|
|
113
|
+
```javascript
|
|
114
|
+
x = new Big(255.5)
|
|
115
|
+
x.toExponential(5) // "2.55500e+2"
|
|
116
|
+
x.toFixed(5) // "255.50000"
|
|
117
|
+
x.toPrecision(5) // "255.50"
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
The arithmetic methods always return the exact result except `div`, `sqrt` and `pow`
|
|
121
|
+
(with negative exponent), as these methods involve division.
|
|
122
|
+
|
|
123
|
+
The maximum number of decimal places and the rounding mode used to round the results of these methods is determined by the value of the `DP` and `RM` properties of the `Big` number constructor.
|
|
124
|
+
|
|
125
|
+
```javascript
|
|
126
|
+
Big.DP = 10
|
|
127
|
+
Big.RM = Big.roundHalfUp
|
|
128
|
+
|
|
129
|
+
x = new Big(2);
|
|
130
|
+
y = new Big(3);
|
|
131
|
+
z = x.div(y) // "0.6666666667"
|
|
132
|
+
z.sqrt() // "0.8164965809"
|
|
133
|
+
z.pow(-3) // "3.3749999995"
|
|
134
|
+
z.times(z) // "0.44444444448888888889"
|
|
135
|
+
z.times(z).round(10) // "0.4444444445"
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
|
|
139
|
+
|
|
140
|
+
```javascript
|
|
141
|
+
x = new Big(-123.456);
|
|
142
|
+
x.c // [1,2,3,4,5,6] coefficient (i.e. significand)
|
|
143
|
+
x.e // 2 exponent
|
|
144
|
+
x.s // -1 sign
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
For advanced usage, multiple Big number constructors can be created, each with an independent configuration.
|
|
148
|
+
|
|
149
|
+
For further information see the [API](http://mikemcl.github.io/big.js/) reference documentation.
|
|
150
|
+
|
|
151
|
+
## Minify
|
|
152
|
+
|
|
153
|
+
To minify using, for example, npm and [terser](https://github.com/terser/terser)
|
|
154
|
+
|
|
155
|
+
```bash
|
|
156
|
+
$ npm install -g terser
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
$ terser big.js -c -m -o big.min.js
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Test
|
|
164
|
+
|
|
165
|
+
The *test* directory contains the test scripts for each Big number method.
|
|
166
|
+
|
|
167
|
+
The tests can be run with Node.js or a browser.
|
|
168
|
+
|
|
169
|
+
Run all the tests:
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
$ npm test
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Test a single method:
|
|
176
|
+
|
|
177
|
+
```bash
|
|
178
|
+
$ node test/toFixed
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
For the browser, see *runner.html* and *test.html* in the *test/browser* directory.
|
|
182
|
+
|
|
183
|
+
*big-vs-number.html* is a old application that enables some of the methods of big.js to be compared with those of JavaScript's Number type.
|
|
184
|
+
|
|
185
|
+
## TypeScript
|
|
186
|
+
|
|
187
|
+
The [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) project has a Typescript type definitions file for big.js.
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
$ npm install --save-dev @types/big.js
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.
|
|
194
|
+
|
|
195
|
+
## Licence
|
|
196
|
+
|
|
197
|
+
[MIT](LICENCE.md)
|
|
198
|
+
|
|
199
|
+
## Contributors
|
|
200
|
+
|
|
201
|
+
<a href="graphs/contributors"><img src="https://opencollective.com/bigjs/contributors.svg?width=890&button=false" /></a>
|
|
202
|
+
|
|
203
|
+
## Financial supporters
|
|
204
|
+
|
|
205
|
+
Thank you to all who have supported this project via [Open Collective](https://opencollective.com/bigjs), particularly [Coinbase](https://www.coinbase.com/).
|
|
206
|
+
|
|
207
|
+
<img src="https://opencollective.com/bigjs/sponsor/0/avatar.svg">
|