UNPKG

12 kBTypeScriptView Raw
1/**
2 * Round down a number to specific precision.
3 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/floor)
4 * @param x a number
5 * @param pre to precision (1)
6 */
7declare function floor(x: number, pre?: number): number;
8/**
9 * Round up a number to specific precision.
10 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/ceil)
11 * @param x a number
12 * @param pre to precision [1]
13 */
14declare function ceil(x: number, pre?: number): number;
15/**
16 * Round a number to specific precision.
17 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/round)
18 * @param x a number
19 * @param pre to precision [1]
20 */
21declare function round(x: number, pre?: number): number;
22/**
23 * Perform floor-divison of two numbers.
24 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/floorDiv)
25 * @param x divisor
26 * @param y dividend
27 * @returns āŒŠx/yāŒ‹
28 */
29declare function floorDiv(x: number, y: number): number;
30/**
31 * Perform ceiling-divison of two numbers.
32 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/ceilDiv)
33 * @param x divisor
34 * @param y dividend
35 * @returns āŒˆx/yāŒ‰
36 */
37declare function ceilDiv(x: number, y: number): number;
38/**
39 * Perform rounded-divison of two numbers.
40 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/roundDiv)
41 * @param x divisor
42 * @param y dividend
43 * @returns [x/y]
44 */
45declare function roundDiv(x: number, y: number): number;
46/**
47 * Find the remainder of x/y with sign of x (truncated division).
48 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/rem)
49 * @param x dividend
50 * @param y divisor
51 * @returns trunc(x % y)
52 */
53declare function rem(x: number, y: number): number;
54/**
55 * Find the remainder of x/y with sign of y (floored division).
56 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/mod)
57 * @param x dividend
58 * @param y divisor
59 * @returns floor(x % y)
60 */
61declare function mod(x: number, y: number): number;
62/**
63 * Find the remainder of x/y with +ve sign (euclidean division).
64 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/modp)
65 * @param x dividend
66 * @param y divisor
67 * @returns n>0: floor(x % y), n<0: ceil(x % y)
68 */
69declare function modp(x: number, y: number): number;
70/**
71 * Constrain a number within a minimum and a maximum value.
72 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/constrain)
73 * @param x a number
74 * @param min minimum value
75 * @param max maximum value
76 * @returns x<min: min, x>max: max, x
77 */
78declare function constrain(x: number, min: number, max: number): number;
79
80/**
81 * Normalize a number from its current range into a value between 0 and 1.
82 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/normalize)
83 * @param x a number
84 * @param r lower bound of current range
85 * @param R upper bound of current range
86 * @returns āˆˆ [0, 1]
87 */
88declare function normalize(x: number, r: number, R: number): number;
89
90/**
91 * Re-map a number from one range to another.
92 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/remap)
93 * @param x a number
94 * @param r lower bound of current range
95 * @param R upper bound of current range
96 * @param t lower bound of target range
97 * @param T upper bound of target range
98 * @returns āˆˆ [ymin, ymax]
99 */
100declare function remap(x: number, r: number, R: number, t: number, T: number): number;
101
102/**
103 * Linearly interpolate a number between two numbers.
104 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/lerp)
105 * @param x start number
106 * @param y stop number
107 * @param t interpolant āˆˆ [0, 1]
108 * @returns āˆˆ [x, y]
109 */
110declare function lerp(x: number, y: number, t: number): number;
111/**
112 * Check if a number is a power-of-n.
113 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/isPow)
114 * @param x a number
115 * @param n base
116 * @returns nā± = x? | i = +ve integer
117 */
118declare function isPow(x: number, n: number): boolean;
119/**
120 * Find largest power-of-n less than or equal to given number.
121 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/prevPow)
122 * @param x a number
123 * @param n base
124 * @returns nā± | nā± ā‰¤ x and nā± > x/n
125 */
126declare function prevPow(x: number, n: number): number;
127/**
128 * Find smallest power-of-n greater than or equal to given number.
129 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/nextPow)
130 * @param x a number
131 * @param n base
132 * @returns nā± | nā± ā‰„ x and nā± < n*x
133 */
134declare function nextPow(x: number, n: number): number;
135/**
136 * Find the nth root of a number (āæāˆš).
137 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/root)
138 * @param x a number
139 * @param n root
140 * @returns āæāˆšx
141 */
142declare function root(x: number, n: number): number;
143/**
144 * Find the logarithm of a number with a given base.
145 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/log)
146 * @param x a number
147 * @param b logarithm base [e]
148 * @returns log_b (x)
149 */
150declare function log(x: number, b?: number): number;
151/**
152 * List all divisors of a number, except itself.
153 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/properDivisors)
154 * @param x a number
155 * @returns proper divisors (factors)
156 */
157declare function properDivisors(x: number): number[];
158
159/**
160 * Sum all proper divisors of a number.
161 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/aliquotSum)
162 * @param x a number
163 * @returns Ī£dįµ¢ | dįµ¢ is a divisor of x and ā‰ x
164 */
165declare function aliquotSum(x: number): number;
166/**
167 * Find the least prime number which divides a number.
168 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/minPrimeFactor)
169 * @param x a number
170 * @returns least prime factor
171 */
172declare function minPrimeFactor(x: number): number;
173
174/**
175 * Find the greatest prime number which divides a number.
176 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/maxPrimeFactor)
177 * @param x a number
178 * @returns greatest prime factor
179 */
180declare function maxPrimeFactor(x: number): number;
181
182/**
183 * Find the prime factors of a number.
184 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/primeFactors)
185 * @param x a number
186 * @returns [fā‚€, fā‚, ...] | fįµ¢ divides x and is prime
187 */
188declare function primeFactors(x: number): number[];
189/**
190 * Find the prime factors and respective exponents of a number.
191 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/primeExponentials)
192 * @param x a number
193 * @returns [[fā‚€, eā‚€], [fā‚, eā‚], ...] | fįµ¢ is a prime factor of x and eįµ¢ is its exponent
194 */
195declare function primeExponentials(x: number): [number, number][];
196/**
197 * Check if number is prime.
198 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/isPrime)
199 * @param x a number
200 * @returns is divisible by 1 and itself only?
201 */
202declare function isPrime(x: number): boolean;
203/**
204 * Find the greatest common divisor of numbers.
205 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/gcd)
206 * @param xs a list of numbers
207 * @returns gcd(xā‚, xā‚‚, ...)
208 */
209declare function gcd(...xs: number[]): number;
210
211/**
212 * Find the least common multiple of numbers.
213 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/lcm)
214 * @param xs a list of numbers
215 * @returns lcm(xā‚, xā‚‚, ...)
216 */
217declare function lcm(...xs: number[]): number;
218/**
219 * Find the factorial of a number.
220 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/factorial)
221 * @param n a number
222 * @param k denominator factorial [0]
223 * @returns P(n, k); k=0: n!, k>0: n!/k!
224 */
225declare function factorial(n: number, k?: number): number;
226/**
227 * Find the number of ways to choose k elements from a set of n elements.
228 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/binomial)
229 * @param n elements in source set
230 * @param k elements in choose set
231 * @returns C(n, k)
232 */
233declare function binomial(n: number, k: number): number;
234/**
235 * Find the number of ways to put n objects in m bins (n=sum(kįµ¢)).
236 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/multinomial)
237 * @param ks objects per bin (kįµ¢)
238 * @returns n!/(kā‚!kā‚‚!...) | n=sum(kįµ¢)
239 */
240declare function multinomial(...ks: number[]): number;
241/**
242 * Convert radians to degrees.
243 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/degrees)
244 * @param x radians
245 * @returns 2Ļ€ ā†’ 360
246 */
247declare function degrees(x: number): number;
248/**
249 * Convert degrees to radians.
250 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/radians)
251 * @param x degrees
252 * @returns 360 ā†’ 2Ļ€
253 */
254declare function radians(x: number): number;
255/**
256 * Find the sum of numbers (Ī£).
257 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/sum)
258 * @param xs a list of numbers
259 * @returns Ī£xįµ¢
260 */
261declare function sum(...xs: number[]): number;
262/**
263 * Find the product of numbers (āˆ).
264 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/product)
265 * @param xs a list of numbers
266 * @returns āˆxįµ¢
267 */
268declare function product(...xs: number[]): number;
269/**
270 * Find the value separating the higher and lower halves of numbers.
271 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/median)
272 * @param xs a list of numbers
273 * @returns xā‚˜ | sort(xs) = [..., xā‚˜, ...]
274 */
275declare function median(...xs: number[]): number;
276/**
277 * Find the values that appear most often.
278 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/modes)
279 * @param xs a list of numbers
280 * @returns [xā‚˜ā‚, xā‚˜ā‚‚, ...] | count(xā‚˜įµ¢) ā‰„ count(xįµ¢) āˆ€ xįµ¢ āˆˆ xs
281 */
282declare function modes(...xs: number[]): number[];
283/**
284 * Find the smallest and largest values.
285 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/range)
286 * @param xs a list of numbers
287 * @returns [min(xs), max(xs)]
288 */
289declare function range(...xs: number[]): [number, number];
290/**
291 * Find the mean of squared deviation of numbers from its mean.
292 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/variance)
293 * @param xs a list of numbers
294 * @returns ĻƒĀ² = E[(xs - Āµ)Ā²] | Āµ = mean(xs)
295 */
296declare function variance(...xs: number[]): number;
297/**
298 * Find the average of numbers.
299 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/arithmeticMean)
300 * @param xs a list of numbers
301 * @returns Ī£xįµ¢/n | n = size(xs)
302 */
303declare function arithmeticMean(...xs: number[]): number;
304
305/**
306 * Find the geometric mean of numbers.
307 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/geometricMean)
308 * @param xs a list of numbers
309 * @returns āæāˆš(āˆxįµ¢) | n = size(xs)
310 */
311declare function geometricMean(...xs: number[]): number;
312/**
313 * Find the harmonic mean of numbers.
314 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/harmonicMean)
315 * @param xs a list of numbers
316 * @returns n/Ī£(1/xįµ¢) | n = size(xs)
317 */
318declare function harmonicMean(...xs: number[]): number;
319/**
320 * Find the quadriatic mean of numbers.
321 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/quadriaticMean)
322 * @param xs a list of numbers
323 * @returns āˆš(Ī£xįµ¢Ā²)/n | n = size(xs)
324 */
325declare function quadriaticMean(...xs: number[]): number;
326
327/**
328 * Find the cubic mean of numbers.
329 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/cubicMean)
330 * @param xs a list of numbers
331 * @returns Ā³āˆš(Ī£xįµ¢Ā³)/n | n = size(xs)
332 */
333declare function cubicMean(...xs: number[]): number;
334
335/**
336 * Calculate the magnitude (length) of a vector.
337 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/magnitude)
338 * @param xs vector ([x, y, z, ...])
339 * @returns āˆš(xĀ² + yĀ² + zĀ²)
340 */
341declare function magnitude(xs: number[]): number;
342/**
343 * Calculate the distance between two points.
344 * [šŸ“˜](https://github.com/nodef/extra-math/wiki/distance)
345 * @param xs first point ([x, y, z, ...])
346 * @param ys second point ([x, y, z, ...])
347 * @returns āˆš(Ī”xĀ² + Ī”yĀ² + Ī”zĀ²)
348 */
349declare function distance(xs: number[], ys: number[]): number;
350
351export { properDivisors as aliquotParts, aliquotSum, arithmeticMean, binomial, ceil, ceilDiv, constrain as clamp, constrain, cubicMean, degrees, distance, factorial, floor, floorDiv, gcd, geometricMean, maxPrimeFactor as greatestPrimeFactor, harmonicMean, gcd as hcf, isPow, isPrime, lcm, minPrimeFactor as leastPrimeFactor, lerp, log, magnitude, remap as map, maxPrimeFactor, arithmeticMean as mean, median, minPrimeFactor, mod, modes, modp, multinomial, nextPow, normalize as norm, normalize, prevPow, primeExponentials, primeFactors, product, properDivisors, quadriaticMean, radians, range, rem, remap, root, quadriaticMean as rootMeanSquare, round, roundDiv, sum, variance };