cmath-js 1.0.17 → 1.0.18

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
@@ -1,22 +1,24 @@
1
1
  # cmath-js
2
- Implementations of some of C's math functions in TypeScript/JavaScript.
2
+ Implementation of parts of C's & C++'s numerics libraries in TypeScript/JavaScript.
3
3
 
4
4
  ## Floating-point functions
5
5
  - [`copysign`](https://en.cppreference.com/w/c/numeric/math/copysign)
6
6
  - [`fabs`](https://en.cppreference.com/w/c/numeric/math/fabs)
7
7
  - [`frexp`](https://en.cppreference.com/w/c/numeric/math/frexp)
8
- - [`hypot`](https://en.cppreference.com/w/cpp/numeric/math/hypot)¹
8
+ - [`hypot`](https://en.cppreference.com/w/cpp/numeric/math/hypot)
9
9
  - [`ldexp`](https://en.cppreference.com/w/c/numeric/math/ldexp)
10
10
  - [`nextafter`](https://en.cppreference.com/w/c/numeric/math/nextafter)
11
11
  - [`pow`](https://en.cppreference.com/w/c/numeric/math/pow)
12
12
  - [`signbit`](https://en.cppreference.com/w/c/numeric/math/signbit)
13
13
 
14
- ### Notes
15
- 1: `hypot` is the C++ version which works just like the C version but accepts an optional third value.
16
-
17
14
  ## Integer functions
18
- This function accepts either a `number` or a `bigint`:
15
+ These functions accept either a `number` or a `bigint`:
19
16
  - [`abs`](https://en.cppreference.com/w/c/numeric/math/abs)
17
+ - [`countl_one`](https://en.cppreference.com/w/cpp/numeric/countl_one)
18
+ - [`countl_zero`](https://en.cppreference.com/w/cpp/numeric/countl_zero)
19
+ - [`countr_one`](https://en.cppreference.com/w/cpp/numeric/countr_one)
20
+ - [`countr_zero`](https://en.cppreference.com/w/cpp/numeric/countr_zero)
21
+ - [`popcount`](https://en.cppreference.com/w/cpp/numeric/popcount)
20
22
 
21
23
  ## Test coverage
22
24
  The test coverage is a perfect 100% and enforced by the publishing and pull request verification workflows.
@@ -1 +1,2 @@
1
- export declare function abs(number: bigint | number): number | bigint;
1
+ export declare function abs(integer: bigint): bigint;
2
+ export declare function abs(integer: number): number;
@@ -1,7 +1,7 @@
1
- export function abs(number) {
2
- if (number < 0 || Object.is(number, -0)) {
3
- return -number;
1
+ export function abs(integer) {
2
+ if (integer < 0 || Object.is(integer, -0)) {
3
+ return -integer;
4
4
  }
5
- return number;
5
+ return integer;
6
6
  }
7
7
  //# sourceMappingURL=abs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"abs.js","sourceRoot":"","sources":["../../src/integer/abs.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,GAAG,CAAC,MAAuB;IAC1C,IAAI,MAAM,GAAG,CAAC,IAAI,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE;QACxC,OAAO,CAAC,MAAM,CAAC;KACf;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
1
+ {"version":3,"file":"abs.js","sourceRoot":"","sources":["../../src/integer/abs.ts"],"names":[],"mappings":"AAIA,MAAM,UAAU,GAAG,CAAC,OAAwB;IAC3C,IAAI,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE;QAC1C,OAAO,CAAC,OAAO,CAAC;KAChB;IACD,OAAO,OAAO,CAAC;AAChB,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function bitsInUnsignedInteger(values: [fixedIntegerSizeInBits: number, integer: bigint | number] | [integer: bigint | number]): Uint8Array;
@@ -0,0 +1,30 @@
1
+ export function bitsInUnsignedInteger(values) {
2
+ let integer;
3
+ let fixedIntegerSizeInBits = NaN;
4
+ if (values.length === 1) {
5
+ [integer] = values;
6
+ }
7
+ else {
8
+ [fixedIntegerSizeInBits, integer] = values;
9
+ if (fixedIntegerSizeInBits <= 0 || !Number.isInteger(fixedIntegerSizeInBits)) {
10
+ return new Uint8Array();
11
+ }
12
+ }
13
+ const integerIsZeroOrInvalid = integer <= 0 || (typeof integer === "number" && !Number.isInteger(integer));
14
+ if (integerIsZeroOrInvalid) {
15
+ if (!fixedIntegerSizeInBits) {
16
+ return new Uint8Array();
17
+ }
18
+ integer = 0;
19
+ }
20
+ let bitString = BigInt(integer).toString(2);
21
+ if (bitString.length < fixedIntegerSizeInBits) {
22
+ bitString = bitString.padStart(fixedIntegerSizeInBits, "0");
23
+ }
24
+ const digits = Uint8Array.from(bitString);
25
+ if (digits.length > fixedIntegerSizeInBits) {
26
+ return digits.slice(-fixedIntegerSizeInBits);
27
+ }
28
+ return digits;
29
+ }
30
+ //# sourceMappingURL=bitsInUnsignedInteger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bitsInUnsignedInteger.js","sourceRoot":"","sources":["../../src/integer/bitsInUnsignedInteger.ts"],"names":[],"mappings":"AAOA,MAAM,UAAU,qBAAqB,CACpC,MAA+F;IAE/F,IAAI,OAAwB,CAAC;IAC7B,IAAI,sBAAsB,GAAW,GAAG,CAAC;IAEzC,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;QACxB,CAAC,OAAO,CAAC,GAAG,MAAM,CAAC;KACnB;SAAM;QACN,CAAC,sBAAsB,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC;QAE3C,IAAI,sBAAsB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,sBAAsB,CAAC,EAAE;YAC7E,OAAO,IAAI,UAAU,EAAE,CAAC;SACxB;KACD;IAED,MAAM,sBAAsB,GAC3B,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,OAAO,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAC;IAC7E,IAAI,sBAAsB,EAAE;QAC3B,IAAI,CAAC,sBAAsB,EAAE;YAC5B,OAAO,IAAI,UAAU,EAAE,CAAC;SACxB;QACD,OAAO,GAAG,CAAC,CAAC;KACZ;IAED,IAAI,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC;IAC5C,IAAI,SAAS,CAAC,MAAM,GAAG,sBAAsB,EAAE;QAC9C,SAAS,GAAG,SAAS,CAAC,QAAQ,CAAC,sBAAsB,EAAE,GAAG,CAAC,CAAC;KAC5D;IAED,MAAM,MAAM,GAAG,UAAU,CAAC,IAAI,CAAC,SAAqD,CAAC,CAAC;IAEtF,IAAI,MAAM,CAAC,MAAM,GAAG,sBAAsB,EAAE;QAC3C,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC,sBAAsB,CAAC,CAAC;KAC7C;IAED,OAAO,MAAM,CAAC;AACf,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function countl_one(fixedIntegerSizeInBits: number, integer: bigint | number): number;
2
+ export declare function countl_one(integer: bigint | number): number;
@@ -0,0 +1,10 @@
1
+ import { bitsInUnsignedInteger } from "./bitsInUnsignedInteger";
2
+ export function countl_one(...values) {
3
+ const digits = bitsInUnsignedInteger(values);
4
+ const endOfOnes = digits.indexOf(0);
5
+ if (endOfOnes === -1) {
6
+ return digits.length;
7
+ }
8
+ return endOfOnes;
9
+ }
10
+ //# sourceMappingURL=countl_one.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"countl_one.js","sourceRoot":"","sources":["../../src/integer/countl_one.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAShE,MAAM,UAAU,UAAU,CACzB,GAAG,MAA+F;IAElG,MAAM,MAAM,GAAG,qBAAqB,CAAC,MAAM,CAAC,CAAC;IAC7C,MAAM,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;IAEpC,IAAI,SAAS,KAAK,CAAC,CAAC,EAAE;QACrB,OAAO,MAAM,CAAC,MAAM,CAAC;KACrB;IACD,OAAO,SAAS,CAAC;AAClB,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function countl_zero(fixedIntegerSizeInBits: number, integer: bigint | number): number;
@@ -0,0 +1,10 @@
1
+ import { bitsInUnsignedInteger } from "./bitsInUnsignedInteger";
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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"countl_zero.js","sourceRoot":"","sources":["../../src/integer/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;QACvB,OAAO,MAAM,CAAC,MAAM,CAAC;KACrB;IACD,OAAO,WAAW,CAAC;AACpB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function countr_one(fixedIntegerSizeInBits: number, integer: bigint | number): number;
2
+ export declare function countr_one(integer: bigint | number): number;
@@ -0,0 +1,6 @@
1
+ import { bitsInUnsignedInteger } from "./bitsInUnsignedInteger";
2
+ export function countr_one(...values) {
3
+ const digits = bitsInUnsignedInteger(values);
4
+ return digits.length - digits.lastIndexOf(0) - 1;
5
+ }
6
+ //# sourceMappingURL=countr_one.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"countr_one.js","sourceRoot":"","sources":["../../src/integer/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"}
@@ -0,0 +1,2 @@
1
+ export declare function countr_zero(fixedIntegerSizeInBits: number, integer: bigint | number): number;
2
+ export declare function countr_zero(integer: bigint | number): number;
@@ -0,0 +1,6 @@
1
+ import { bitsInUnsignedInteger } from "./bitsInUnsignedInteger";
2
+ export function countr_zero(...values) {
3
+ const digits = bitsInUnsignedInteger(values);
4
+ return digits.length - digits.lastIndexOf(1) - 1;
5
+ }
6
+ //# sourceMappingURL=countr_zero.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"countr_zero.js","sourceRoot":"","sources":["../../src/integer/countr_zero.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAC;AAUhE,MAAM,UAAU,WAAW,CAC1B,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 +1,6 @@
1
1
  export { abs } from "./abs.js";
2
+ export { countl_one } from "./countl_one.js";
3
+ export { countl_zero } from "./countl_zero.js";
4
+ export { countr_one } from "./countr_one.js";
5
+ export { countr_zero } from "./countr_zero.js";
6
+ export { popcount } from "./popcount.js";
@@ -1,2 +1,7 @@
1
1
  export { abs } from "./abs.js";
2
+ export { countl_one } from "./countl_one.js";
3
+ export { countl_zero } from "./countl_zero.js";
4
+ export { countr_one } from "./countr_one.js";
5
+ export { countr_zero } from "./countr_zero.js";
6
+ export { popcount } from "./popcount.js";
2
7
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/integer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/integer/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,QAAQ,EAAE,MAAM,eAAe,CAAC"}
@@ -0,0 +1 @@
1
+ export declare function popcount(integer: bigint | number): number;
@@ -0,0 +1,11 @@
1
+ export function popcount(integer) {
2
+ if (integer <= 0 || (typeof integer === "number" && !Number.isInteger(integer))) {
3
+ return 0;
4
+ }
5
+ let count = 0;
6
+ for (const digit of BigInt(integer).toString(2)) {
7
+ count += Number(digit);
8
+ }
9
+ return count;
10
+ }
11
+ //# sourceMappingURL=popcount.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"popcount.js","sourceRoot":"","sources":["../../src/integer/popcount.ts"],"names":[],"mappings":"AAEA,MAAM,UAAU,QAAQ,CAAC,OAAwB;IAChD,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,OAAO,KAAK,QAAQ,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,EAAE;QAChF,OAAO,CAAC,CAAC;KACT;IACD,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,KAAK,MAAM,KAAK,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE;QAChD,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;KACvB;IACD,OAAO,KAAK,CAAC;AACd,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": "1.0.17",
8
+ "version": "1.0.18",
9
9
  "exports": "./dist/index.js",
10
10
  "types": "./dist/index.d.ts",
11
11
  "type": "module",