@thi.ng/math 5.7.6 → 5.7.7
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 +1 -1
- package/README.md +1 -1
- package/abs.js +6 -2
- package/angle.js +54 -122
- package/api.js +42 -20
- package/crossing.js +8 -58
- package/eqdelta.js +6 -22
- package/extrema.js +27 -69
- package/fit.js +14 -57
- package/int.js +128 -64
- package/interval.js +73 -205
- package/libc.js +38 -107
- package/min-error.js +27 -39
- package/mix.js +109 -391
- package/package.json +7 -4
- package/permutations.js +18 -47
- package/prec.js +18 -31
- package/prime.js +31 -48
- package/ratio.js +18 -16
- package/safe-div.js +4 -7
- package/solve.js +37 -92
- package/step.js +14 -63
package/int.js
CHANGED
|
@@ -1,64 +1,128 @@
|
|
|
1
|
-
const M8 =
|
|
2
|
-
const M16 =
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
1
|
+
const M8 = 255;
|
|
2
|
+
const M16 = 65535;
|
|
3
|
+
const signExtend8 = (a) => (a &= M8, a & 128 ? a | ~M8 : a);
|
|
4
|
+
const signExtend16 = (a) => (a &= M16, a & 32768 ? a | ~M16 : a);
|
|
5
|
+
const addi8 = (a, b) => signExtend8((a | 0) + (b | 0));
|
|
6
|
+
const divi8 = (a, b) => signExtend8((a | 0) / (b | 0));
|
|
7
|
+
const muli8 = (a, b) => signExtend8((a | 0) * (b | 0));
|
|
8
|
+
const subi8 = (a, b) => signExtend8((a | 0) - (b | 0));
|
|
9
|
+
const andi8 = (a, b) => signExtend8((a | 0) & (b | 0));
|
|
10
|
+
const ori8 = (a, b) => signExtend8(a | 0 | (b | 0));
|
|
11
|
+
const xori8 = (a, b) => signExtend8((a | 0) ^ (b | 0));
|
|
12
|
+
const noti8 = (a) => signExtend8(~a);
|
|
13
|
+
const lshifti8 = (a, b) => signExtend8((a | 0) << (b | 0));
|
|
14
|
+
const rshifti8 = (a, b) => signExtend8((a | 0) >> (b | 0));
|
|
15
|
+
const addi16 = (a, b) => signExtend16((a | 0) + (b | 0));
|
|
16
|
+
const divi16 = (a, b) => signExtend16((a | 0) / (b | 0));
|
|
17
|
+
const muli16 = (a, b) => signExtend16((a | 0) * (b | 0));
|
|
18
|
+
const subi16 = (a, b) => signExtend16((a | 0) - (b | 0));
|
|
19
|
+
const andi16 = (a, b) => signExtend16((a | 0) & (b | 0));
|
|
20
|
+
const ori16 = (a, b) => signExtend16(a | 0 | (b | 0));
|
|
21
|
+
const xori16 = (a, b) => signExtend16((a | 0) ^ (b | 0));
|
|
22
|
+
const noti16 = (a) => signExtend16(~a);
|
|
23
|
+
const lshifti16 = (a, b) => signExtend16((a | 0) << (b | 0));
|
|
24
|
+
const rshifti16 = (a, b) => signExtend16((a | 0) >> (b | 0));
|
|
25
|
+
const addi32 = (a, b) => (a | 0) + (b | 0) | 0;
|
|
26
|
+
const divi32 = (a, b) => (a | 0) / (b | 0) | 0;
|
|
27
|
+
const muli32 = (a, b) => (a | 0) * (b | 0) | 0;
|
|
28
|
+
const subi32 = (a, b) => (a | 0) - (b | 0) | 0;
|
|
29
|
+
const andi32 = (a, b) => (a | 0) & (b | 0);
|
|
30
|
+
const ori32 = (a, b) => a | 0 | (b | 0);
|
|
31
|
+
const xori32 = (a, b) => (a | 0) ^ (b | 0);
|
|
32
|
+
const lshifti32 = (a, b) => (a | 0) << (b | 0);
|
|
33
|
+
const rshifti32 = (a, b) => (a | 0) >> (b | 0);
|
|
34
|
+
const noti32 = (a) => ~a;
|
|
35
|
+
const addu8 = (a, b) => (a & M8) + (b & M8) & M8;
|
|
36
|
+
const divu8 = (a, b) => (a & M8) / (b & M8) & M8;
|
|
37
|
+
const mulu8 = (a, b) => (a & M8) * (b & M8) & M8;
|
|
38
|
+
const subu8 = (a, b) => (a & M8) - (b & M8) & M8;
|
|
39
|
+
const andu8 = (a, b) => a & M8 & (b & M8) & M8;
|
|
40
|
+
const oru8 = (a, b) => (a & M8 | b & M8) & M8;
|
|
41
|
+
const xoru8 = (a, b) => (a & M8 ^ b & M8) & M8;
|
|
42
|
+
const notu8 = (a) => ~a & M8;
|
|
43
|
+
const lshiftu8 = (a, b) => (a & M8) << (b & M8) & M8;
|
|
44
|
+
const rshiftu8 = (a, b) => (a & M8) >>> (b & M8) & M8;
|
|
45
|
+
const addu16 = (a, b) => (a & M16) + (b & M16) & M16;
|
|
46
|
+
const divu16 = (a, b) => (a & M16) / (b & M16) & M16;
|
|
47
|
+
const mulu16 = (a, b) => (a & M16) * (b & M16) & M16;
|
|
48
|
+
const subu16 = (a, b) => (a & M16) - (b & M16) & M16;
|
|
49
|
+
const andu16 = (a, b) => a & M16 & (b & M16) & M16;
|
|
50
|
+
const oru16 = (a, b) => (a & M16 | b & M16) & M16;
|
|
51
|
+
const xoru16 = (a, b) => (a & M16 ^ b & M16) & M16;
|
|
52
|
+
const notu16 = (a) => ~a & M16;
|
|
53
|
+
const lshiftu16 = (a, b) => (a & M16) << (b & M16) & M16;
|
|
54
|
+
const rshiftu16 = (a, b) => (a & M16) >>> (b & M16) & M16;
|
|
55
|
+
const addu32 = (a, b) => (a >>> 0) + (b >>> 0) >>> 0;
|
|
56
|
+
const divu32 = (a, b) => (a >>> 0) / (b >>> 0) >>> 0;
|
|
57
|
+
const mulu32 = (a, b) => (a >>> 0) * (b >>> 0) >>> 0;
|
|
58
|
+
const subu32 = (a, b) => (a >>> 0) - (b >>> 0) >>> 0;
|
|
59
|
+
const andu32 = (a, b) => (a >>> 0 & b >>> 0) >>> 0;
|
|
60
|
+
const oru32 = (a, b) => (a >>> 0 | b >>> 0) >>> 0;
|
|
61
|
+
const xoru32 = (a, b) => (a >>> 0 ^ b >>> 0) >>> 0;
|
|
62
|
+
const notu32 = (a) => ~a >>> 0;
|
|
63
|
+
const lshiftu32 = (a, b) => a >>> 0 << (b >>> 0) >>> 0;
|
|
64
|
+
const rshiftu32 = (a, b) => a >>> 0 >>> (b >>> 0) >>> 0;
|
|
65
|
+
export {
|
|
66
|
+
addi16,
|
|
67
|
+
addi32,
|
|
68
|
+
addi8,
|
|
69
|
+
addu16,
|
|
70
|
+
addu32,
|
|
71
|
+
addu8,
|
|
72
|
+
andi16,
|
|
73
|
+
andi32,
|
|
74
|
+
andi8,
|
|
75
|
+
andu16,
|
|
76
|
+
andu32,
|
|
77
|
+
andu8,
|
|
78
|
+
divi16,
|
|
79
|
+
divi32,
|
|
80
|
+
divi8,
|
|
81
|
+
divu16,
|
|
82
|
+
divu32,
|
|
83
|
+
divu8,
|
|
84
|
+
lshifti16,
|
|
85
|
+
lshifti32,
|
|
86
|
+
lshifti8,
|
|
87
|
+
lshiftu16,
|
|
88
|
+
lshiftu32,
|
|
89
|
+
lshiftu8,
|
|
90
|
+
muli16,
|
|
91
|
+
muli32,
|
|
92
|
+
muli8,
|
|
93
|
+
mulu16,
|
|
94
|
+
mulu32,
|
|
95
|
+
mulu8,
|
|
96
|
+
noti16,
|
|
97
|
+
noti32,
|
|
98
|
+
noti8,
|
|
99
|
+
notu16,
|
|
100
|
+
notu32,
|
|
101
|
+
notu8,
|
|
102
|
+
ori16,
|
|
103
|
+
ori32,
|
|
104
|
+
ori8,
|
|
105
|
+
oru16,
|
|
106
|
+
oru32,
|
|
107
|
+
oru8,
|
|
108
|
+
rshifti16,
|
|
109
|
+
rshifti32,
|
|
110
|
+
rshifti8,
|
|
111
|
+
rshiftu16,
|
|
112
|
+
rshiftu32,
|
|
113
|
+
rshiftu8,
|
|
114
|
+
signExtend16,
|
|
115
|
+
signExtend8,
|
|
116
|
+
subi16,
|
|
117
|
+
subi32,
|
|
118
|
+
subi8,
|
|
119
|
+
subu16,
|
|
120
|
+
subu32,
|
|
121
|
+
subu8,
|
|
122
|
+
xori16,
|
|
123
|
+
xori32,
|
|
124
|
+
xori8,
|
|
125
|
+
xoru16,
|
|
126
|
+
xoru32,
|
|
127
|
+
xoru8
|
|
128
|
+
};
|
package/interval.js
CHANGED
|
@@ -1,207 +1,75 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
* Clamps value `x` to closed [-1 .. 1] interval.
|
|
23
|
-
*
|
|
24
|
-
* @param x -
|
|
25
|
-
*/
|
|
26
|
-
export const clamp11 = (x) => (x < -1 ? -1 : x > 1 ? 1 : x);
|
|
27
|
-
/**
|
|
28
|
-
* Clamps value `x` to closed [0 .. 0.5] interval.
|
|
29
|
-
*
|
|
30
|
-
* @param x -
|
|
31
|
-
*/
|
|
32
|
-
export const clamp05 = (x) => (x < 0 ? 0 : x > 0.5 ? 0.5 : x);
|
|
33
|
-
/**
|
|
34
|
-
* Returns 2-tuple of [min(x,y), max(x,y)].
|
|
35
|
-
*
|
|
36
|
-
* @param x
|
|
37
|
-
* @param y
|
|
38
|
-
*/
|
|
39
|
-
export const minMax = (x, y) => x < y ? [x, y] : [y, x];
|
|
40
|
-
/**
|
|
41
|
-
* Folds `x` back inside closed [min..max] interval. Also see
|
|
42
|
-
* {@link wrapOnce}.
|
|
43
|
-
*
|
|
44
|
-
* @param x -
|
|
45
|
-
* @param min -
|
|
46
|
-
* @param max -
|
|
47
|
-
*/
|
|
48
|
-
export const wrap = (x, min, max) => {
|
|
49
|
-
if (min === max)
|
|
50
|
-
return min;
|
|
51
|
-
if (x > max) {
|
|
52
|
-
const d = max - min;
|
|
53
|
-
x -= d;
|
|
54
|
-
if (x > max)
|
|
55
|
-
x -= d * (((x - min) / d) | 0);
|
|
56
|
-
}
|
|
57
|
-
else if (x < min) {
|
|
58
|
-
const d = max - min;
|
|
59
|
-
x += d;
|
|
60
|
-
if (x < min)
|
|
61
|
-
x += d * (((min - x) / d + 1) | 0);
|
|
62
|
-
}
|
|
63
|
-
return x;
|
|
1
|
+
const clamp = (x, min, max) => x < min ? min : x > max ? max : x;
|
|
2
|
+
const clamp0 = (x) => x > 0 ? x : 0;
|
|
3
|
+
const clamp01 = (x) => x < 0 ? 0 : x > 1 ? 1 : x;
|
|
4
|
+
const clamp11 = (x) => x < -1 ? -1 : x > 1 ? 1 : x;
|
|
5
|
+
const clamp05 = (x) => x < 0 ? 0 : x > 0.5 ? 0.5 : x;
|
|
6
|
+
const minMax = (x, y) => x < y ? [x, y] : [y, x];
|
|
7
|
+
const wrap = (x, min, max) => {
|
|
8
|
+
if (min === max)
|
|
9
|
+
return min;
|
|
10
|
+
if (x > max) {
|
|
11
|
+
const d = max - min;
|
|
12
|
+
x -= d;
|
|
13
|
+
if (x > max)
|
|
14
|
+
x -= d * ((x - min) / d | 0);
|
|
15
|
+
} else if (x < min) {
|
|
16
|
+
const d = max - min;
|
|
17
|
+
x += d;
|
|
18
|
+
if (x < min)
|
|
19
|
+
x += d * ((min - x) / d + 1 | 0);
|
|
20
|
+
}
|
|
21
|
+
return x;
|
|
64
22
|
};
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
export
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
: 3
|
|
118
|
-
: c >= d
|
|
119
|
-
? 2
|
|
120
|
-
: 3;
|
|
121
|
-
/**
|
|
122
|
-
* Returns the non-zero minimum value of the given `a`, `b` args.
|
|
123
|
-
*
|
|
124
|
-
* @param a -
|
|
125
|
-
* @param b -
|
|
126
|
-
*/
|
|
127
|
-
export const minNonZero2 = (a, b) => a !== 0 ? (b !== 0 ? Math.min(a, b) : a) : b;
|
|
128
|
-
/**
|
|
129
|
-
* Returns the non-zero minimum value of the given `a`, `b`, `c` args.
|
|
130
|
-
*
|
|
131
|
-
* @param a -
|
|
132
|
-
* @param b -
|
|
133
|
-
* @param c -
|
|
134
|
-
*/
|
|
135
|
-
export const minNonZero3 = (a, b, c) => minNonZero2(minNonZero2(a, b), c);
|
|
136
|
-
/**
|
|
137
|
-
* See `smax()`.
|
|
138
|
-
*
|
|
139
|
-
* @param a -
|
|
140
|
-
* @param b -
|
|
141
|
-
* @param k - smooth exponent (MUST be > 0)
|
|
142
|
-
*/
|
|
143
|
-
export const smin = (a, b, k) => smax(a, b, -k);
|
|
144
|
-
/**
|
|
145
|
-
* Smooth maximum. Note: Result values will be slightly larger than max value
|
|
146
|
-
* near max(a,b) + eps due to exponential decay. Higher `k` values reduce the
|
|
147
|
-
* error, but also reduce the smoothing. Recommended k=16.
|
|
148
|
-
*
|
|
149
|
-
* https://en.wikipedia.org/wiki/Smooth_maximum
|
|
150
|
-
*
|
|
151
|
-
* @param a -
|
|
152
|
-
* @param b -
|
|
153
|
-
* @param k - smooth exponent (MUST be > 0)
|
|
154
|
-
*/
|
|
155
|
-
export const smax = (a, b, k) => {
|
|
156
|
-
const ea = Math.exp(a * k);
|
|
157
|
-
const eb = Math.exp(b * k);
|
|
158
|
-
return (a * ea + b * eb) / (ea + eb);
|
|
23
|
+
const wrapOnce = (x, min, max) => x < min ? x - min + max : x > max ? x - max + min : x;
|
|
24
|
+
const wrap01 = (x) => x < 0 ? x + 1 : x > 1 ? x - 1 : x;
|
|
25
|
+
const wrap11 = (x) => x < -1 ? x + 2 : x > 1 ? x - 2 : x;
|
|
26
|
+
const min2id = (a, b) => a <= b ? 0 : 1;
|
|
27
|
+
const min3id = (a, b, c) => a <= b ? a <= c ? 0 : 2 : b <= c ? 1 : 2;
|
|
28
|
+
const min4id = (a, b, c, d) => a <= b ? a <= c ? a <= d ? 0 : 3 : c <= d ? 2 : 3 : b <= c ? b <= d ? 1 : 3 : c <= d ? 2 : 3;
|
|
29
|
+
const max2id = (a, b) => a >= b ? 0 : 1;
|
|
30
|
+
const max3id = (a, b, c) => a >= b ? a >= c ? 0 : 2 : b >= c ? 1 : 2;
|
|
31
|
+
const max4id = (a, b, c, d) => a >= b ? a >= c ? a >= d ? 0 : 3 : c >= d ? 2 : 3 : b >= c ? b >= d ? 1 : 3 : c >= d ? 2 : 3;
|
|
32
|
+
const minNonZero2 = (a, b) => a !== 0 ? b !== 0 ? Math.min(a, b) : a : b;
|
|
33
|
+
const minNonZero3 = (a, b, c) => minNonZero2(minNonZero2(a, b), c);
|
|
34
|
+
const smin = (a, b, k) => smax(a, b, -k);
|
|
35
|
+
const smax = (a, b, k) => {
|
|
36
|
+
const ea = Math.exp(a * k);
|
|
37
|
+
const eb = Math.exp(b * k);
|
|
38
|
+
return (a * ea + b * eb) / (ea + eb);
|
|
39
|
+
};
|
|
40
|
+
const sclamp = (x, min, max, k) => smin(smax(x, min, k), max, k);
|
|
41
|
+
const absMin = (a, b) => Math.abs(a) < Math.abs(b) ? a : b;
|
|
42
|
+
const absMax = (a, b) => Math.abs(a) > Math.abs(b) ? a : b;
|
|
43
|
+
const foldback = (e, x) => x < -e || x > e ? Math.abs(Math.abs((x - e) % (4 * e)) - 2 * e) - e : x;
|
|
44
|
+
const foldback01 = (x) => (x = Math.abs(x) % 2) > 1 ? 2 - x : x;
|
|
45
|
+
const inRange = (x, min, max) => x >= min && x <= max;
|
|
46
|
+
const inOpenRange = (x, min, max) => x > min && x < max;
|
|
47
|
+
export {
|
|
48
|
+
absMax,
|
|
49
|
+
absMin,
|
|
50
|
+
clamp,
|
|
51
|
+
clamp0,
|
|
52
|
+
clamp01,
|
|
53
|
+
clamp05,
|
|
54
|
+
clamp11,
|
|
55
|
+
foldback,
|
|
56
|
+
foldback01,
|
|
57
|
+
inOpenRange,
|
|
58
|
+
inRange,
|
|
59
|
+
max2id,
|
|
60
|
+
max3id,
|
|
61
|
+
max4id,
|
|
62
|
+
min2id,
|
|
63
|
+
min3id,
|
|
64
|
+
min4id,
|
|
65
|
+
minMax,
|
|
66
|
+
minNonZero2,
|
|
67
|
+
minNonZero3,
|
|
68
|
+
sclamp,
|
|
69
|
+
smax,
|
|
70
|
+
smin,
|
|
71
|
+
wrap,
|
|
72
|
+
wrap01,
|
|
73
|
+
wrap11,
|
|
74
|
+
wrapOnce
|
|
159
75
|
};
|
|
160
|
-
/**
|
|
161
|
-
* Same as `smin(smax(x, min, k), max, k)`.
|
|
162
|
-
*
|
|
163
|
-
* @param x -
|
|
164
|
-
* @param min -
|
|
165
|
-
* @param max -
|
|
166
|
-
* @param k -
|
|
167
|
-
*/
|
|
168
|
-
export const sclamp = (x, min, max, k) => smin(smax(x, min, k), max, k);
|
|
169
|
-
export const absMin = (a, b) => (Math.abs(a) < Math.abs(b) ? a : b);
|
|
170
|
-
export const absMax = (a, b) => (Math.abs(a) > Math.abs(b) ? a : b);
|
|
171
|
-
/**
|
|
172
|
-
* If `abs(x) > abs(e)`, recursively mirrors `x` back into `[-e .. +e]`
|
|
173
|
-
* interval at respective positive/negative boundary.
|
|
174
|
-
*
|
|
175
|
-
* @remarks
|
|
176
|
-
* References:
|
|
177
|
-
* - https://www.desmos.com/calculator/lkyf2ag3ta
|
|
178
|
-
* - https://www.musicdsp.org/en/latest/Effects/203-fold-back-distortion.html
|
|
179
|
-
*
|
|
180
|
-
* @param e - threshold (> 0)
|
|
181
|
-
* @param x - input value
|
|
182
|
-
*/
|
|
183
|
-
export const foldback = (e, x) => x < -e || x > e ? Math.abs(Math.abs((x - e) % (4 * e)) - 2 * e) - e : x;
|
|
184
|
-
/**
|
|
185
|
-
* Similar to {@link foldback}, but with fixed target range: Folds `x` into the
|
|
186
|
-
* closed [0..1] interval, using infinite internal reflection on either side of
|
|
187
|
-
* the interval.
|
|
188
|
-
*
|
|
189
|
-
* @param x
|
|
190
|
-
*/
|
|
191
|
-
export const foldback01 = (x) => ((x = Math.abs(x) % 2) > 1 ? 2 - x : x);
|
|
192
|
-
/**
|
|
193
|
-
* Returns true iff `x` is in closed interval `[min .. max]`
|
|
194
|
-
*
|
|
195
|
-
* @param x -
|
|
196
|
-
* @param min -
|
|
197
|
-
* @param max -
|
|
198
|
-
*/
|
|
199
|
-
export const inRange = (x, min, max) => x >= min && x <= max;
|
|
200
|
-
/**
|
|
201
|
-
* Returns true iff `x` is in open interval `(min .. max)`
|
|
202
|
-
*
|
|
203
|
-
* @param x -
|
|
204
|
-
* @param min -
|
|
205
|
-
* @param max -
|
|
206
|
-
*/
|
|
207
|
-
export const inOpenRange = (x, min, max) => x > min && x < max;
|
package/libc.js
CHANGED
|
@@ -1,109 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
export const fdim = (x, y) => Math.max(x - y, 0);
|
|
22
|
-
/**
|
|
23
|
-
* Returns `x * y + z`.
|
|
24
|
-
*
|
|
25
|
-
* @param x -
|
|
26
|
-
* @param y -
|
|
27
|
-
* @param z -
|
|
28
|
-
*/
|
|
29
|
-
export const fma = (x, y, z) => x * y + z;
|
|
30
|
-
/**
|
|
31
|
-
* Similar to {@link mod}, {@link remainder}. Returns `x - y * trunc(x / y)`,
|
|
32
|
-
* i.e. essentially the same as JS `%` operator. Result will always have the
|
|
33
|
-
* sign of `x`.
|
|
34
|
-
*
|
|
35
|
-
* @remarks
|
|
36
|
-
* **Caution:** Due to the introduction of libc math functions in v4.0.0 and the
|
|
37
|
-
* resulting name/behavior clashes between the modulo logic in JS, C & GLSL, the
|
|
38
|
-
* previous `fmod` function has been renamed to {@link mod} to align w/ its GLSL
|
|
39
|
-
* version and now exhibits a different behavior to this current {@link fmod}
|
|
40
|
-
* function.
|
|
41
|
-
*
|
|
42
|
-
* Reference: https://www.cplusplus.com/reference/cmath/fmod/
|
|
43
|
-
*
|
|
44
|
-
* @param x -
|
|
45
|
-
* @param y -
|
|
46
|
-
*/
|
|
47
|
-
export const fmod = (x, y) => x % y;
|
|
48
|
-
//export const fmod: FnN2 = (x, y) => x - y * Math.trunc(x / y);
|
|
49
|
-
/**
|
|
50
|
-
* Inverse op of {@link ldexp}. Breaks the number `x` into its binary
|
|
51
|
-
* significand (a floating point with an abs value in `[0.5,1.0)` interval and
|
|
52
|
-
* an integral exponent for 2, such that: `x = significand * 2^exp`. Returns
|
|
53
|
-
* tuple of `[sig, exp]`.
|
|
54
|
-
*
|
|
55
|
-
* @remarks
|
|
56
|
-
* - If `x` is zero, both parts (significand and exponent) are zero.
|
|
57
|
-
* - If `x` is negative, the significand returned by this function is negative.
|
|
58
|
-
*
|
|
59
|
-
* Based on:
|
|
60
|
-
* https://github.com/locutusjs/locutus/blob/master/src/c/math/frexp.js
|
|
61
|
-
*
|
|
62
|
-
* @param x -
|
|
63
|
-
*/
|
|
64
|
-
export const frexp = (x) => {
|
|
65
|
-
if (x === 0 || !isFinite(x))
|
|
66
|
-
return [x, 0];
|
|
67
|
-
const abs = Math.abs(x);
|
|
68
|
-
let exp = Math.max(-1023, Math.floor(Math.log2(abs)) + 1);
|
|
69
|
-
let y = abs * 2 ** -exp;
|
|
70
|
-
while (y < 0.5) {
|
|
71
|
-
y *= 2;
|
|
72
|
-
exp--;
|
|
73
|
-
}
|
|
74
|
-
while (y >= 1) {
|
|
75
|
-
y *= 0.5;
|
|
76
|
-
exp++;
|
|
77
|
-
}
|
|
78
|
-
return [x < 0 ? -y : y, exp];
|
|
1
|
+
const copysign = (x, y) => Math.sign(y) * Math.abs(x);
|
|
2
|
+
const exp2 = (x) => 2 ** x;
|
|
3
|
+
const fdim = (x, y) => Math.max(x - y, 0);
|
|
4
|
+
const fma = (x, y, z) => x * y + z;
|
|
5
|
+
const fmod = (x, y) => x % y;
|
|
6
|
+
const frexp = (x) => {
|
|
7
|
+
if (x === 0 || !isFinite(x))
|
|
8
|
+
return [x, 0];
|
|
9
|
+
const abs = Math.abs(x);
|
|
10
|
+
let exp = Math.max(-1023, Math.floor(Math.log2(abs)) + 1);
|
|
11
|
+
let y = abs * 2 ** -exp;
|
|
12
|
+
while (y < 0.5) {
|
|
13
|
+
y *= 2;
|
|
14
|
+
exp--;
|
|
15
|
+
}
|
|
16
|
+
while (y >= 1) {
|
|
17
|
+
y *= 0.5;
|
|
18
|
+
exp++;
|
|
19
|
+
}
|
|
20
|
+
return [x < 0 ? -y : y, exp];
|
|
79
21
|
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
* Computes both the quotient and remainder of the integer division of the
|
|
99
|
-
* numerator `x` by the denominator `y`.
|
|
100
|
-
*
|
|
101
|
-
* @param x -
|
|
102
|
-
* @param y -
|
|
103
|
-
*/
|
|
104
|
-
export const ldiv = (x, y) => {
|
|
105
|
-
x |= 0;
|
|
106
|
-
y |= 0;
|
|
107
|
-
const q = (x / y) | 0;
|
|
108
|
-
return [q, x - q * y];
|
|
22
|
+
const ldexp = (x, exp) => x * 2 ** exp;
|
|
23
|
+
const remainder = (x, y) => x - y * Math.round(x / y);
|
|
24
|
+
const ldiv = (x, y) => {
|
|
25
|
+
x |= 0;
|
|
26
|
+
y |= 0;
|
|
27
|
+
const q = x / y | 0;
|
|
28
|
+
return [q, x - q * y];
|
|
29
|
+
};
|
|
30
|
+
export {
|
|
31
|
+
copysign,
|
|
32
|
+
exp2,
|
|
33
|
+
fdim,
|
|
34
|
+
fma,
|
|
35
|
+
fmod,
|
|
36
|
+
frexp,
|
|
37
|
+
ldexp,
|
|
38
|
+
ldiv,
|
|
39
|
+
remainder
|
|
109
40
|
};
|