cmath-js 2.0.1 → 3.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.
package/README.md CHANGED
@@ -15,13 +15,21 @@ Implementation of parts of C's & C++'s numerics libraries in TypeScript/JavaScri
15
15
  These functions accept either a `bigint` or an integer `number`:
16
16
  - [`abs`](https://en.cppreference.com/w/c/numeric/math/abs)
17
17
  - [`countl_one`](https://en.cppreference.com/w/cpp/numeric/countl_one)
18
- - [`countl_zero`](https://en.cppreference.com/w/cpp/numeric/countl_zero)
18
+ - [`countl_zero`](https://en.cppreference.com/w/cpp/numeric/countl_zero) in the form of
19
+ ```ts
20
+ import { create_countl_zero } from "cmath-js";
21
+ const countl_zero_u16 = create_countl_zero({ bits: 16 });
22
+ const integer = 0x0FFF;
23
+ // Prints "0000111111111111 has 4 leading zero bits."
24
+ console.log(`${integer.toString(2).padStart(16, "0")} has ${countl_zero_u16(integer)} leading zero bits.`);
25
+ ```
19
26
  - [`countr_one`](https://en.cppreference.com/w/cpp/numeric/countr_one)
20
27
  - [`countr_zero`](https://en.cppreference.com/w/cpp/numeric/countr_zero)
21
28
  - [`div`](https://en.cppreference.com/w/cpp/numeric/math/div)
22
29
  - [`gcd`](https://en.cppreference.com/w/cpp/numeric/gcd). Example:
23
30
  ```ts
24
31
  import { gcd } from "cmath-js";
32
+ // Prints "The greatest common divisor of 24 and 32 is 8."
25
33
  console.log(`The greatest common divisor of 24 and 32 is ${gcd(24, 32)}.`);
26
34
  ```
27
35
  - [`lcm`](https://en.cppreference.com/w/cpp/numeric/lcm)
@@ -1,3 +1,5 @@
1
1
  export { bitsInUnsignedInteger } from "./bitsInUnsignedInteger.js";
2
2
  export { floatFromBits } from "./floatFromBits.js";
3
3
  export { floatOctets } from "./floatOctets.js";
4
+ export { sanitizeSmallUnsignedInteger } from "./sanitizeSmallUnsignedInteger.js";
5
+ export { sanitizeUnsignedInteger } from "./sanitizeUnsignedInteger.js";
@@ -1,4 +1,6 @@
1
1
  export { bitsInUnsignedInteger } from "./bitsInUnsignedInteger.js";
2
2
  export { floatFromBits } from "./floatFromBits.js";
3
3
  export { floatOctets } from "./floatOctets.js";
4
+ export { sanitizeSmallUnsignedInteger } from "./sanitizeSmallUnsignedInteger.js";
5
+ export { sanitizeUnsignedInteger } from "./sanitizeUnsignedInteger.js";
4
6
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/internal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/internal/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AACnE,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,4BAA4B,EAAE,MAAM,mCAAmC,CAAC;AACjF,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function sanitizeSmallUnsignedInteger(someNumber: number): number;
@@ -0,0 +1,7 @@
1
+ export function sanitizeSmallUnsignedInteger(someNumber) {
2
+ if (Number.isInteger(someNumber) && someNumber > 0) {
3
+ return someNumber;
4
+ }
5
+ return 0;
6
+ }
7
+ //# sourceMappingURL=sanitizeSmallUnsignedInteger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitizeSmallUnsignedInteger.js","sourceRoot":"","sources":["../../src/internal/sanitizeSmallUnsignedInteger.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,4BAA4B,CAAC,UAAkB;IAC9D,IAAI,MAAM,CAAC,SAAS,CAAC,UAAU,CAAC,IAAI,UAAU,GAAG,CAAC,EAAE,CAAC;QACpD,OAAO,UAAU,CAAC;IACnB,CAAC;IACD,OAAO,CAAC,CAAC;AACV,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function sanitizeUnsignedInteger(someNumber: number | bigint): bigint;
@@ -0,0 +1,11 @@
1
+ import { sanitizeSmallUnsignedInteger } from "./index.js";
2
+ export function sanitizeUnsignedInteger(someNumber) {
3
+ if (typeof someNumber === "bigint") {
4
+ if (someNumber < 0n) {
5
+ return 0n;
6
+ }
7
+ return someNumber;
8
+ }
9
+ return BigInt(sanitizeSmallUnsignedInteger(someNumber));
10
+ }
11
+ //# sourceMappingURL=sanitizeUnsignedInteger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sanitizeUnsignedInteger.js","sourceRoot":"","sources":["../../src/internal/sanitizeUnsignedInteger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,MAAM,YAAY,CAAC;AAE1D,MAAM,UAAU,uBAAuB,CAAC,UAA2B;IAClE,IAAI,OAAO,UAAU,KAAK,QAAQ,EAAE,CAAC;QACpC,IAAI,UAAU,GAAG,EAAE,EAAE,CAAC;YACrB,OAAO,EAAE,CAAC;QACX,CAAC;QACD,OAAO,UAAU,CAAC;IACnB,CAAC;IACD,OAAO,MAAM,CAAC,4BAA4B,CAAC,UAAU,CAAC,CAAC,CAAC;AACzD,CAAC"}
@@ -1,2 +1 @@
1
- export declare function countr_one(fixedIntegerSizeInBits: number, integer: bigint | number): number;
2
1
  export declare function countr_one(integer: bigint | number): number;
@@ -1,6 +1,6 @@
1
- import { bitsInUnsignedInteger } from "../../internal/index.js";
2
- export function countr_one(...values) {
3
- const digits = bitsInUnsignedInteger(values);
4
- return digits.length - digits.lastIndexOf(0) - 1;
1
+ import { sanitizeUnsignedInteger } from "../../internal/sanitizeUnsignedInteger.js";
2
+ export function countr_one(integer) {
3
+ const digits = sanitizeUnsignedInteger(integer).toString(2);
4
+ return digits.length - digits.lastIndexOf("0") - 1;
5
5
  }
6
6
  //# sourceMappingURL=countr_one.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"countr_one.js","sourceRoot":"","sources":["../../../src/namespace-std/integers/countr_one.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAUhE,MAAM,UAAU,UAAU,CACzB,GAAG,MAA+F;IAElG,MAAM,MAAM,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC7C,OAAO,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC"}
1
+ {"version":3,"file":"countr_one.js","sourceRoot":"","sources":["../../../src/namespace-std/integers/countr_one.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,2CAA2C,CAAC;AAqBpF,MAAM,UAAU,UAAU,CAAC,OAAwB;IAClD,MAAM,MAAM,GAAG,uBAAuB,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC5D,OAAO,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACpD,CAAC"}
@@ -0,0 +1,4 @@
1
+ export type countl_zero_function = (integer: bigint | number) => number;
2
+ export declare function create_countl_zero(options: {
3
+ bits: number;
4
+ }): countl_zero_function;
@@ -0,0 +1,12 @@
1
+ import { sanitizeSmallUnsignedInteger, sanitizeUnsignedInteger } from "../../internal/index.js";
2
+ export function create_countl_zero(options) {
3
+ const bits = sanitizeSmallUnsignedInteger(options.bits);
4
+ return (integer) => {
5
+ const truncated = BigInt.asUintN(bits, sanitizeUnsignedInteger(integer));
6
+ if (truncated === 0n) {
7
+ return bits;
8
+ }
9
+ return bits - truncated.toString(2).length;
10
+ };
11
+ }
12
+ //# sourceMappingURL=create_countl_zero.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create_countl_zero.js","sourceRoot":"","sources":["../../../src/namespace-std/integers/create_countl_zero.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,4BAA4B,EAAE,uBAAuB,EAAE,MAAM,yBAAyB,CAAC;AA4BhG,MAAM,UAAU,kBAAkB,CAAC,OAQlC;IACA,MAAM,IAAI,GAAG,4BAA4B,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACxD,OAAO,CAAC,OAAwB,EAAU,EAAE;QAC3C,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,IAAI,EAAE,uBAAuB,CAAC,OAAO,CAAC,CAAC,CAAC;QACzE,IAAI,SAAS,KAAK,EAAE,EAAE,CAAC;YACtB,OAAO,IAAI,CAAC;QACb,CAAC;QACD,OAAO,IAAI,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC;IAC5C,CAAC,CAAC;AACH,CAAC"}
@@ -1,8 +1,7 @@
1
1
  import { type div_t_bigint } from "./div_bigint.js";
2
2
  import { type div_t_number } from "./div_number.js";
3
- export interface div_t<NumType extends bigint | number = bigint | number> {
4
- quot: NumType;
5
- rem: NumType;
6
- }
3
+ type AnyInteger = bigint | number;
4
+ export type div_t<NumType extends AnyInteger = AnyInteger> = (NumType extends bigint ? div_t_bigint : div_t_number) & (NumType extends number ? div_t_number : div_t_bigint);
7
5
  export declare function div(aInteger: bigint, bInteger: bigint): div_t_bigint;
8
6
  export declare function div(aInteger: number, bInteger: number): div_t_number;
7
+ export {};
@@ -1 +1 @@
1
- {"version":3,"file":"div.js","sourceRoot":"","sources":["../../../../src/namespace-std/integers/div/div.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAqB,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAqB,MAAM,iBAAiB,CAAC;AAShE,MAAM,UAAU,GAAG,CAClB,QAAyB,EACzB,QAAyB;IAEzB,OAAO,OAAO,QAAQ,KAAK,QAAQ;QAClC,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE,QAAkB,CAAC;QAC1C,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE,QAAkB,CAAC,CAAC;AAC7C,CAAC"}
1
+ {"version":3,"file":"div.js","sourceRoot":"","sources":["../../../../src/namespace-std/integers/div/div.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAqB,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,UAAU,EAAqB,MAAM,iBAAiB,CAAC;AAWhE,MAAM,UAAU,GAAG,CAA6B,QAAiB,EAAE,QAAiB;IACnF,OAAO,OAAO,QAAQ,KAAK,QAAQ;QAClC,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE,QAAkB,CAAC;QAC1C,CAAC,CAAC,UAAU,CAAC,QAAQ,EAAE,QAAkB,CAAC,CAAC;AAC7C,CAAC"}
@@ -1,6 +1,6 @@
1
1
  export { abs } from "./abs.js";
2
2
  export { countl_one } from "./countl_one.js";
3
- export { countl_zero } from "./countl_zero.js";
3
+ export { create_countl_zero, type countl_zero_function } from "./create_countl_zero.js";
4
4
  export { countr_one } from "./countr_one.js";
5
5
  export { countr_zero } from "./countr_zero.js";
6
6
  export { div, type div_t, type div_t_bigint, type div_t_number } from "./div/index.js";
@@ -1,6 +1,6 @@
1
1
  export { abs } from "./abs.js";
2
2
  export { countl_one } from "./countl_one.js";
3
- export { countl_zero } from "./countl_zero.js";
3
+ export { create_countl_zero } from "./create_countl_zero.js";
4
4
  export { countr_one } from "./countr_one.js";
5
5
  export { countr_zero } from "./countr_zero.js";
6
6
  export { div } from "./div/index.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/namespace-std/integers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,GAAG,EAAoD,MAAM,gBAAgB,CAAC;AACvF,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/namespace-std/integers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAA6B,MAAM,yBAAyB,CAAC;AACxF,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,GAAG,EAAoD,MAAM,gBAAgB,CAAC;AACvF,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC"}
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/oskarlh/cmath-js.git"
7
7
  },
8
- "version": "2.0.1",
8
+ "version": "3.0.0",
9
9
  "exports": {
10
10
  ".": {
11
11
  "default": "./dist/index.js",
@@ -1 +0,0 @@
1
- export declare function countl_zero(fixedIntegerSizeInBits: number, integer: bigint | number): number;
@@ -1,10 +0,0 @@
1
- import { bitsInUnsignedInteger } from "../../internal/index.js";
2
- export function countl_zero(fixedIntegerSizeInBits, integer) {
3
- const digits = bitsInUnsignedInteger([fixedIntegerSizeInBits, integer]);
4
- const endOfZeroes = digits.indexOf(1);
5
- if (endOfZeroes === -1) {
6
- return digits.length;
7
- }
8
- return endOfZeroes;
9
- }
10
- //# sourceMappingURL=countl_zero.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"countl_zero.js","sourceRoot":"","sources":["../../../src/namespace-std/integers/countl_zero.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAShE,MAAM,UAAU,WAAW,CAAC,sBAA8B,EAAE,OAAwB;IACnF,MAAM,MAAM,GAAG,qBAAqB,CAAC,CAAC,sBAAsB,EAAE,OAAO,CAAC,CAAC,CAAC;IAExE,MAAM,WAAW,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEtC,IAAI,WAAW,KAAK,CAAC,CAAC,EAAE,CAAC;QACxB,OAAO,MAAM,CAAC,MAAM,CAAC;IACtB,CAAC;IACD,OAAO,WAAW,CAAC;AACpB,CAAC"}