@numio/bigmath 2.3.2 → 2.4.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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ### 2.4.0
2
+ Add Mod - Modulo operation, which finds the remainder when one number is divided by another
3
+
1
4
  ### 2.3.2
2
5
  FDR - change variable name to make it consistent
3
6
 
package/README.md CHANGED
@@ -5,11 +5,10 @@
5
5
 
6
6
  ## Latest Update
7
7
 
8
- This update introduces several enhancements and new functionalities:
8
+ This update introduces new functionalities:
9
9
 
10
- - **Improved Error Handling:** The application now features more robust error handling to provide clearer feedback and prevent unexpected crashes.
11
- - **Freedman-Diaconis Rule (FDR):** Added the Freedman-Diaconis rule for optimal binwidth estimation in histogram creation.
12
- - **Median Absolute Deviation (MAD):** Implemented calculation for the Median Absolute Deviation, allowing users to compute it based on either the `median` or the `mean` of the dataset.
10
+ * **Modulo Operation (`mod`):** A new `mod` function has been added for performing modulo operations.
11
+ * **Pipe Function Enhancement:** The `mod` function has been integrated into the `Pipe` function for seamless chaining of operations.
13
12
 
14
13
  ## Existing Functionality
15
14
 
@@ -32,6 +31,7 @@ The application currently supports the following operations:
32
31
  - `min`: Finding the minimum value in a dataset.
33
32
  - `MAD`: Calculating the Median Absolute Deviation (based on either median or mean).
34
33
  - `IQR`: Calculating the Interquartile Range.
34
+ - `FDR`: Freedman-Diaconis rule
35
35
  - **Comparison:**
36
36
  - `isEqual`: Checking if two values are equal.
37
37
  - `isLeftGreater`: Checking if the left value is greater than the right value.
@@ -40,6 +40,7 @@ The application currently supports the following operations:
40
40
  - `sqrt`: Calculating the square root of a number.
41
41
  - `cbrt`: Calculating the cube root of a number.
42
42
  - `abs`: Calculating the absolute value of a number.
43
+ - `mod`: Modulo operation, which finds the remainder when one number is divided by another
43
44
  - **Number Base Conversion:**
44
45
  - `toBase`: Converting a number to a specified base.
45
46
  - `isHex`: Checking if a string is a valid hexadecimal number.
package/index.d.ts CHANGED
@@ -24,4 +24,5 @@ import { isDecimal } from "./src/isValid/isDecimal.d.ts";
24
24
  import { isOctal } from "./src/isValid/isOctal.d.ts";
25
25
  import { isNumber } from "./src/isValid/isNumber.d.ts";
26
26
  import { FDR } from "./src/FDR/fdr.d.ts";
27
- export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, isHex, isBinary, isDecimal, isOctal, isNumber, FDR };
27
+ import { mod } from "./src/mod/mod.d.ts";
28
+ export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, isHex, isBinary, isDecimal, isOctal, isNumber, FDR, mod };
package/index.js CHANGED
@@ -24,4 +24,5 @@ import { isDecimal } from "./src/isValid/isDecimal.js";
24
24
  import { isOctal } from "./src/isValid/isOctal.js";
25
25
  import { isNumber } from "./src/isValid/isNumber.js";
26
26
  import { FDR } from "./src/FDR/fdr.js";
27
- export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, isHex, isBinary, isDecimal, isOctal, isNumber, FDR };
27
+ import { mod } from "./src/mod/mod.js";
28
+ export { abs, add, cbrt, div, IQR, isEqual, isLeftGreater, isLeftGreaterOrEqual, MAD, max, mean, min, mul, Pipe, quartile, round, sort, sqrt, sub, toBase, isHex, isBinary, isDecimal, isOctal, isNumber, FDR, mod };
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.3.2",
4
+ "version": "2.4.0",
5
5
  "keywords": [
6
6
  "precision",
7
7
  "arithmetic",
@@ -54,7 +54,8 @@
54
54
  "binary",
55
55
  "cube root",
56
56
  "FDR",
57
- "Freedman-Diaconis Rule"
57
+ "Freedman-Diaconis Rule",
58
+ "mod"
58
59
  ],
59
60
  "repository": {
60
61
  "type": "git",
@@ -0,0 +1,2 @@
1
+ import type { Mod } from "./types.d.ts";
2
+ export declare const mod: Mod;
package/src/mod/mod.js ADDED
@@ -0,0 +1,6 @@
1
+ import { bi2s, s2bi } from "../shared/utils.js";
2
+ import { modInner } from "./utils.js";
3
+ export const mod = (left, right, options = { precision: 20 }) => {
4
+ const bi = modInner(s2bi(left), s2bi(right), options);
5
+ return bi2s(bi);
6
+ };
@@ -0,0 +1,6 @@
1
+ import type { BI } from "../shared/types.d.ts";
2
+ export type ModOptions = {
3
+ precision: number;
4
+ };
5
+ export type Mod = (left: string, right: string, options?: ModOptions) => string;
6
+ export type ModInner = (left: BI, right: BI, options: ModOptions) => BI;
@@ -0,0 +1,2 @@
1
+ import type { ModInner } from "./types.d.ts";
2
+ export declare const modInner: ModInner;
@@ -0,0 +1,6 @@
1
+ import { divInner } from "../operations/div/utils.js";
2
+ import { tryBigInt } from "../shared/utils.js";
3
+ export const modInner = (left, right, { precision }) => {
4
+ const bi = divInner([left, right], precision);
5
+ return bi[1] === 0 ? [0n, 0] : [bi[0] % (10n ** tryBigInt(bi[1])), 0];
6
+ };
@@ -1,4 +1,4 @@
1
- import { bi2s, calcInner, s2bi, tryBigInt, tryNumber } from "../../shared/utils.js";
1
+ import { bi2s, calcInner, s2bi, tryBigInt, tryNumber, } from "../../shared/utils.js";
2
2
  const MAX_SUM = 8e+15;
3
3
  const MIN_SUM = 8e-15;
4
4
  /** This function adds numbers (as string). */
@@ -1,4 +1,5 @@
1
1
  import type { BI2S } from "../shared/types.d.ts";
2
+ import type { ModOptions } from "../mod/types.d.ts";
2
3
  /** Using this function you can chain operations (add, sub, div, mul). */
3
4
  export declare class Pipe {
4
5
  #private;
@@ -8,6 +9,7 @@ export declare class Pipe {
8
9
  div(array: string[], precision?: number): Pipe;
9
10
  mul(array: string[]): Pipe;
10
11
  abs(): Pipe;
12
+ mod(value: string, options?: ModOptions): Pipe;
11
13
  calc(): ReturnType<BI2S>;
12
14
  resultToBase(radix: number): string;
13
15
  }
package/src/pipe/pipe.js CHANGED
@@ -13,6 +13,7 @@ var _Pipe_result;
13
13
  import { bi2s, calcInner, s2bi } from "../shared/utils.js";
14
14
  import { divInner } from "../operations/div/utils.js";
15
15
  import { absInner } from "../abs/utils.js";
16
+ import { modInner } from "../mod/utils.js";
16
17
  /** Using this function you can chain operations (add, sub, div, mul). */
17
18
  export class Pipe {
18
19
  constructor(value) {
@@ -46,6 +47,13 @@ export class Pipe {
46
47
  __classPrivateFieldSet(this, _Pipe_result, absInner(__classPrivateFieldGet(this, _Pipe_result, "f")), "f");
47
48
  return this;
48
49
  }
50
+ mod(value, options = { precision: 20 }) {
51
+ if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
52
+ throw new Error("Value is undefined.");
53
+ }
54
+ __classPrivateFieldSet(this, _Pipe_result, modInner(__classPrivateFieldGet(this, _Pipe_result, "f"), s2bi(value), options), "f");
55
+ return this;
56
+ }
49
57
  calc() {
50
58
  if (!__classPrivateFieldGet(this, _Pipe_result, "f")) {
51
59
  throw new Error("Cannot calculate based on an undefined input.");
@@ -1,4 +1,5 @@
1
1
  export type BI = [bigint, number];
2
+ export type Input = string[];
2
3
  export type FillHead = (len: number, fpe: number, isNeg: boolean, hasBefore: boolean) => string;
3
4
  export type TrimTail = (str: string) => string;
4
5
  export type CalcInner = (array: BI[], op: (a: bigint, b: bigint) => bigint, def?: BI) => BI;