cmath-js 1.0.3 → 1.0.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/LICENSE +1 -1
- package/README.md +13 -10
- package/dist/double/copysign.d.ts +1 -0
- package/dist/double/copysign.js +4 -0
- package/dist/double/copysign.js.map +1 -0
- package/dist/double/fabs.d.ts +1 -0
- package/dist/double/fabs.js +2 -0
- package/dist/double/fabs.js.map +1 -0
- package/dist/double/frexp.d.ts +1 -0
- package/dist/double/frexp.js +23 -0
- package/dist/double/frexp.js.map +1 -0
- package/dist/double/hypot.d.ts +1 -0
- package/dist/double/hypot.js +19 -0
- package/dist/double/hypot.js.map +1 -0
- package/dist/double/index.d.ts +8 -0
- package/dist/double/index.js +9 -0
- package/dist/double/index.js.map +1 -0
- package/dist/double/ldexp.d.ts +1 -0
- package/dist/double/ldexp.js +5 -0
- package/dist/double/ldexp.js.map +1 -0
- package/dist/double/nextafter.d.ts +1 -0
- package/dist/double/nextafter.js +22 -0
- package/dist/double/nextafter.js.map +1 -0
- package/dist/double/pow.d.ts +1 -0
- package/dist/double/pow.js +8 -0
- package/dist/double/pow.js.map +1 -0
- package/dist/double/signbit.d.ts +1 -0
- package/dist/double/signbit.js +4 -0
- package/dist/double/signbit.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/integer/abs.d.ts +1 -0
- package/dist/integer/abs.js +7 -0
- package/dist/integer/abs.js.map +1 -0
- package/dist/integer/index.d.ts +1 -0
- package/dist/integer/index.js +2 -0
- package/dist/integer/index.js.map +1 -0
- package/package.json +52 -35
- package/.vscode/ipch/5ab7b32c9fc14e9f/mmap_address.bin +0 -0
- package/.vscode/launch.json +0 -23
- package/.vscode/tasks.json +0 -22
- package/dist/cmath.d.ts +0 -9
- package/dist/cmath.js +0 -126
- package/dist/cmath.js.map +0 -1
- package/jest.config.json +0 -9
- package/src/cmath.ts +0 -146
- package/test/cmath.test.ts +0 -123
- package/tsconfig.json +0 -21
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
# cmath-js
|
|
2
|
-
Implementations of some of C's math functions in JavaScript
|
|
2
|
+
Implementations of some of C's math functions in TypeScript/JavaScript.
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
4
|
+
## Floating-point functions
|
|
5
|
+
- copysign
|
|
6
|
+
- fabs
|
|
7
|
+
- frexp
|
|
8
|
+
- hypot
|
|
9
|
+
- ldexp
|
|
10
|
+
- nextafter
|
|
11
|
+
- pow
|
|
12
|
+
- signbit
|
|
13
13
|
|
|
14
|
+
## Integer functions
|
|
15
|
+
These accept either a `number` or a `bigint`:
|
|
16
|
+
- abs
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function copysign(num: number, sign: number): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"copysign.js","sourceRoot":"","sources":["../../src/double/copysign.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,QAAQ,CAAY,GAAW,EAAa,IAAY;IACvE,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACtE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const fabs: (x: number) => number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fabs.js","sourceRoot":"","sources":["../../src/double/fabs.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function frexp(num: number): [fraction: number, exponent: number];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function frexp(num) {
|
|
2
|
+
const result = [num, 0];
|
|
3
|
+
if (num !== 0 && Number.isFinite(num)) {
|
|
4
|
+
const absNum = Math.abs(num);
|
|
5
|
+
let exp = Math.max(-1023, Math.floor(Math.log2(absNum)) + 1);
|
|
6
|
+
let x = absNum * 2 ** -exp;
|
|
7
|
+
while (x < 0.5) {
|
|
8
|
+
x *= 2;
|
|
9
|
+
--exp;
|
|
10
|
+
}
|
|
11
|
+
while (x >= 1) {
|
|
12
|
+
x *= 0.5;
|
|
13
|
+
++exp;
|
|
14
|
+
}
|
|
15
|
+
if (num < 0) {
|
|
16
|
+
x = -x;
|
|
17
|
+
}
|
|
18
|
+
result[0] = x;
|
|
19
|
+
result[1] = exp;
|
|
20
|
+
}
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=frexp.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"frexp.js","sourceRoot":"","sources":["../../src/double/frexp.ts"],"names":[],"mappings":"AAQA,MAAM,UAAU,KAAK,CACT,GAAW;IAEtB,MAAM,MAAM,GAAqB,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IAE1C,IAAI,GAAG,KAAK,CAAC,IAAI,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE;QACtC,MAAM,MAAM,GAAW,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAErC,IAAI,GAAG,GAAW,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;QACrE,IAAI,CAAC,GAAW,MAAM,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC;QAInC,OAAO,CAAC,GAAG,GAAG,EAAE;YACf,CAAC,IAAI,CAAC,CAAC;YACP,EAAE,GAAG,CAAC;SACN;QAGD,OAAO,CAAC,IAAI,CAAC,EAAE;YACd,CAAC,IAAI,GAAG,CAAC;YACT,EAAE,GAAG,CAAC;SACN;QAED,IAAI,GAAG,GAAG,CAAC,EAAE;YACZ,CAAC,GAAG,CAAC,CAAC,CAAC;SACP;QACD,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QACd,MAAM,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;KAChB;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function hypot(x: number, y: number, z?: number): number;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function hypot(x, y, z) {
|
|
2
|
+
let result = 0;
|
|
3
|
+
if (z !== undefined) {
|
|
4
|
+
result = Math.hypot(x, y, z);
|
|
5
|
+
}
|
|
6
|
+
else {
|
|
7
|
+
result = Infinity;
|
|
8
|
+
if (x !== Infinity && x !== -Infinity && y !== Infinity && y !== -Infinity) {
|
|
9
|
+
if (x === 0 || y === 0) {
|
|
10
|
+
result = Math.max(Math.abs(x), Math.abs(y));
|
|
11
|
+
}
|
|
12
|
+
else {
|
|
13
|
+
result = Math.hypot(x, y);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=hypot.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hypot.js","sourceRoot":"","sources":["../../src/double/hypot.ts"],"names":[],"mappings":"AASA,MAAM,UAAU,KAAK,CAAY,CAAS,EAAa,CAAS,EAAa,CAAU;IACtF,IAAI,MAAM,GAAW,CAAC,CAAC;IACvB,IAAI,CAAC,KAAK,SAAS,EAAE;QACpB,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;KAC7B;SAAM;QACN,MAAM,GAAG,QAAQ,CAAC;QAClB,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,QAAQ,IAAI,CAAC,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE;YAC3E,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;gBACvB,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;aAC5C;iBAAM;gBACN,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;aAC1B;SACD;KACD;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { copysign } from "./copysign.js";
|
|
2
|
+
export { fabs } from "./fabs.js";
|
|
3
|
+
export { frexp } from "./frexp.js";
|
|
4
|
+
export { hypot } from "./hypot.js";
|
|
5
|
+
export { ldexp } from "./ldexp.js";
|
|
6
|
+
export { nextafter } from "./nextafter.js";
|
|
7
|
+
export { pow } from "./pow.js";
|
|
8
|
+
export { signbit } from "./signbit.js";
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { copysign } from "./copysign.js";
|
|
2
|
+
export { fabs } from "./fabs.js";
|
|
3
|
+
export { frexp } from "./frexp.js";
|
|
4
|
+
export { hypot } from "./hypot.js";
|
|
5
|
+
export { ldexp } from "./ldexp.js";
|
|
6
|
+
export { nextafter } from "./nextafter.js";
|
|
7
|
+
export { pow } from "./pow.js";
|
|
8
|
+
export { signbit } from "./signbit.js";
|
|
9
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/double/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AACnC,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAC/B,OAAO,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function ldexp(factor: number, exponent: number): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ldexp.js","sourceRoot":"","sources":["../../src/double/ldexp.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,KAAK,CAAY,MAAc,EAAU,QAAgB;IACxE,MAAM,0BAA0B,GAAW,CAAC,IAAI,IAAI,CAAC,KAAK,CAAC,QAAQ,GAAG,GAAG,CAAC,CAAC;IAC3E,OAAO,CACN,MAAM,GAAG,0BAA0B,GAAG,0BAA0B,GAAG,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,GAAG,CAAC,CAAC,CAC/F,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function nextafter(num: number, toward: number): number;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function nextafter(num, toward) {
|
|
2
|
+
if (num === toward) {
|
|
3
|
+
return toward;
|
|
4
|
+
}
|
|
5
|
+
if (num === 0) {
|
|
6
|
+
return Math.sign(toward) * Number.MIN_VALUE;
|
|
7
|
+
}
|
|
8
|
+
if (num === Infinity || num === -Infinity) {
|
|
9
|
+
return Number.MAX_VALUE * Math.sign(num);
|
|
10
|
+
}
|
|
11
|
+
if (num === -Number.MIN_VALUE && toward > num) {
|
|
12
|
+
return -0;
|
|
13
|
+
}
|
|
14
|
+
let differenceMultiplier = 0.5 * Math.sign(num) * (num < toward ? 1 : -1);
|
|
15
|
+
let result;
|
|
16
|
+
do {
|
|
17
|
+
result = num + num * (Number.EPSILON * differenceMultiplier);
|
|
18
|
+
differenceMultiplier *= 2;
|
|
19
|
+
} while (result === num);
|
|
20
|
+
return result;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=nextafter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nextafter.js","sourceRoot":"","sources":["../../src/double/nextafter.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,SAAS,CAAY,GAAW,EAAa,MAAc;IAC1E,IAAI,GAAG,KAAK,MAAM,EAAE;QACnB,OAAO,MAAM,CAAC;KACd;IACD,IAAI,GAAG,KAAK,CAAC,EAAE;QACd,OAAO,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC,SAAS,CAAC;KAC5C;IACD,IAAI,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,CAAC,QAAQ,EAAE;QAC1C,OAAO,MAAM,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;KACzC;IACD,IAAI,GAAG,KAAK,CAAC,MAAM,CAAC,SAAS,IAAI,MAAM,GAAG,GAAG,EAAE;QAC9C,OAAO,CAAC,CAAC,CAAC;KACV;IACD,IAAI,oBAAoB,GAAG,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1E,IAAI,MAAc,CAAC;IACnB,GAAG;QACF,MAAM,GAAG,GAAG,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,OAAO,GAAG,oBAAoB,CAAC,CAAC;QAC7D,oBAAoB,IAAI,CAAC,CAAC;KAC1B,QAAQ,MAAM,KAAK,GAAG,EAAE;IACzB,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function pow(base: number, exponent: number): number;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pow.js","sourceRoot":"","sources":["../../src/double/pow.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,GAAG,CAAY,IAAY,EAAa,QAAgB;IACvE,IAAI,MAAM,GAAG,IAAI,IAAI,QAAQ,CAAC;IAC9B,IAAI,IAAI,KAAK,CAAC,IAAI,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,CAAC,QAAQ,KAAK,QAAQ,IAAI,QAAQ,KAAK,CAAC,QAAQ,CAAC,CAAC,EAAE;QACrF,MAAM,GAAG,CAAC,CAAC;KACX;IACD,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function signbit(num: number): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signbit.js","sourceRoot":"","sources":["../../src/double/signbit.ts"],"names":[],"mappings":"AACA,MAAM,UAAU,OAAO,CAAY,GAAW;IAC7C,OAAO,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC;AACtC,CAAC"}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AASA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function abs(number: bigint | number): number | bigint;
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { abs } from "./abs.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/integer/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,37 +1,54 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
2
|
+
"name": "cmath-js",
|
|
3
|
+
"homepage": "https://github.com/oskarlh/cmath.js",
|
|
4
|
+
"repository": "github:oskarlh/cmath.js",
|
|
5
|
+
"version": "1.0.5",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"type": "module",
|
|
9
|
+
"author": "Oskar Larsson Högfeldt <oskar@oskar.pm>",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"description": "C's math functions implemented in TypeScript/JavaScript",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"cmath",
|
|
14
|
+
"C",
|
|
15
|
+
"C++",
|
|
16
|
+
"Math",
|
|
17
|
+
"Maths",
|
|
18
|
+
"JavaScript",
|
|
19
|
+
"ECMAScript",
|
|
20
|
+
"TypeScript",
|
|
21
|
+
"Node",
|
|
22
|
+
"NodeJS",
|
|
23
|
+
"Web",
|
|
24
|
+
"Browser"
|
|
25
|
+
],
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@swc/cli": "^0.1.57",
|
|
28
|
+
"@swc/core": "^1.3.18",
|
|
29
|
+
"@swc/jest": "^0.2.23",
|
|
30
|
+
"@types/jest": "^29",
|
|
31
|
+
"@types/node": "^20",
|
|
32
|
+
"@typescript-eslint/eslint-plugin": "^6.2",
|
|
33
|
+
"@typescript-eslint/parser": "^6.2",
|
|
34
|
+
"eslint": "^8.5.0",
|
|
35
|
+
"eslint-config-prettier": "^8.3.0",
|
|
36
|
+
"eslint-plugin-prettier": "^5",
|
|
37
|
+
"jest": "^29.3.1",
|
|
38
|
+
"prettier": "^3",
|
|
39
|
+
"ts-jest": "^29.0.3",
|
|
40
|
+
"typescript": "~5.1"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsc",
|
|
44
|
+
"build-and-verify": "npm run test-with-typechecking --ci && npm run lint && npm run build",
|
|
45
|
+
"build-without-typechecking": "swc -C exclude=.test.ts -d dist -s true -- src",
|
|
46
|
+
"lint": "eslint .",
|
|
47
|
+
"test": "jest",
|
|
48
|
+
"test-with-typechecking": "jest --config=jest.config.with-typechecking.js"
|
|
49
|
+
},
|
|
50
|
+
"files": [
|
|
51
|
+
"dist",
|
|
52
|
+
"README.md"
|
|
53
|
+
]
|
|
37
54
|
}
|
|
Binary file
|
package/.vscode/launch.json
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// Use IntelliSense to learn about possible attributes.
|
|
3
|
-
// Hover to view descriptions of existing attributes.
|
|
4
|
-
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
-
"version": "0.2.0",
|
|
6
|
-
"configurations": [
|
|
7
|
-
{
|
|
8
|
-
"type": "node",
|
|
9
|
-
"request": "launch",
|
|
10
|
-
"name": "Launch Program",
|
|
11
|
-
"program": "${workspaceFolder}\\test\\cmath.test.ts",
|
|
12
|
-
"runtimeArgs": [
|
|
13
|
-
"--experimental-modules"
|
|
14
|
-
],
|
|
15
|
-
"outFiles": [
|
|
16
|
-
"${workspaceFolder}/dist/**/*.js"
|
|
17
|
-
]
|
|
18
|
-
}
|
|
19
|
-
]
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
package/.vscode/tasks.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
|
3
|
-
// for the documentation about the tasks.json format
|
|
4
|
-
"version": "2.0.0",
|
|
5
|
-
"tasks": [
|
|
6
|
-
{
|
|
7
|
-
"type": "typescript",
|
|
8
|
-
"tsconfig": "tsconfig.json",
|
|
9
|
-
"option": "watch",
|
|
10
|
-
"problemMatcher": [
|
|
11
|
-
"$tsc-watch"
|
|
12
|
-
]
|
|
13
|
-
},
|
|
14
|
-
{
|
|
15
|
-
"type": "typescript",
|
|
16
|
-
"tsconfig": "tsconfig.json",
|
|
17
|
-
"problemMatcher": [
|
|
18
|
-
"$tsc"
|
|
19
|
-
]
|
|
20
|
-
}
|
|
21
|
-
]
|
|
22
|
-
}
|
package/dist/cmath.d.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export declare function nextafter(/*double*/ num: number, /*double*/ toward: number): number;
|
|
2
|
-
export declare function pow(/*double*/ base: number, /*double*/ exponent: number): number;
|
|
3
|
-
export declare function signbit(/*double*/ num: number): boolean;
|
|
4
|
-
export declare function frexp(/*double*/ num: number): [/*double*/ number, /*int*/ number];
|
|
5
|
-
export declare function ldexp(/*double*/ factor: number, /*int*/ exponent: number): number;
|
|
6
|
-
export declare function copysign(/*double*/ num: number, /*double*/ sign: number): number;
|
|
7
|
-
export declare const fabs: (x: number) => number;
|
|
8
|
-
export declare const abs: (x: number) => number;
|
|
9
|
-
export declare function hypot(/*double*/ x: number, /*double*/ y: number, /*double*/ z?: number): number;
|
package/dist/cmath.js
DELETED
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
// These functions are JavaScript versions of math functions from C (version 2017) and C++ (version 2017)
|
|
2
|
-
// They follow the rules for IEEE-754 implementations
|
|
3
|
-
// References:
|
|
4
|
-
// C17: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.13.10.4
|
|
5
|
-
// C++17: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf
|
|
6
|
-
// IEEE-754:2008 (IEC 60559): http://irem.univ-reunion.fr/IMG/pdf/ieee-754-2008.pdf
|
|
7
|
-
// https://en.cppreference.com/
|
|
8
|
-
// When reading this it's important to remember that 0 === -0, but Object.is(0, -0) === false
|
|
9
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/nextafter
|
|
10
|
-
export function nextafter(/*double*/ num, /*double*/ toward) {
|
|
11
|
-
if (num === toward) {
|
|
12
|
-
return toward;
|
|
13
|
-
}
|
|
14
|
-
if (num === 0) {
|
|
15
|
-
return Math.sign(toward) * Number.MIN_VALUE;
|
|
16
|
-
}
|
|
17
|
-
if (num === Infinity || num === -Infinity) {
|
|
18
|
-
return Number.MAX_VALUE * Math.sign(num);
|
|
19
|
-
}
|
|
20
|
-
if (num === -Number.MIN_VALUE && toward > num) {
|
|
21
|
-
return -0;
|
|
22
|
-
}
|
|
23
|
-
let differenceMultiplier = 0.5 * Math.sign(num) * (num < toward ? 1 : -1);
|
|
24
|
-
let result;
|
|
25
|
-
do {
|
|
26
|
-
result = num + num * (Number.EPSILON * differenceMultiplier);
|
|
27
|
-
differenceMultiplier *= 2;
|
|
28
|
-
} while (result === num);
|
|
29
|
-
return result;
|
|
30
|
-
}
|
|
31
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/pow
|
|
32
|
-
// ECMAScript ** operator: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-applying-the-exp-operator
|
|
33
|
-
export function pow(/*double*/ base, /*double*/ exponent) {
|
|
34
|
-
let result = base ** exponent;
|
|
35
|
-
if (base === 1 || (base === -1 && (exponent === Infinity || exponent === -Infinity))) {
|
|
36
|
-
result = 1;
|
|
37
|
-
}
|
|
38
|
-
return result;
|
|
39
|
-
}
|
|
40
|
-
export function signbit(/*double*/ num) {
|
|
41
|
-
return Object.is(num, -0) || num < 0;
|
|
42
|
-
}
|
|
43
|
-
// Note: Instead of "double frexp(double arg, int* exp)" this is built as "[double, int] frexp(double arg)" due to ECMAScripts's lack of pointers
|
|
44
|
-
// A hypothetical issue with this implementation is that the precision the ** operator is not defined in the ECMAScript standard,
|
|
45
|
-
// however, sane ECMAScript implementations should give precise results for 2**<integer> expressions
|
|
46
|
-
// Cppreference: http://en.cppreference.com/w/c/numeric/math/frexp for a more detailed description
|
|
47
|
-
// Object.is(n, frexp(n)[0] * 2 ** frexp(n)[1]) for all number values of n except when Math.isFinite(n) && Math.abs(n) > 2**1023
|
|
48
|
-
// Object.is(n, (2 * frexp(n)[0]) * 2 ** (frexp(n)[1] - 1)) for all number values of n
|
|
49
|
-
// Object.is(n, frexp(n)[0]) for these values of n: 0, -0, NaN, Infinity, -Infinity
|
|
50
|
-
// Math.abs(frexp(n)[0]) is >= 0.5 and < 1.0 for any other number-type value of n
|
|
51
|
-
export function frexp(/*double*/ num) {
|
|
52
|
-
const result = [num, 0];
|
|
53
|
-
if (num !== 0 && Number.isFinite(num)) {
|
|
54
|
-
const absNum = Math.abs(num);
|
|
55
|
-
let exp = Math.max(-1023, Math.floor(Math.log2(absNum)) + 1);
|
|
56
|
-
let x = absNum * 2 ** -exp;
|
|
57
|
-
// These while loops compensate for rounding errors that may occur because of ECMAScript's Math.log2's undefined precision
|
|
58
|
-
// and the first one also helps work around the issue of 2 ** -exp === Infinity when exp <= -1024
|
|
59
|
-
while (x < 0.5) {
|
|
60
|
-
x *= 2;
|
|
61
|
-
--exp;
|
|
62
|
-
}
|
|
63
|
-
// istanbul ignore next This might not run and that's okay. See the above comment
|
|
64
|
-
while (x >= 1) {
|
|
65
|
-
x *= 0.5;
|
|
66
|
-
++exp;
|
|
67
|
-
}
|
|
68
|
-
if (num < 0) {
|
|
69
|
-
x = -x;
|
|
70
|
-
}
|
|
71
|
-
result[0] = x;
|
|
72
|
-
result[1] = exp;
|
|
73
|
-
}
|
|
74
|
-
return result;
|
|
75
|
-
}
|
|
76
|
-
// ldexp multiplies a floating-point number by an integral power of 2
|
|
77
|
-
// ldexp returns factor * 2**exponent
|
|
78
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.7.12.6
|
|
79
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/ldexp
|
|
80
|
-
// Implementation is complicated by the need to avoid underflow/overflow given a large exponent (-1075< >1023)
|
|
81
|
-
export function ldexp(/*double*/ factor, /*int*/ exponent) {
|
|
82
|
-
const halfPowerRoundedTowardZero = 2 ** Math.trunc(exponent * 0.5);
|
|
83
|
-
return factor * halfPowerRoundedTowardZero * halfPowerRoundedTowardZero * 2 ** Math.sign(exponent % 2);
|
|
84
|
-
}
|
|
85
|
-
// copysign produces a value with the magnitude of 'num' and the sign 'sign'
|
|
86
|
-
// Note: ECMAScript does not have negative NaNs
|
|
87
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.7.12.11
|
|
88
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/copysign
|
|
89
|
-
// The implementation is complicated by the need to handle positive and negative zero
|
|
90
|
-
export function copysign(/*double*/ num, /*double*/ sign) {
|
|
91
|
-
return Math.abs(num) * (Object.is(0 * Math.sign(sign), -0) ? -1 : 1);
|
|
92
|
-
}
|
|
93
|
-
// fabs is just like JavaScript's Math.abs
|
|
94
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/fabs
|
|
95
|
-
export const fabs = Math.abs;
|
|
96
|
-
// abs is like fabs but for integers
|
|
97
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/abs
|
|
98
|
-
export const abs = fabs;
|
|
99
|
-
// hypot computes the square root of the sum of the squares of x and y, without undue overflow or underflow
|
|
100
|
-
// This implementation allows an optional third argument, as specified in the C++ standard
|
|
101
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.7.12.7
|
|
102
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.13.10.4
|
|
103
|
-
// C++ spec for 3-arg version: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf#subsection.29.9.3
|
|
104
|
-
// Cppreference C version (limited to 2 args): https://en.cppreference.com/w/c/numeric/math/hypot
|
|
105
|
-
// Cppreference C++ version (2 or 3 args): https://en.cppreference.com/w/cpp/numeric/math/hypot
|
|
106
|
-
// ECMAScript's Math.hypot: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-math.hypot
|
|
107
|
-
// Complicated by the requirements for implementations for IEC 60559 floating-point environments, which thankfully only apply to the 2-arg (C) version
|
|
108
|
-
export function hypot(/*double*/ x, /*double*/ y, /*double*/ z) {
|
|
109
|
-
let result = 0;
|
|
110
|
-
if (z !== undefined) {
|
|
111
|
-
result = Math.hypot(x, y, z);
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
result = Infinity;
|
|
115
|
-
if (x !== Infinity && x !== -Infinity && y !== Infinity && y !== -Infinity) {
|
|
116
|
-
if (x === 0 || y === 0) {
|
|
117
|
-
result = Math.max(Math.abs(x), Math.abs(y));
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
result = Math.hypot(x, y);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return result;
|
|
125
|
-
}
|
|
126
|
-
//# sourceMappingURL=cmath.js.map
|
package/dist/cmath.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cmath.js","sourceRoot":"","sources":["../src/cmath.ts"],"names":[],"mappings":"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"}
|
package/jest.config.json
DELETED
package/src/cmath.ts
DELETED
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
// These functions are JavaScript versions of math functions from C (version 2017) and C++ (version 2017)
|
|
2
|
-
// They follow the rules for IEEE-754 implementations
|
|
3
|
-
// References:
|
|
4
|
-
// C17: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.13.10.4
|
|
5
|
-
// C++17: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf
|
|
6
|
-
// IEEE-754:2008 (IEC 60559): http://irem.univ-reunion.fr/IMG/pdf/ieee-754-2008.pdf
|
|
7
|
-
// https://en.cppreference.com/
|
|
8
|
-
// When reading this it's important to remember that 0 === -0, but Object.is(0, -0) === false
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/nextafter
|
|
12
|
-
export function nextafter(/*double*/ num : number, /*double*/ toward : number) : /*double*/ number {
|
|
13
|
-
if(num === toward) {
|
|
14
|
-
return toward;
|
|
15
|
-
}
|
|
16
|
-
if(num === 0) {
|
|
17
|
-
return Math.sign(toward) * Number.MIN_VALUE;
|
|
18
|
-
}
|
|
19
|
-
if(num === Infinity || num === -Infinity) {
|
|
20
|
-
return Number.MAX_VALUE * Math.sign(num);
|
|
21
|
-
}
|
|
22
|
-
if(num === -Number.MIN_VALUE && toward > num) {
|
|
23
|
-
return -0;
|
|
24
|
-
}
|
|
25
|
-
let differenceMultiplier = 0.5 * Math.sign(num) * (num < toward ? 1 : -1);
|
|
26
|
-
let result : number;
|
|
27
|
-
do {
|
|
28
|
-
result = num + num * (Number.EPSILON * differenceMultiplier);
|
|
29
|
-
differenceMultiplier *= 2;
|
|
30
|
-
} while(result === num);
|
|
31
|
-
return result;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/pow
|
|
36
|
-
// ECMAScript ** operator: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-applying-the-exp-operator
|
|
37
|
-
export function pow(/*double*/ base : number, /*double*/ exponent : number) : /*double*/ number {
|
|
38
|
-
let result = base ** exponent;
|
|
39
|
-
if(base === 1 || (base === -1 && (exponent === Infinity || exponent === -Infinity))) {
|
|
40
|
-
result = 1;
|
|
41
|
-
}
|
|
42
|
-
return result;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
export function signbit(/*double*/ num : number) : boolean {
|
|
47
|
-
return Object.is(num, -0) || num < 0;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
// Note: Instead of "double frexp(double arg, int* exp)" this is built as "[double, int] frexp(double arg)" due to ECMAScripts's lack of pointers
|
|
52
|
-
// A hypothetical issue with this implementation is that the precision the ** operator is not defined in the ECMAScript standard,
|
|
53
|
-
// however, sane ECMAScript implementations should give precise results for 2**<integer> expressions
|
|
54
|
-
// Cppreference: http://en.cppreference.com/w/c/numeric/math/frexp for a more detailed description
|
|
55
|
-
// Object.is(n, frexp(n)[0] * 2 ** frexp(n)[1]) for all number values of n except when Math.isFinite(n) && Math.abs(n) > 2**1023
|
|
56
|
-
// Object.is(n, (2 * frexp(n)[0]) * 2 ** (frexp(n)[1] - 1)) for all number values of n
|
|
57
|
-
// Object.is(n, frexp(n)[0]) for these values of n: 0, -0, NaN, Infinity, -Infinity
|
|
58
|
-
// Math.abs(frexp(n)[0]) is >= 0.5 and < 1.0 for any other number-type value of n
|
|
59
|
-
export function frexp(/*double*/ num : number) : [/*double*/ number, /*int*/ number] {
|
|
60
|
-
const result : [number, number] = [num, 0];
|
|
61
|
-
|
|
62
|
-
if(num !== 0 && Number.isFinite(num)) {
|
|
63
|
-
const absNum : number = Math.abs(num);
|
|
64
|
-
|
|
65
|
-
let exp : number = Math.max(-1023, Math.floor(Math.log2(absNum)) + 1);
|
|
66
|
-
let x : number = absNum * 2 ** -exp;
|
|
67
|
-
|
|
68
|
-
// These while loops compensate for rounding errors that may occur because of ECMAScript's Math.log2's undefined precision
|
|
69
|
-
// and the first one also helps work around the issue of 2 ** -exp === Infinity when exp <= -1024
|
|
70
|
-
while(x < 0.5) {
|
|
71
|
-
x *= 2;
|
|
72
|
-
--exp;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
// istanbul ignore next This might not run and that's okay. See the above comment
|
|
76
|
-
while(x >= 1) {
|
|
77
|
-
x *= 0.5;
|
|
78
|
-
++exp;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
if(num < 0) {
|
|
82
|
-
x = -x;
|
|
83
|
-
}
|
|
84
|
-
result[0] = x;
|
|
85
|
-
result[1] = exp;
|
|
86
|
-
}
|
|
87
|
-
return result;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
// ldexp multiplies a floating-point number by an integral power of 2
|
|
92
|
-
// ldexp returns factor * 2**exponent
|
|
93
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.7.12.6
|
|
94
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/ldexp
|
|
95
|
-
// Implementation is complicated by the need to avoid underflow/overflow given a large exponent (-1075< >1023)
|
|
96
|
-
export function ldexp(/*double*/ factor : number, /*int*/ exponent : number) : /*double*/ number {
|
|
97
|
-
const halfPowerRoundedTowardZero : number = 2 ** Math.trunc(exponent * 0.5);
|
|
98
|
-
return factor * halfPowerRoundedTowardZero * halfPowerRoundedTowardZero * 2 ** Math.sign(exponent % 2);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
// copysign produces a value with the magnitude of 'num' and the sign 'sign'
|
|
103
|
-
// Note: ECMAScript does not have negative NaNs
|
|
104
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.7.12.11
|
|
105
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/copysign
|
|
106
|
-
// The implementation is complicated by the need to handle positive and negative zero
|
|
107
|
-
export function copysign(/*double*/ num : number, /*double*/ sign : number) : /*double*/ number {
|
|
108
|
-
return Math.abs(num) * (Object.is(0 * Math.sign(sign), -0) ? -1 : 1);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
// fabs is just like JavaScript's Math.abs
|
|
113
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/fabs
|
|
114
|
-
export const fabs = Math.abs;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// abs is like fabs but for integers
|
|
118
|
-
// Cppreference: https://en.cppreference.com/w/c/numeric/math/abs
|
|
119
|
-
export const abs = fabs;
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
// hypot computes the square root of the sum of the squares of x and y, without undue overflow or underflow
|
|
123
|
-
// This implementation allows an optional third argument, as specified in the C++ standard
|
|
124
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.7.12.7
|
|
125
|
-
// C spec: https://web.archive.org/web/20181230041359if_/http://www.open-std.org/jtc1/sc22/wg14/www/abq/c17_updated_proposed_fdis.pdf#subsection.13.10.4
|
|
126
|
-
// C++ spec for 3-arg version: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf#subsection.29.9.3
|
|
127
|
-
// Cppreference C version (limited to 2 args): https://en.cppreference.com/w/c/numeric/math/hypot
|
|
128
|
-
// Cppreference C++ version (2 or 3 args): https://en.cppreference.com/w/cpp/numeric/math/hypot
|
|
129
|
-
// ECMAScript's Math.hypot: https://www.ecma-international.org/ecma-262/9.0/index.html#sec-math.hypot
|
|
130
|
-
// Complicated by the requirements for implementations for IEC 60559 floating-point environments, which thankfully only apply to the 2-arg (C) version
|
|
131
|
-
export function hypot(/*double*/ x : number, /*double*/ y : number, /*double*/ z ?: number) : number {
|
|
132
|
-
let result : number = 0;
|
|
133
|
-
if(z !== undefined) {
|
|
134
|
-
result = Math.hypot(x, y, z);
|
|
135
|
-
} else {
|
|
136
|
-
result = Infinity;
|
|
137
|
-
if(x !== Infinity && x !== -Infinity && y !== Infinity && y !== -Infinity) {
|
|
138
|
-
if(x === 0 || y === 0) {
|
|
139
|
-
result = Math.max(Math.abs(x), Math.abs(y));
|
|
140
|
-
} else {
|
|
141
|
-
result = Math.hypot(x, y);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
return result;
|
|
146
|
-
}
|
package/test/cmath.test.ts
DELETED
|
@@ -1,123 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
import * as cmath from "../src/cmath"
|
|
3
|
-
|
|
4
|
-
test("nextafter", () => {
|
|
5
|
-
expect(cmath.nextafter(-3.5, 3.5)).toBe(-3.5 + Number.EPSILON * 2);
|
|
6
|
-
expect(cmath.nextafter(1, 2)).toBe(1 + Number.EPSILON);
|
|
7
|
-
expect(cmath.nextafter(3, 4)).toBe(3 + Number.EPSILON*2);
|
|
8
|
-
expect(cmath.nextafter(3.5, 3.5)).toBe(3.5);
|
|
9
|
-
expect(cmath.nextafter(-3.5, -3.5)).toBe(-3.5);
|
|
10
|
-
expect(cmath.nextafter(1024, -Infinity)).toBe(1024 - Number.EPSILON * 1024/2);
|
|
11
|
-
expect(cmath.nextafter(-0, 0)).toBe(0);
|
|
12
|
-
expect(cmath.nextafter(0, -0)).toBe(-0);
|
|
13
|
-
expect(cmath.nextafter(-0, -0)).toBe(-0);
|
|
14
|
-
expect(cmath.nextafter(NaN, NaN)).toBe(NaN);
|
|
15
|
-
expect(cmath.nextafter(NaN, 0)).toBe(NaN);
|
|
16
|
-
expect(cmath.nextafter(0, NaN)).toBe(NaN);
|
|
17
|
-
expect(cmath.nextafter(Infinity, Infinity)).toBe(Infinity);
|
|
18
|
-
expect(cmath.nextafter(Infinity, -Infinity)).toBe(Number.MAX_VALUE);
|
|
19
|
-
expect(cmath.nextafter(-Infinity, Infinity)).toBe(-Number.MAX_VALUE);
|
|
20
|
-
expect(cmath.nextafter(-Infinity, -Infinity)).toBe(-Infinity);
|
|
21
|
-
expect(cmath.nextafter(Number.MAX_VALUE, Infinity)).toBe(Infinity);
|
|
22
|
-
expect(cmath.nextafter(-Number.MAX_VALUE, -Infinity)).toBe(-Infinity);
|
|
23
|
-
expect(cmath.nextafter(Number.MIN_VALUE, -1)).toBe(0);
|
|
24
|
-
expect(cmath.nextafter(-Number.MIN_VALUE, Infinity)).toBe(-0);
|
|
25
|
-
expect(cmath.nextafter(Number.MIN_VALUE, -0)).toBe(0);
|
|
26
|
-
expect(cmath.nextafter(Number.MIN_VALUE, 1)).toBe(Number.MIN_VALUE * 2);
|
|
27
|
-
expect(cmath.nextafter(-Number.MIN_VALUE, -1)).toBe(-Number.MIN_VALUE * 2);
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
test("pow", () => {
|
|
31
|
-
expect(cmath.pow(0, -3)).toBe(Infinity);
|
|
32
|
-
expect(cmath.pow(-0, -3)).toBe(-Infinity);
|
|
33
|
-
expect(cmath.pow(0, -4)).toBe(Infinity);
|
|
34
|
-
expect(cmath.pow(-0, -4)).toBe(Infinity);
|
|
35
|
-
expect(cmath.pow(0, -4.4)).toBe(Infinity);
|
|
36
|
-
expect(cmath.pow(-0, -4.4)).toBe(Infinity);
|
|
37
|
-
expect(cmath.pow(0, -Infinity)).toBe(Infinity);
|
|
38
|
-
expect(cmath.pow(-0, -Infinity)).toBe(Infinity);
|
|
39
|
-
expect(cmath.pow(NaN, 0)).toBe(1);
|
|
40
|
-
expect(cmath.pow(1, NaN)).toBe(1);
|
|
41
|
-
expect(cmath.pow(1, 45)).toBe(1);
|
|
42
|
-
expect(cmath.pow(1, -654132432423)).toBe(1);
|
|
43
|
-
expect(cmath.pow(-1, Infinity)).toBe(1);
|
|
44
|
-
expect(cmath.pow(-1, -Infinity)).toBe(1);
|
|
45
|
-
expect(cmath.pow(0, 555)).toBe(0);
|
|
46
|
-
expect(cmath.pow(-0, 713737315)).toBe(-0);
|
|
47
|
-
expect(cmath.pow(Infinity, -0.2)).toBe(0);
|
|
48
|
-
expect(cmath.pow(Infinity, 3737315.5)).toBe(Infinity);
|
|
49
|
-
expect(cmath.pow(-Infinity, 3737315)).toBe(-Infinity);
|
|
50
|
-
expect(cmath.pow(-Infinity, 3737315.5)).toBe(Infinity);
|
|
51
|
-
expect(cmath.pow(-3, 3)).toBe(-27);
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
test("signbit", () => {
|
|
56
|
-
expect(cmath.signbit(-Infinity)).toBe(true);
|
|
57
|
-
expect(cmath.signbit(-53245432534253)).toBe(true);
|
|
58
|
-
expect(cmath.signbit(-0)).toBe(true);
|
|
59
|
-
expect(cmath.signbit(0)).toBe(false);
|
|
60
|
-
expect(cmath.signbit(98970789063)).toBe(false);
|
|
61
|
-
expect(cmath.signbit(Infinity)).toBe(false);
|
|
62
|
-
expect(cmath.signbit(NaN)).toBe(false);
|
|
63
|
-
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
test("frexp", () => {
|
|
68
|
-
expect(cmath.frexp(1)).toStrictEqual([0.5, 1]);
|
|
69
|
-
expect(cmath.frexp(1.5)).toStrictEqual([0.75, 1]);
|
|
70
|
-
expect(cmath.frexp(3 * 2**500)).toStrictEqual([0.75, 502]);
|
|
71
|
-
expect(cmath.frexp(-4)).toStrictEqual([-0.5, 3]);
|
|
72
|
-
expect(cmath.frexp(Number.MAX_VALUE)).toStrictEqual([0.9999999999999999, 1024]);
|
|
73
|
-
expect(cmath.frexp(Number.MIN_VALUE)).toStrictEqual([0.5, -1073]);
|
|
74
|
-
expect(cmath.frexp(-Infinity)).toStrictEqual([-Infinity, 0]);
|
|
75
|
-
expect(cmath.frexp(-0)).toStrictEqual([-0, 0]);
|
|
76
|
-
expect(cmath.frexp(NaN)).toStrictEqual([NaN, 0]);
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
test("ldexp", () => {
|
|
81
|
-
expect(cmath.ldexp(1.5, -1)).toBe(0.75);
|
|
82
|
-
expect(cmath.ldexp(2**25, -3)).toBe(2**22);
|
|
83
|
-
expect(cmath.ldexp(2**-1072, 1073)).toBe(2);
|
|
84
|
-
expect(cmath.ldexp(2, 4)).toBe(2 ** 5);
|
|
85
|
-
expect(cmath.ldexp(2, 5)).toBe(2 ** 6);
|
|
86
|
-
expect(cmath.ldexp(NaN, -3)).toBe(NaN);
|
|
87
|
-
|
|
88
|
-
expect(cmath.ldexp(NaN, NaN)).toBe(NaN);
|
|
89
|
-
expect(cmath.ldexp(6, NaN)).toBe(NaN);
|
|
90
|
-
expect(cmath.ldexp(Infinity, 43)).toBe(Infinity);
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
test("copysign", () => {
|
|
95
|
-
expect(cmath.copysign(8, -4)).toBe(-8);
|
|
96
|
-
expect(cmath.copysign(-8, -0.00000000000000000000001)).toBe(-8);
|
|
97
|
-
expect(cmath.copysign(-Infinity, 0.00000000000000000000001)).toBe(Infinity);
|
|
98
|
-
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
test("abs and fabs", () => {
|
|
103
|
-
for(let func of [cmath.abs, cmath.fabs]) {
|
|
104
|
-
expect(func(123084109743)).toBe(123084109743);
|
|
105
|
-
expect(func(-123084109743)).toBe(123084109743);
|
|
106
|
-
expect(func(0)).toBe(0);
|
|
107
|
-
}
|
|
108
|
-
expect(cmath.fabs(12523423523523532432)).toBe(12523423523523532432);
|
|
109
|
-
expect(cmath.fabs(-52523423523523532444432)).toBe(52523423523523532444432);
|
|
110
|
-
expect(cmath.fabs(-0)).toBe(0);
|
|
111
|
-
expect(cmath.fabs(-Infinity)).toBe(Infinity);
|
|
112
|
-
expect(cmath.fabs(NaN)).toBe(NaN);
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
test("hypot", () => {
|
|
117
|
-
expect(cmath.hypot(105, 100)).toBeCloseTo(145, 3);
|
|
118
|
-
expect(cmath.hypot(-Infinity, 0)).toBe(Infinity);
|
|
119
|
-
expect(cmath.hypot(0, -0.1451493619437592)).toBe(0.1451493619437592);
|
|
120
|
-
expect(cmath.hypot(0, 0, -0.1451493619437592)).toBe(0.1451493619437592);
|
|
121
|
-
expect(cmath.hypot(-3, -2, -1)).toBeCloseTo(3.741657386773941, 3);
|
|
122
|
-
}, 1000);
|
|
123
|
-
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"declaration": true,
|
|
4
|
-
"module": "ES2020",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"noImplicitReturns": true,
|
|
7
|
-
"noUnusedLocals": true,
|
|
8
|
-
"outDir": "dist",
|
|
9
|
-
"preserveConstEnums": true,
|
|
10
|
-
"sourceMap": true,
|
|
11
|
-
"strict": true,
|
|
12
|
-
"target": "ES2020"
|
|
13
|
-
},
|
|
14
|
-
"include": [
|
|
15
|
-
"src/**/*"
|
|
16
|
-
],
|
|
17
|
-
"exclude": [
|
|
18
|
-
"node_modules",
|
|
19
|
-
"dist"
|
|
20
|
-
]
|
|
21
|
-
}
|