@numio/bigmath 2.2.3 → 2.2.5

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/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ### 2.2.5
2
+ Handle "0B", "0X", "0O", "-0B", "-0X", "-0O" as a start of the input for toBase function.
3
+ Move documentation from README.md to web-site http://numio.deno.dev
4
+
5
+ ### 2.2.4
6
+ Fix handling negative numbers for toBase function.
7
+
1
8
  ### 2.2.3
2
9
  Add validation for negative number input in square root function
3
10
  Add validation for negative number input in cube root function
package/README.md CHANGED
@@ -96,336 +96,9 @@ pnpm add @numio/bigmath
96
96
  deno add jsr:@numio/bigmath
97
97
  ```
98
98
 
99
- # Examples:
100
-
101
- ### Add numbers
102
-
103
- ```javascript
104
- import { add } from "@numio/bigmath";
105
-
106
- const int = add(["12345", "99"]); // 12444
107
- const float = add(["0.1", "0.2", "0.3"]); // 0.6
108
- const negative = add(["0.1", "-0.3", "0.1"]); // -0.1
109
- ```
110
-
111
- ### Subtract numbers
112
-
113
- ```javascript
114
- import { sub } from "@numio/bigmath";
115
-
116
- const int = sub(["150", "99"]); // 51
117
- const float = sub(["1", "0.99"]); // 0.01
118
- const negative = sub(["-0.1", "-0.3", "0.4"]); // -0.2
119
- ```
120
-
121
- ### Multiply numbers
122
-
123
- ```javascript
124
- import { mul } from "@numio/bigmath";
125
-
126
- const int = mul(["15", "11", "2"]); // 330
127
- const float = mul(["0.01", "0.99"]); // 0.0099
128
- const negative = mul(["-2", "3"]); // -6
129
- ```
130
-
131
- ### Divide numbers
132
-
133
- ```javascript
134
- import { div } from "@numio/bigmath";
135
-
136
- const int = div(["9999", "33"]); // 303
137
- const float = div(["0.06", "0.2"]); // 0.3
138
- const negative = div(["-2", "-3", "2"]); //0.33333333333333333333
139
-
140
- // set number of digit after the decimal. By default it's 20
141
- div(["10", "3"], 4); // 3.3333
142
- ```
143
-
144
- ### HEX, decimal, octal, binary
145
- ```javascript
146
- import { add, sub } from "@numio/bigmath";
147
-
148
- add(["0xA", "0x5"]) // HEX + HEX, 10 + 5 = 15
149
- add(["0xA", "2"]) // HEX + decimal, 10 + 2 = 12
150
- sub(["35", "0b11"]) // decimal - binary, 35 - 3 = 32
151
- sub(["0x1A", "0o31", "0b101", ]) // HEX - octal - binary, 26 - 25 - 5 = -4
152
-
153
- ```
154
-
155
- ### Round
156
-
157
- ```javascript
158
- import { round } from "@numio/bigmath";
159
-
160
- round("-1.12345"); // -1
161
- round("1.5"); // 2
162
- round("1.0"); // 1
163
- round("0.00001"); // 0
164
- round("9.9"); // 10
165
- ```
166
-
167
- ### Round at position
168
-
169
- ```javascript
170
- import { round } from "@numio/bigmath";
171
-
172
- round("1.12345", { decimals: 1 }); // 1.1
173
- round("1.12345", { decimals: 2 }); // 1.12
174
- round("1.12234", { decimals: 0 }); // 1
175
- round("9.999", { decimals: 2 }); // 10
176
- ```
177
-
178
- ### Round modes
179
-
180
- ```javascript
181
- import { round } from "@numio/bigmath";
182
-
183
- round("1.11", { decimals: 1, roundMode: "up" }); // 1.2
184
- round("1.19", { decimals: 1, roundMode: "up" }); // 1.2
185
-
186
- round("1.11", { decimals: 1, roundMode: "down" }); // 1.1
187
- round("1.19", { decimals: 1, roundMode: "down" }); // 1.1
188
-
189
- round("1.15", { decimals: 1, roundMode: "half-up" }); // 1.2
190
- round("1.15", { decimals: 1, roundMode: "half-down" }); // 1.1
191
-
192
- round("1.15", { decimals: 1, roundMode: "half-even" }); // 1.2
193
- round("1.25", { decimals: 1, roundMode: "half-even" }); // 1.2
194
- round("1.35", { decimals: 1, roundMode: "half-even" }); // 1.4
195
- round("1.45", { decimals: 1, roundMode: "half-even" }); // 1.4
196
- round("1.55", { decimals: 1, roundMode: "half-even" }); // 1.6
197
-
198
- round("1.15", { decimals: 1, roundMode: "half-odd" }); // 1.1
199
- round("1.25", { decimals: 1, roundMode: "half-odd" }); // 1.3
200
- round("1.35", { decimals: 1, roundMode: "half-odd" }); // 1.3
201
- round("1.45", { decimals: 1, roundMode: "half-odd" }); // 1.5
202
- round("1.55", { decimals: 1, roundMode: "half-odd" }); // 1.5
203
- ```
204
-
205
- ### Round with "significant figures" flag
206
-
207
- ```javascript
208
- import { round } from "@numio/bigmath";
209
-
210
- round("0.000119", { decimals: 2, sigFig: false }); // 0
211
- round("0.000119", { decimals: 2, sigFig: true }); // 0.00012
212
-
213
- round("0.0019", { decimals: 1, sigFig: true, roundMode: "down" }); // 0.001
214
- round("0.0011", { decimals: 1, sigFig: true, roundMode: "up" }); // 0.002
215
-
216
- round("1.000119", { decimals: 2, sigFig: false }); // 1
217
- round("1.000119", { decimals: 2, sigFig: true }); // 1
218
- ```
219
-
220
- ### Pipe
221
-
222
- ```javascript
223
- import { Pipe } from "@numio/bigmath";
224
-
225
- const addNums = ["1", "2", "3"];
226
- const subNums = ["0.2", "0.3"];
227
- const divNums = ["4"];
228
- const mulNums = ["2", "5", "0.2"];
229
-
230
- new Pipe().add(addNums) // 6
231
- .div(divNums) // 6 / 4 = 1.5
232
- .sub(subNums) // 1.5 - 0.2 - 0.3 = 1
233
- .mul(mulNums) // 1 * 2 * 5 * 0.2 = 2
234
- .calc() // convert end result to readable string
235
-
236
- new Pipe().sub(["1", "5"]) // 1 - 5 = -4
237
- .abs() // 4 - returns absolute value
238
-
239
- new Pipe().add(["10", "5"]) // 11 + 5 = 15
240
- .resultToBase(16) // f - returns result converted to base 16
241
- ```
242
-
243
- ### Quartile
244
- ```javascript
245
- import { quartile } from "@numio/bigmath";
246
-
247
- quartile(["1", "2", "3", "4", "5", "6", "7", "8", "9"]) // { Q1: "2.5", Q2: "5", Q3: "7.5" }
248
- quartile(["0.001", "0.3", "0.4", "1"]) // { Q1: "0.1505", Q2: "0.35", Q3: "0.7" }
249
-
250
- ```
251
-
252
- ### Sort
253
- ```javascript
254
- import { sort } from "@numio/bigmath";
255
-
256
- // native js sort for strings
257
- ["1", "10", "11", "101", "11", "10", "1"].sort() // ["1", "1", "10", "10", "101", "11", "11"]
258
-
259
- // sort from "@numio/bigmath"
260
- sort(["1", "10", "11", "101", "11", "10", "1"]) // ["1", "1", "10", "10", "11", "11", "101"]
261
-
262
- // ASC sorting
263
- sort(["-0.1", "0.1", "-1"], "asc") // ["-1", "-0.1", "0.1"]
264
-
265
- // DESC sorting
266
- sort(["-0.1", "0.1", "-1"], "desc") // ["0.1", "-0.1", "-1"]
267
-
268
- ```
269
-
270
- ### Mean
271
- ```javascript
272
- import { mean } from "@numio/bigmath";
273
-
274
- mean(["5", "4", "3", "2", "1", "0"]) // "2.5"
275
- mean(["0.5", "0.4", "0.3", "0.2", "0.1", "0"]) // "0.25"
276
- ```
277
-
278
- ### Max
279
- ```javascript
280
- import { max } from "@numio/bigmath";
281
-
282
- max(["2", "-1", "0.1"]) // 2;
283
- ```
284
-
285
- ### Min
286
- ```javascript
287
- import { min } from "@numio/bigmath";
288
-
289
- min(["2", "-1", "0.1"]) // -1;
290
- ```
291
-
292
- ### IsEqual
293
- ```javascript
294
- import { isEqual } from "@numio/bigmath";
295
-
296
- isEqual({left: "0.1", right: "0.1"}) // true;
297
- isEqual({left: "2", right: "0.1"}) // false;
298
- ```
299
-
300
- ### IsLeftGreater
301
- ```javascript
302
- import { isLeftGreater } from "@numio/bigmath";
303
-
304
- isLeftGreater({left: "0.1", right: "2"}) // false;
305
- isLeftGreater({left: "2", right: "0.1"}) // true;
306
- isLeftGreater({left: "0.1", right: "0.1"}) // false;
307
- isLeftGreater({left: "0.1", right: "-0.1"}) // true;
308
- ```
309
-
310
- ### IsLeftGreaterOrEqual
311
- ```javascript
312
- import { isLeftGreaterOrEqual } from "@numio/bigmath";
313
-
314
- isLeftGreaterOrEqual({left: "0.1", right: "2"}) // false;
315
- isLeftGreaterOrEqual({left: "2", right: "0.1"}) // true;
316
- isLeftGreaterOrEqual({left: "0.1", right: "0.1"}) // true;
317
- isLeftGreaterOrEqual({left: "0.1", right: "-0.1"}) // true;
318
- ```
319
-
320
- ### MAD - Median Absolute Deviation
321
- ```javascript
322
- import { MAD } from "@numio/bigmath";
323
-
324
- MAD(["7", "15", "36", "39", "40", "41"]) // 3;
325
- ```
326
-
327
- ### IQR - Interquartile Range
328
- ```javascript
329
- import { IQR } from "@numio/bigmath";
330
-
331
- IQR(["7", "15", "36", "39", "40", "41"]) // 25;
332
- ```
333
-
334
- ### SQRT - square root of a number
335
- ```javascript
336
- import { sqrt } from "@numio/bigmath";
337
-
338
- sqrt("81") // 9;
339
- sqrt("3") // 1.7320508075689;
340
-
341
- // you can change precision of a result (second parameter),
342
- sqrt("3", "0.01") // 1.732;
343
- sqrt("3", "0.000000000000000000001") // 1.732050807568877293527;
344
- ```
345
-
346
- ### CBRT - cube root of a number
347
- ```javascript
348
- import { cbrt } from "@numio/bigmath";
349
-
350
- cbrt("37") // 3;
351
- cbrt("1000") // 10;
352
- cbrt("15"), // 2.4662120743305
353
-
354
- // you can change precision of a result (second parameter),
355
- cbrt("15", "0.001") // 2.466
356
- ```
357
-
358
- ### ABS - absolute value
359
- ```javascript
360
- import { abs } from "@numio/bigmath";
361
-
362
- abs("-1") // 1;
363
- abs("1") // 1;
364
- ```
365
-
366
- ### toBase - convert number to another base
367
- ```javascript
368
- import { toBase } from "@numio/bigmath";
369
-
370
- toBase({ value: "11", toBase: 16 }) // b
371
- // 0x - HEX
372
- toBase({ value: "0xb", toBase: 10 }) // 11
373
- // 0b - binary
374
- toBase({ value: "0b101", toBase: 10 }) // 5
375
- toBase({ value: "0b1101", toBase: 16 }) // d
376
- // 0o - octal
377
- toBase({ value: "0o11", toBase: 10 }) // 9
378
- ```
379
-
380
- ### Number format validation
381
-
382
- ```javascript
383
- import { isHex, isBinary, isDecimal, isOctal, isNumber } from "@numio/bigmath";
384
-
385
- console.log(isHex("0x1A")); // true
386
- console.log(isHex("1A")); // false (no 0x prefix)
387
- console.log(isHex("-0x2f")); // true
388
- console.log(isHex("0xG")); // false (invalid hex digit)
389
-
390
- console.log(isBinary("0b101")); // true
391
- console.log(isBinary("101")); // false (no 0b prefix)
392
- console.log(isBinary("-0b11")); // true
393
- console.log(isBinary("0b12")); // false (invalid binary digit)
394
-
395
- console.log(isDecimal("123")); // true
396
- console.log(isDecimal("123.45"));// true
397
- console.log(isDecimal("-0.5")); // true
398
- console.log(isDecimal(".5")); // false (leading dot)
399
- console.log(isDecimal("10.")); // false (trailing dot)
400
-
401
- console.log(isOctal("0o77")); // true
402
- console.log(isOctal("77")); // false (no 0o prefix)
403
- console.log(isOctal("-0o12")); // true
404
- console.log(isOctal("0o19")); // false (invalid octal digit)
405
-
406
- console.log(isNumber("0x1A")); // true (hex)
407
- console.log(isNumber("0b101")); // true (binary)
408
- console.log(isNumber("123.45"));// true (decimal)
409
- console.log(isNumber("0o77")); // true (octal)
410
- console.log(isNumber("123")); // true (decimal)
411
- console.log(isNumber("abc")); // false (not a valid number)
412
- ```
413
-
414
- Does not have a limitation on the number of digits. You can use any length you'd
415
- like
416
-
417
- ```javascript
418
- // NO precision loss using numeric literals with more than 15 significant digits.
419
- const int = add(
420
- "999999999999999999999999999999999999999999999999999999999999999",
421
- "2",
422
- ); // "1000000000000000000000000000000000000000000000000000000000000001"
423
-
424
- const float = mul(
425
- "0.00000000000000000000000000000000000000000000000000000000000000000009",
426
- "0.000000002",
427
- ); // 0.00000000000000000000000000000000000000000000000000000000000000000000000000018
428
- ```
99
+ ##
100
+ ### See documentation and examples here: https://numio.deno.dev
101
+ ##
429
102
 
430
103
  Download from NPM - https://www.npmjs.com/package/@numio/bigmath
431
104
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@numio/bigmath",
3
3
  "description": "@numio/bigmath is an arbitrary-precision arithmetic library. It can be used for basic operations with decimal numbers (integers and float)",
4
- "version": "2.2.3",
4
+ "version": "2.2.5",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -18,8 +18,19 @@ export const bi2s = (bigInt, fpe) => {
18
18
  };
19
19
  export const s2bi = (str) => {
20
20
  const fpi = str.indexOf(".");
21
- if (fpi === -1)
21
+ const isHex = str.startsWith("0x") || str.startsWith("-0x") ||
22
+ str.startsWith("-0X") || str.startsWith("0X");
23
+ const isOctal = str.startsWith("0o") || str.startsWith("-0o") ||
24
+ str.startsWith("-0O") || str.startsWith("0O");
25
+ const isBinary = str.startsWith("0b") || str.startsWith("-0b") ||
26
+ str.startsWith("-0B") || str.startsWith("0B");
27
+ if (fpi === -1 && !isHex && !isOctal && !isBinary)
22
28
  return [BigInt(str), 0];
29
+ if (isHex || isBinary || isOctal) {
30
+ const isNegative = str[0] === "-";
31
+ const bi = BigInt(str.slice(isNegative ? 1 : 0));
32
+ return [isNegative ? -1n * bi : bi, 0];
33
+ }
23
34
  if (str.length < 15 && str[0] !== "0") {
24
35
  return [
25
36
  BigInt(+(str.slice(0, fpi) + str.slice(fpi + 1))),
@@ -1,4 +1,14 @@
1
+ import { s2bi } from "../shared/utils.js";
1
2
  /** Convert number to another base */
2
3
  export const toBase = ({ value, toBase }) => {
3
- return BigInt(value).toString(toBase);
4
+ const [bi] = s2bi(value);
5
+ const isNegative = value[0] === "-";
6
+ const map = {
7
+ 2: "0b",
8
+ 8: "0o",
9
+ 10: "",
10
+ 16: "0x",
11
+ };
12
+ return (isNegative ? "-" : "") + map[toBase] +
13
+ (isNegative ? -1n * bi : bi).toString(toBase).toUpperCase();
4
14
  };
@@ -1,4 +1,4 @@
1
1
  export type ToBase = (arg: {
2
2
  value: string;
3
- toBase: number;
3
+ toBase: 16 | 10 | 8 | 2;
4
4
  }) => string;