@numio/bigmath 2.2.2 → 2.2.4

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.4
2
+ Fix handling negative numbers for toBase function.
3
+
4
+ ### 2.2.3
5
+ Add validation for negative number input in square root function
6
+ Add validation for negative number input in cube root function
7
+
1
8
  ### 2.2.2
2
9
  Throw an error if input for MAD and IQR is less than 3 elements
3
10
  Add home-page url
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.2",
4
+ "version": "2.2.4",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
package/src/cbrt/utils.js CHANGED
@@ -3,14 +3,18 @@ import { divInner } from "../operations/div/utils.js";
3
3
  import { PipeInner } from "../pipe/utils.js";
4
4
  import { calcInner } from "../shared/utils.js";
5
5
  export const cbrtInner = (value, prec = [1n, 13], max = 100) => {
6
+ var _a, _b;
7
+ if (value[0] < 0n) {
8
+ throw new Error("Invalid input: Cannot take the cube root of a negative value.");
9
+ }
6
10
  let guess = value;
7
11
  for (let i = 0; i < max; i++) {
8
12
  const mul = calcInner([guess, guess], (a, b) => a * b);
9
- const nextGuess = new PipeInner().add([
13
+ const nextGuess = (_a = new PipeInner().add([
10
14
  calcInner([guess, [2n, 0]], (a, b) => a * b),
11
15
  divInner([value, mul], 20),
12
- ]).div([[3n, 0]]).calc();
13
- let [bi, fpe] = new PipeInner().sub([nextGuess, guess]).calc();
16
+ ]).div([[3n, 0]]).result) !== null && _a !== void 0 ? _a : [0n, 0];
17
+ let [bi, fpe] = (_b = new PipeInner().sub([nextGuess, guess]).result) !== null && _b !== void 0 ? _b : [0n, 0];
14
18
  if (bi < 0n)
15
19
  bi *= -1n;
16
20
  if (isLeftGreaterInner({ left: prec, right: [bi, fpe] })) {
@@ -18,8 +18,16 @@ 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
+ const isOctal = str.startsWith("0o") || str.startsWith("-0o");
23
+ const isBinary = str.startsWith("0b") || str.startsWith("-0b");
24
+ if (fpi === -1 && !isHex && !isOctal && !isBinary)
22
25
  return [BigInt(str), 0];
26
+ if (isHex || isBinary || isOctal) {
27
+ const isNegative = str[0] === "-";
28
+ const bi = BigInt(str.slice(isNegative ? 1 : 0));
29
+ return [isNegative ? -1n * bi : bi, 0];
30
+ }
23
31
  if (str.length < 15 && str[0] !== "0") {
24
32
  return [
25
33
  BigInt(+(str.slice(0, fpi) + str.slice(fpi + 1))),
package/src/sqrt/utils.js CHANGED
@@ -1,13 +1,16 @@
1
1
  import { isLeftGreaterInner } from "../compare/utils.js";
2
2
  import { PipeInner } from "../pipe/utils.js";
3
3
  export const sqrtInner = (value, prec = [1n, 13], max = 100) => {
4
+ var _a;
5
+ if (value[0] < 0n) {
6
+ throw new Error("Invalid input: Cannot take the square root of a negative value.");
7
+ }
4
8
  let guess = value;
5
9
  for (let i = 0; i < max; i++) {
6
- const nextGuess = new PipeInner()
10
+ const nextGuess = (_a = new PipeInner()
7
11
  .div([value, guess])
8
12
  .add([guess])
9
- .mul([[5n, 1]])
10
- .calc();
13
+ .mul([[5n, 1]]).result) !== null && _a !== void 0 ? _a : [0n, 0];
11
14
  let [bi, fpe] = new PipeInner().sub([nextGuess, guess]).calc();
12
15
  if (bi < 0n)
13
16
  bi *= -1n;
@@ -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;