bigmathutils 1.0.0

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.
Files changed (4) hide show
  1. package/README.md +161 -0
  2. package/big.js +1048 -0
  3. package/big.mjs +1034 -0
  4. package/package.json +47 -0
package/README.md ADDED
@@ -0,0 +1,161 @@
1
+ # bigmathutils
2
+
3
+ A lightweight, high-precision **big number** utility for JavaScript and Node.js.
4
+ Designed for financial calculations, blockchain integrations, and any environment where native JavaScript numbers are not enough.
5
+
6
+ ## โœจ Features
7
+
8
+ - ๐Ÿ”ข Arbitrary-precision math using BigInt
9
+ - โšก Zero dependencies โ€” fast & secure
10
+ - ๐Ÿงฎ Easy-to-use math functions (`add`, `sub`, `mul`, `div`, `pow`, etc.)
11
+ - ๐Ÿงฑ Type-safe and stable
12
+ - ๐Ÿ”’ Suitable for cryptography, blockchain, finance, token math
13
+ - ๐Ÿ“ฆ Works in Node.js and bundlers (Vite, Webpack, etc.)
14
+
15
+ ## ๐Ÿ“ฆ Installation
16
+
17
+ The library is the single JavaScript file *big.js* or the ES module *big.mjs*.
18
+
19
+ ### [Node.js](http://nodejs.org)
20
+
21
+ ```bash
22
+ $ npm install bigmathutils
23
+ ```
24
+
25
+ CommonJS:
26
+
27
+ ```javascript
28
+ const Big = require('bigmathutils');
29
+ ```
30
+
31
+ ES module:
32
+
33
+ ```javascript
34
+ import Big from 'bigmathutils';
35
+ ```
36
+
37
+ ## ๐Ÿš€ Usage
38
+
39
+ *In the code examples below, semicolons and `toString` calls are not shown.*
40
+
41
+ The library exports a single constructor function, `Big`.
42
+
43
+ A Big number is created from a primitive number, string, or other Big number.
44
+
45
+ ```javascript
46
+ x = new Big(123.4567)
47
+ y = Big('123456.7e-3') // 'new' is optional
48
+ z = new Big(x)
49
+ x.eq(y) && x.eq(z) && y.eq(z) // true
50
+ ```
51
+
52
+ In Big strict mode, creating a Big number from a primitive number is disallowed.
53
+
54
+ ```javascript
55
+ Big.strict = true
56
+ x = new Big(1) // TypeError: [bigmathutils] Invalid number
57
+ y = new Big('1.0000000000000001')
58
+ y.toNumber() // Error: [bigmathutils] Imprecise conversion
59
+ ```
60
+
61
+ A Big number is immutable in the sense that it is not changed by its methods.
62
+
63
+ ```javascript
64
+ 0.3 - 0.1 // 0.19999999999999998
65
+ x = new Big(0.3)
66
+ x.minus(0.1) // "0.2"
67
+ x // "0.3"
68
+ ```
69
+
70
+ The methods that return a Big number can be chained.
71
+
72
+ ```javascript
73
+ x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772')
74
+ x.sqrt().div(y).pow(3).gt(y.mod(z)) // true
75
+ ```
76
+
77
+ Like JavaScript's Number type, there are `toExponential`, `toFixed` and `toPrecision` methods.
78
+
79
+ ```javascript
80
+ x = new Big(255.5)
81
+ x.toExponential(5) // "2.55500e+2"
82
+ x.toFixed(5) // "255.50000"
83
+ x.toPrecision(5) // "255.50"
84
+ ```
85
+
86
+ The arithmetic methods always return the exact result except `div`, `sqrt` and `pow`
87
+ (with negative exponent), as these methods involve division.
88
+
89
+ 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.
90
+
91
+ ```javascript
92
+ Big.DP = 10
93
+ Big.RM = Big.roundHalfUp
94
+
95
+ x = new Big(2);
96
+ y = new Big(3);
97
+ z = x.div(y) // "0.6666666667"
98
+ z.sqrt() // "0.8164965809"
99
+ z.pow(-3) // "3.3749999995"
100
+ z.times(z) // "0.44444444448888888889"
101
+ z.times(z).round(10) // "0.4444444445"
102
+ ```
103
+
104
+ The value of a Big number is stored in a decimal floating point format in terms of a coefficient, exponent and sign.
105
+
106
+ ```javascript
107
+ x = new Big(-123.456);
108
+ x.c // [1,2,3,4,5,6] coefficient (i.e. significand)
109
+ x.e // 2 exponent
110
+ x.s // -1 sign
111
+ ```
112
+
113
+ For advanced usage, multiple Big number constructors can be created, each with an independent configuration.
114
+
115
+ ## ๐Ÿง  Minify
116
+
117
+ To minify using, for example, npm and [terser](https://github.com/terser/terser)
118
+
119
+ ```bash
120
+ $ npm install -g terser
121
+ ```
122
+
123
+ ```bash
124
+ $ terser big.js -c -m -o big.min.js
125
+ ```
126
+
127
+ ## ๐Ÿงช Test
128
+
129
+ The *test* directory contains the test scripts for each Big number method.
130
+
131
+ The tests can be run with Node.js or a browser.
132
+
133
+ Run all the tests:
134
+
135
+ ```bash
136
+ $ npm test
137
+ ```
138
+
139
+ Test a single method:
140
+
141
+ ```bash
142
+ $ node test/toFixed
143
+ ```
144
+
145
+ For the browser, see *runner.html* and *test.html* in the *test/browser* directory.
146
+
147
+ *big-vs-number.html* is a old application that enables some of the methods of bigmathutils to be compared with those of JavaScript's Number type.
148
+
149
+ ## ๐Ÿ“ TypeScript
150
+
151
+ The [DefinitelyTyped](https://github.com/borisyankov/DefinitelyTyped) project has a Typescript type definitions file for bigmathutils.
152
+
153
+ ```bash
154
+ $ npm install --save-dev @types/bigmathutils
155
+ ```
156
+
157
+ Any questions about the TypeScript type definitions file should be addressed to the DefinitelyTyped project.
158
+
159
+ ## ๐Ÿ“„ Licence
160
+
161
+ [MIT](LICENCE.md)