1
|
# big.js
|
2
|
|
3
|
**A small, fast JavaScript library for arbitrary-precision decimal arithmetic.**
|
4
|
|
5
|
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.
|
6
|
|
7
|
## Features
|
8
|
|
9
|
- Faster, smaller and easier-to-use than JavaScript versions of Java's BigDecimal
|
10
|
- Only 5.9 KB minified and 2.7 KB gzipped
|
11
|
- Simple API
|
12
|
- Replicates the `toExponential`, `toFixed` and `toPrecision` methods of JavaScript's Number type
|
13
|
- Includes a `sqrt` method
|
14
|
- Stores values in an accessible decimal floating point format
|
15
|
- No dependencies
|
16
|
- Comprehensive [documentation](http://mikemcl.github.io/big.js/) and test set
|
17
|
|
18
|
## Set up
|
19
|
|
20
|
The library is the single JavaScript file *big.js* (or *big.min.js*, which is *big.js* minified).
|
21
|
|
22
|
Browser:
|
23
|
|
24
|
```html
|
25
|
<script src='path/to/big.js'></script>
|
26
|
```
|
27
|
|
28
|
[Node.js](http://nodejs.org):
|
29
|
|
30
|
```bash
|
31
|
$ npm install big.js
|
32
|
```
|
33
|
|
34
|
```javascript
|
35
|
const Big = require('big.js');
|
36
|
```
|
37
|
|
38
|
ES6 module:
|
39
|
|
40
|
```javascript
|
41
|
import Big from 'big.mjs';
|
42
|
```
|
43
|
## Use
|
44
|
|
45
|
*In all examples below, `var`, semicolons and `toString` calls are not shown. If a commented-out value is in quotes it means `toString` has been called on the preceding expression.*
|
46
|
|
47
|
The library exports a single function, `Big`, the constructor of Big number instances.
|
48
|
It accepts a value of type number, string or Big number object.
|
49
|
|
50
|
x = new Big(123.4567)
|
51
|
y = Big('123456.7e-3') // 'new' is optional
|
52
|
z = new Big(x)
|
53
|
x.eq(y) && x.eq(z) && y.eq(z) // true
|
54
|
|
55
|
A Big number is immutable in the sense that it is not changed by its methods.
|
56
|
|
57
|
0.3 - 0.1 // 0.19999999999999998
|
58
|
x = new Big(0.3)
|
59
|
x.minus(0.1) // "0.2"
|
60
|
x // "0.3"
|
61
|
|
62
|
The methods that return a Big number can be chained.
|
63
|
|
64
|
x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
|
65
|
x.sqrt().div(y).pow(3).gt(y.mod(z)) // true
|
66
|
|
67
|
Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods.
|
68
|
|
69
|
x = new Big(255.5)
|
70
|
x.toExponential(5) // "2.55500e+2"
|
71
|
x.toFixed(5) // "255.50000"
|
72
|
x.toPrecision(5) // "255.50"
|
73
|
|
74
|
The arithmetic methods always return the exact result except `div`, `sqrt` and `pow`
|
75
|
(with negative exponent), as these methods involve division.
|
76
|
|
77
|
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.
|
78
|
|
79
|
Big.DP = 10
|
80
|
Big.RM = 1
|
81
|
|
82
|
x = new Big(2);
|
83
|
y = new Big(3);
|
84
|
z = x.div(y) // "0.6666666667"
|
85
|
z.sqrt() // "0.8164965809"
|
86
|
z.pow(-3) // "3.3749999995"
|
87
|
z.times(z) // "0.44444444448888888889"
|
88
|
z.times(z).round(10) // "0.4444444445"
|
89
|
|
90
|
Multiple Big number constructors can be created, each with an independent configuration.
|
91
|
|
92
|
The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
|
93
|
|
94
|
x = new Big(-123.456);
|
95
|
x.c // [1,2,3,4,5,6] coefficient (i.e. significand)
|
96
|
x.e // 2 exponent
|
97
|
x.s // -1 sign
|
98
|
|
99
|
For further information see the [API](http://mikemcl.github.io/big.js/) reference from the *doc* folder.
|
100
|
|
101
|
## Test
|
102
|
|
103
|
The *test* directory contains the test scripts for each Big number method.
|
104
|
|
105
|
The tests can be run with Node.js or a browser.
|
106
|
|
107
|
To run all the tests
|
108
|
|
109
|
$ npm test
|
110
|
|
111
|
To test a single method
|
112
|
|
113
|
$ node test/toFixed
|
114
|
|
115
|
For the browser, see *single-test.html* and *every-test.html* in the *test/browser* directory.
|
116
|
|
117
|
*big-vs-number.html* is a simple application that enables some of the methods of big.js to be compared with those of JavaScript's Number type.
|
118
|
|
119
|
## Performance
|
120
|
|
121
|
The *perf* directory contains two legacy applications and a *lib* directory containing the BigDecimal libraries used by both.
|
122
|
|
123
|
*big-vs-bigdecimal.html* tests the performance of big.js against the JavaScript translations of two versions of BigDecimal, its use should be more or less self-explanatory.
|
124
|
|
125
|
* [GWT: java.math.BigDecimal](https://github.com/iriscouch/bigdecimal.js)
|
126
|
* [ICU4J: com.ibm.icu.math.BigDecimal](https://github.com/dtrebbien/BigDecimal.js)
|
127
|
|
128
|
The BigDecimal in the npm registry is the GWT version. It has some bugs, see the Node.js script *perf/lib/bigdecimal_GWT/bugs.js* for examples of flaws in its *remainder*, *divide* and *compareTo* methods.
|
129
|
|
130
|
*bigtime.js* is a Node.js command-line application which tests the performance of big.js against the GWT version of
|
131
|
BigDecimal from the npm registry.
|
132
|
|
133
|
For example, to compare the time taken by the big.js `plus` method and the BigDecimal `add` method
|
134
|
|
135
|
$ node bigtime plus 10000 40
|
136
|
|
137
|
This will time 10000 calls to each, using operands of up to 40 random digits and will check that the results match.
|
138
|
|
139
|
For help
|
140
|
|
141
|
$ node bigtime -h
|
142
|
|
143
|
## Build
|
144
|
|
145
|
If [uglify-js](https://github.com/mishoo/UglifyJS2) is installed globally
|
146
|
|
147
|
$ npm install uglify-js -g
|
148
|
|
149
|
then
|
150
|
|
151
|
$ npm run build
|
152
|
|
153
|
will create *big.min.js*.
|
154
|
|
155
|
## TypeScript
|
156
|
|
157
|
The [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) project has a Typescript type definitions file for big.js.
|
158
|
|
159
|
$ npm install @types/big.js
|
160
|
|
161
|
Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.
|
162
|
|
163
|
## Feedback
|
164
|
|
165
|
Bugs/comments/questions?
|
166
|
|
167
|
Open an issue, or email <a href="mailto:M8ch88l@gmail.com">Michael</a>
|
168
|
|
169
|
## Licence
|
170
|
|
171
|
[MIT](LICENCE)
|
172
|
|
173
|
## Contributors
|
174
|
|
175
|
This project exists thanks to all the people who contribute. [[Contribute](CONTRIBUTING.md)].
|
176
|
<a href="graphs/contributors"><img src="https://opencollective.com/bigjs/contributors.svg?width=890&button=false" /></a>
|
177
|
|
178
|
|
179
|
## Backers
|
180
|
|
181
|
Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/bigjs#backer)]
|
182
|
|
183
|
<a href="https://opencollective.com/bigjs#backers" target="_blank"><img src="https://opencollective.com/bigjs/backers.svg?width=890"></a>
|
184
|
|
185
|
|
186
|
## Sponsors
|
187
|
|
188
|
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/bigjs#sponsor)]
|
189
|
|
190
|
<a href="https://opencollective.com/bigjs/sponsor/0/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/0/avatar.svg"></a>
|
191
|
<a href="https://opencollective.com/bigjs/sponsor/1/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/1/avatar.svg"></a>
|
192
|
<a href="https://opencollective.com/bigjs/sponsor/2/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/2/avatar.svg"></a>
|
193
|
<a href="https://opencollective.com/bigjs/sponsor/3/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/3/avatar.svg"></a>
|
194
|
<a href="https://opencollective.com/bigjs/sponsor/4/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/4/avatar.svg"></a>
|
195
|
<a href="https://opencollective.com/bigjs/sponsor/5/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/5/avatar.svg"></a>
|
196
|
<a href="https://opencollective.com/bigjs/sponsor/6/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/6/avatar.svg"></a>
|
197
|
<a href="https://opencollective.com/bigjs/sponsor/7/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/7/avatar.svg"></a>
|
198
|
<a href="https://opencollective.com/bigjs/sponsor/8/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/8/avatar.svg"></a>
|
199
|
<a href="https://opencollective.com/bigjs/sponsor/9/website" target="_blank"><img src="https://opencollective.com/bigjs/sponsor/9/avatar.svg"></a>
|
200
|
|
201
|
|