extra-math 1.3.21 โ†’ 1.3.24

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/index.d.ts CHANGED
@@ -1,196 +1,240 @@
1
1
  /**
2
2
  * Find the remainder of x/y with sign of x (truncated division).
3
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/rem)
3
4
  * @param x dividend
4
5
  * @param y divisor
5
6
  * @returns trunc(x/y)
6
- */
7
+ * */
8
+
7
9
  declare function rem(x: number, y: number): number;
8
10
  /**
9
11
  * Find the remainder of x/y with sign of y (floored division).
12
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/mod)
10
13
  * @param x dividend
11
14
  * @param y divisor
12
15
  * @returns floor(x/y)
13
- */
16
+ * */
17
+
14
18
  declare function mod(x: number, y: number): number;
15
19
  /**
16
20
  * Find the remainder of x/y with +ve sign (euclidean division).
21
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/modp)
17
22
  * @param x dividend
18
23
  * @param y divisor
19
24
  * @returns n>0: floor(x/y), n<0: ceil(x/y)
20
- */
25
+ * */
26
+
21
27
  declare function modp(x: number, y: number): number;
22
28
  /**
23
29
  * Find the greatest common divisor of numbers.
30
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/gcd)
24
31
  * @param xs a list of numbers
25
32
  * @returns gcd(xโ‚, xโ‚‚, ...)
26
- */
33
+ * */
34
+
27
35
  declare function gcd(...xs: number[]): number;
28
36
 
29
37
  /**
30
38
  * Find the least common multiple of numbers.
39
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/lcm)
31
40
  * @param xs a list of numbers
32
41
  * @returns lcm(xโ‚, xโ‚‚, ...)
33
- */
42
+ * */
43
+
34
44
  declare function lcm(...xs: number[]): number;
35
45
  /**
36
46
  * Find the factorial of a number.
47
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/factorial)
37
48
  * @param n a number
38
49
  * @param k denominator factorial [0]
39
50
  * @returns P(n, k); k=0: n!, k>0: n!/k!
40
- */
51
+ * */
52
+
41
53
  declare function factorial(n: number, k?: number): number;
42
54
  /**
43
55
  * Find the number of ways to choose k elements from a set of n elements.
56
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/binomial)
44
57
  * @param n elements in source set
45
58
  * @param k elements in choose set
46
59
  * @returns C(n, k)
47
- */
60
+ * */
61
+
48
62
  declare function binomial(n: number, k: number): number;
49
63
  /**
50
64
  * Find the number of ways to put n objects in m bins (n=sum(kแตข)).
65
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/multinomial)
51
66
  * @param ks objects per bin (kแตข)
52
67
  * @returns n!/(kโ‚!kโ‚‚!...) | n=sum(kแตข)
53
- */
68
+ * */
69
+
54
70
  declare function multinomial(...ks: number[]): number;
55
71
  /**
56
72
  * Constrain a number within a minimum and a maximum value.
73
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/constrain)
57
74
  * @param x a number
58
75
  * @param min minimum value
59
76
  * @param max maximum value
60
77
  * @returns x<min: min, x>max: max, x
61
- */
78
+ * */
79
+
62
80
  declare function constrain(x: number, min: number, max: number): number;
63
81
 
64
82
  /**
65
83
  * Normalize a number from its current range into a value between 0 and 1.
84
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/normalize)
66
85
  * @param x a number
67
86
  * @param r lower bound of current range
68
87
  * @param R upper bound of current range
69
88
  * @returns โˆˆ [0, 1]
70
- */
71
- declare function norm(x: number, r: number, R: number): number;
89
+ * */
90
+
91
+ declare function normalize(x: number, r: number, R: number): number;
92
+
72
93
  /**
73
94
  * Re-map a number from one range to another.
95
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/remap)
74
96
  * @param x a number
75
97
  * @param r lower bound of current range
76
98
  * @param R upper bound of current range
77
99
  * @param t lower bound of target range
78
100
  * @param T upper bound of target range
79
101
  * @returns โˆˆ [ymin, ymax]
80
- */
81
- declare function map(x: number, r: number, R: number, t: number, T: number): number;
102
+ * */
103
+
104
+ declare function remap(x: number, r: number, R: number, t: number, T: number): number;
105
+
82
106
  /**
83
107
  * Linearly interpolate a number between two numbers.
108
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/lerp)
84
109
  * @param x start number
85
110
  * @param y stop number
86
111
  * @param t interpolant โˆˆ [0, 1]
87
112
  * @returns โˆˆ [x, y]
88
- */
113
+ * */
114
+
89
115
  declare function lerp(x: number, y: number, t: number): number;
90
116
  /**
91
117
  * Find the nth root of a number (โฟโˆš).
118
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/root)
92
119
  * @param x a number
93
120
  * @param n root
94
121
  * @returns โฟโˆšx
95
- */
122
+ * */
123
+
96
124
  declare function root(x: number, n: number): number;
97
125
  /**
98
126
  * Find the logarithm of a number with a given base.
127
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/log)
99
128
  * @param x a number
100
129
  * @param b logarithm base [e]
101
130
  * @returns log_b (x)
102
- */
131
+ * */
132
+
103
133
  declare function log(x: number, b?: number): number;
104
- /**
105
- * Compute the gamma function of a number (ฮ“).
106
- * @param x a number
107
- * @returns ฮ“(x); for +ve integer ฮ“(x) = x!
108
- */
109
- declare function gamma(x: number): number;
110
- /**
111
- * Compute the natural logarithm of the absolute value of the gamma function of a number (log-ฮ“).
112
- * @param x a number
113
- * @returns log(|ฮ“(x)|)
114
- */
115
- declare function lgamma(x: number): number;
116
134
  /**
117
135
  * Find error function value of number (approximation).
136
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/erf)
118
137
  * @param x a number
119
138
  * @returns erf(x)
120
- */
139
+ * */
140
+
121
141
  declare function erf(x: number): number;
122
142
  /**
123
143
  * Find the complementary error function value of number (approximation).
144
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/erfc)
124
145
  * @param x a number
125
146
  * @returns erfc(x)
126
- */
147
+ * */
148
+
127
149
  declare function erfc(x: number): number;
128
150
  /**
129
151
  * Convert radians to degrees.
152
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/degrees)
130
153
  * @param x radians
131
154
  * @returns 2ฯ€ โ†’ 360
132
- */
155
+ * */
156
+
133
157
  declare function degrees(x: number): number;
134
158
  /**
135
159
  * Convert degrees to radians.
160
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/radians)
136
161
  * @param x degrees
137
162
  * @returns 360 โ†’ 2ฯ€
138
- */
163
+ * */
164
+
139
165
  declare function radians(x: number): number;
140
166
  /**
141
167
  * Calculate the magnitude (length) of a vector.
168
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/magnitude)
142
169
  * @param xs vector ([x, y, z, ...])
143
170
  * @returns โˆš(xยฒ + yยฒ + zยฒ)
144
- */
171
+ * */
172
+
145
173
  declare function magnitude(xs: number[]): number;
146
174
  /**
147
175
  * Calculate the distance between two points.
176
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/distance)
148
177
  * @param xs first point ([x, y, z, ...])
149
178
  * @param ys second point ([x, y, z, ...])
150
179
  * @returns โˆš(ฮ”xยฒ + ฮ”yยฒ + ฮ”zยฒ)
151
- */
180
+ * */
181
+
152
182
  declare function distance(xs: number[], ys: number[]): number;
153
183
  /**
154
184
  * Find the sum of numbers (ฮฃ).
185
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/sum)
155
186
  * @param xs a list of numbers
156
187
  * @returns ฮฃxแตข
157
- */
188
+ * */
189
+
158
190
  declare function sum(...xs: number[]): number;
159
191
  /**
160
192
  * Find the product of numbers (โˆ).
193
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/product)
161
194
  * @param xs a list of numbers
162
195
  * @returns โˆxแตข
163
- */
196
+ * */
197
+
164
198
  declare function product(...xs: number[]): number;
165
199
  /**
166
200
  * Find the average of numbers.
201
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/mean)
167
202
  * @param xs a list of numbers
168
203
  * @returns ฮฃxแตข/n | n = size(xs)
169
- */
204
+ * */
205
+
170
206
  declare function mean(...xs: number[]): number;
171
207
  /**
172
208
  * Find the value separating the higher and lower halves of numbers.
209
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/median)
173
210
  * @param xs a list of numbers
174
211
  * @returns xโ‚˜ | sort(xs) = [..., xโ‚˜, ...]
175
- */
212
+ * */
213
+
176
214
  declare function median(...xs: number[]): number;
177
215
  /**
178
216
  * Find the values that appear most often.
217
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/modes)
179
218
  * @param xs a list of numbers
180
219
  * @returns [xโ‚˜โ‚, xโ‚˜โ‚‚, ...] | count(xโ‚˜แตข) โ‰ฅ count(xแตข) โˆ€ xแตข โˆˆ xs
181
- */
220
+ * */
221
+
182
222
  declare function modes(...xs: number[]): number[];
183
223
  /**
184
224
  * Find the difference between the largest and smallest values.
225
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/range)
185
226
  * @param xs a list of numbers
186
227
  * @returns max(xs) - min(xs)
187
- */
228
+ * */
229
+
188
230
  declare function range(...xs: number[]): number;
189
231
  /**
190
232
  * Find the mean of squared deviation of numbers from its mean.
233
+ * [๐Ÿ“˜](https://github.com/nodef/extra-math/wiki/variance)
191
234
  * @param xs a list of numbers
192
235
  * @returns ฯƒยฒ = E[(xs - ยต)ยฒ] | ยต = mean(xs)
193
- */
236
+ * */
237
+
194
238
  declare function variance(...xs: number[]): number;
195
239
 
196
- export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gamma, gcd, gcd as hcf, lcm, lerp, lgamma, log, magnitude, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
240
+ export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gcd, gcd as hcf, lcm, lerp, log, magnitude, remap as map, mean, median, mod, modes, modp, multinomial, normalize as norm, normalize, product, radians, range, rem, remap, root, sum, variance };
package/index.js CHANGED
@@ -35,7 +35,7 @@ function factorial(n, k = 0) {
35
35
  if (n < 0)
36
36
  return 0;
37
37
  for (var i = k + 1, a = 1; i <= n; i++)
38
- a *= n;
38
+ a *= i;
39
39
  return a;
40
40
  }
41
41
  function binomial(n, k) {
@@ -61,32 +61,23 @@ function multinomial(...ks) {
61
61
  function constrain(x, min, max) {
62
62
  return Math.min(Math.max(x, min), max);
63
63
  }
64
- function norm(x, r, R) {
64
+ function normalize(x, r, R) {
65
65
  return (x - r) / (R - r);
66
66
  }
67
- function map(x, r, R, t, T) {
67
+ function remap(x, r, R, t, T) {
68
68
  return t + ((x - r) / (R - r)) * (T - t);
69
69
  }
70
70
  function lerp(x, y, t) {
71
71
  return x + t * (y - x);
72
72
  }
73
73
  function root(x, n) {
74
- return Math.pow(x, 1 / n);
74
+ if ((n & 1) === 0)
75
+ return Math.pow(x, 1 / n);
76
+ return Math.sign(x) * Math.pow(Math.abs(x), 1 / n);
75
77
  }
76
78
  function log(x, b = Math.E) {
77
79
  return Math.log(x) / Math.log(b);
78
80
  }
79
- function gamma(x) {
80
- var x = x - 1;
81
- var e1 = Math.sqrt(2 * Math.PI * x);
82
- var e2 = Math.pow(x / Math.E, x);
83
- var e3 = Math.pow(x * Math.sinh(1 / x), x / 2);
84
- var e4 = Math.exp((7 / 324) * (1 / ((x ** 3) * (35 * x ** 2 + 33))));
85
- return e1 * e2 * e3 * e4;
86
- }
87
- function lgamma(x) {
88
- return Math.log(Math.abs(gamma(x)));
89
- }
90
81
  const ERF_A1 = 0.254829592;
91
82
  const ERF_A2 = -0.284496736;
92
83
  const ERF_A3 = 1.421413741;
@@ -212,26 +203,26 @@ exports.distance = distance;
212
203
  exports.erf = erf;
213
204
  exports.erfc = erfc;
214
205
  exports.factorial = factorial;
215
- exports.gamma = gamma;
216
206
  exports.gcd = gcd;
217
207
  exports.hcf = gcd;
218
208
  exports.lcm = lcm;
219
209
  exports.lerp = lerp;
220
- exports.lgamma = lgamma;
221
210
  exports.log = log;
222
211
  exports.magnitude = magnitude;
223
- exports.map = map;
212
+ exports.map = remap;
224
213
  exports.mean = mean;
225
214
  exports.median = median;
226
215
  exports.mod = mod;
227
216
  exports.modes = modes;
228
217
  exports.modp = modp;
229
218
  exports.multinomial = multinomial;
230
- exports.norm = norm;
219
+ exports.norm = normalize;
220
+ exports.normalize = normalize;
231
221
  exports.product = product;
232
222
  exports.radians = radians;
233
223
  exports.range = range;
234
224
  exports.rem = rem;
225
+ exports.remap = remap;
235
226
  exports.root = root;
236
227
  exports.sum = sum;
237
228
  exports.variance = variance;
package/index.mjs CHANGED
@@ -31,7 +31,7 @@ function factorial(n, k = 0) {
31
31
  if (n < 0)
32
32
  return 0;
33
33
  for (var i = k + 1, a = 1; i <= n; i++)
34
- a *= n;
34
+ a *= i;
35
35
  return a;
36
36
  }
37
37
  function binomial(n, k) {
@@ -57,32 +57,23 @@ function multinomial(...ks) {
57
57
  function constrain(x, min, max) {
58
58
  return Math.min(Math.max(x, min), max);
59
59
  }
60
- function norm(x, r, R) {
60
+ function normalize(x, r, R) {
61
61
  return (x - r) / (R - r);
62
62
  }
63
- function map(x, r, R, t, T) {
63
+ function remap(x, r, R, t, T) {
64
64
  return t + ((x - r) / (R - r)) * (T - t);
65
65
  }
66
66
  function lerp(x, y, t) {
67
67
  return x + t * (y - x);
68
68
  }
69
69
  function root(x, n) {
70
- return Math.pow(x, 1 / n);
70
+ if ((n & 1) === 0)
71
+ return Math.pow(x, 1 / n);
72
+ return Math.sign(x) * Math.pow(Math.abs(x), 1 / n);
71
73
  }
72
74
  function log(x, b = Math.E) {
73
75
  return Math.log(x) / Math.log(b);
74
76
  }
75
- function gamma(x) {
76
- var x = x - 1;
77
- var e1 = Math.sqrt(2 * Math.PI * x);
78
- var e2 = Math.pow(x / Math.E, x);
79
- var e3 = Math.pow(x * Math.sinh(1 / x), x / 2);
80
- var e4 = Math.exp((7 / 324) * (1 / ((x ** 3) * (35 * x ** 2 + 33))));
81
- return e1 * e2 * e3 * e4;
82
- }
83
- function lgamma(x) {
84
- return Math.log(Math.abs(gamma(x)));
85
- }
86
77
  const ERF_A1 = 0.254829592;
87
78
  const ERF_A2 = -0.284496736;
88
79
  const ERF_A3 = 1.421413741;
@@ -200,4 +191,4 @@ function variance(...xs) {
200
191
  return a / xs.length;
201
192
  }
202
193
 
203
- export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gamma, gcd, gcd as hcf, lcm, lerp, lgamma, log, magnitude, map, mean, median, mod, modes, modp, multinomial, norm, product, radians, range, rem, root, sum, variance };
194
+ export { binomial, constrain as clamp, constrain, degrees, distance, erf, erfc, factorial, gcd, gcd as hcf, lcm, lerp, log, magnitude, remap as map, mean, median, mod, modes, modp, multinomial, normalize as norm, normalize, product, radians, range, rem, remap, root, sum, variance };
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ module.exports = {
2
+ collectCoverage: true,
3
+ coverageDirectory: "coverage",
4
+ coverageProvider: "v8",
5
+ transform: {
6
+ "^.+\\.(t|j)sx?$": "ts-jest"
7
+ },
8
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "extra-math",
3
- "version": "1.3.21",
3
+ "version": "1.3.24",
4
4
  "description": "A collection of common mathematical functions.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -10,15 +10,18 @@
10
10
  "import": "./index.mjs"
11
11
  },
12
12
  "scripts": {
13
- "test": "exit",
13
+ "test": "jest",
14
14
  "build": "node ./build",
15
- "deploy": "node ./build deploy"
15
+ "publish-docs": "node ./build publish-docs",
16
+ "publish-packages": "node ./build publish-packages"
16
17
  },
17
18
  "keywords": [
18
19
  "extra",
19
20
  "math",
20
21
  "clamp",
21
22
  "hcf",
23
+ "map",
24
+ "norm",
22
25
  "binomial",
23
26
  "constrain",
24
27
  "degrees",
@@ -26,25 +29,23 @@
26
29
  "erf",
27
30
  "erfc",
28
31
  "factorial",
29
- "gamma",
30
32
  "gcd",
31
33
  "lcm",
32
34
  "lerp",
33
- "lgamma",
34
35
  "log",
35
36
  "magnitude",
36
- "map",
37
37
  "mean",
38
38
  "median",
39
39
  "mod",
40
40
  "modes",
41
41
  "modp",
42
42
  "multinomial",
43
- "norm",
43
+ "normalize",
44
44
  "product",
45
45
  "radians",
46
46
  "range",
47
47
  "rem",
48
+ "remap",
48
49
  "root",
49
50
  "sum",
50
51
  "variance"
@@ -62,8 +63,11 @@
62
63
  "devDependencies": {
63
64
  "@rollup/plugin-commonjs": "^22.0.1",
64
65
  "@rollup/plugin-node-resolve": "^13.3.0",
65
- "extra-build": "^2.1.31",
66
+ "@types/jest": "^28.1.6",
67
+ "extra-build": "^2.2.21",
68
+ "jest": "^28.1.3",
66
69
  "rollup-plugin-cleanup": "^3.2.1",
67
- "rollup-plugin-dts": "^4.2.2"
70
+ "rollup-plugin-dts": "^4.2.2",
71
+ "ts-jest": "^28.0.7"
68
72
  }
69
73
  }