calcufly-math 1.0.0
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 +21 -0
- package/README.md +63 -0
- package/index.js +525 -0
- package/package.json +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 CalcuFly (https://calcufly.com)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# calcufly-math
|
|
2
|
+
|
|
3
|
+
Math and statistics calculator functions for Node.js. Percentages, fractions, geometry, algebra, statistics, trigonometry and more.
|
|
4
|
+
|
|
5
|
+
Part of the [CalcuFly](https://calcufly.com) calculator suite — 600+ free online calculators.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install calcufly-math
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
const math = require('calcufly-math');
|
|
17
|
+
|
|
18
|
+
// Quadratic equation
|
|
19
|
+
const roots = math.quadratic(1, -5, 6);
|
|
20
|
+
console.log(roots); // { x1: 3, x2: 2 }
|
|
21
|
+
|
|
22
|
+
// Statistics
|
|
23
|
+
const data = [4, 8, 15, 16, 23, 42];
|
|
24
|
+
console.log(math.mean(data)); // 18
|
|
25
|
+
console.log(math.median(data)); // 15.5
|
|
26
|
+
console.log(math.standardDeviation(data));
|
|
27
|
+
|
|
28
|
+
// Geometry
|
|
29
|
+
console.log(math.circleArea(5)); // 78.54
|
|
30
|
+
console.log(math.sphereVolume(3)); // 113.10
|
|
31
|
+
console.log(math.pythagorean(3, 4)); // 5
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## API Reference
|
|
35
|
+
|
|
36
|
+
| Function | Description |
|
|
37
|
+
|----------|-------------|
|
|
38
|
+
| `percentage(value, percent)` | Percentage of a value |
|
|
39
|
+
| `percentageChange(old, new)` | Percentage change |
|
|
40
|
+
| `gcd(a, b)` | Greatest common divisor |
|
|
41
|
+
| `lcm(a, b)` | Least common multiple |
|
|
42
|
+
| `quadratic(a, b, c)` | Solve quadratic equation |
|
|
43
|
+
| `mean(numbers)` | Average |
|
|
44
|
+
| `median(numbers)` | Median value |
|
|
45
|
+
| `mode(numbers)` | Most frequent values |
|
|
46
|
+
| `standardDeviation(numbers)` | Std deviation |
|
|
47
|
+
| `correlation(x, y)` | Pearson correlation |
|
|
48
|
+
| `factorial(n)` | Factorial |
|
|
49
|
+
| `permutations(n, r)` | Permutations |
|
|
50
|
+
| `combinations(n, r)` | Combinations |
|
|
51
|
+
| `circleArea(r)` | Circle area |
|
|
52
|
+
| `triangleAreaHeron(a, b, c)` | Triangle area (Heron) |
|
|
53
|
+
| `sphereVolume(r)` | Sphere volume |
|
|
54
|
+
| `pythagorean(a, b)` | Hypotenuse |
|
|
55
|
+
| `isPrime(n)` | Prime check |
|
|
56
|
+
| `primeFactors(n)` | Prime factorization |
|
|
57
|
+
| `fibonacci(n)` | Fibonacci sequence |
|
|
58
|
+
|
|
59
|
+
Try these calculations online at **[CalcuFly.com](https://calcufly.com)** — free, no signup required.
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
MIT — [CalcuFly](https://calcufly.com)
|
package/index.js
ADDED
|
@@ -0,0 +1,525 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* calcufly-math
|
|
3
|
+
* Math and statistics calculator functions.
|
|
4
|
+
* Part of the CalcuFly calculator suite — https://calcufly.com
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
// ─── PERCENTAGE ───────────────────────────────────────────────────────────────
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Calculate a percentage of a value.
|
|
13
|
+
* @param {number} value - The base value.
|
|
14
|
+
* @param {number} percent - The percentage to apply.
|
|
15
|
+
* @returns {number}
|
|
16
|
+
*/
|
|
17
|
+
function percentage(value, percent) {
|
|
18
|
+
return (value * percent) / 100;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Calculate the percentage change between two values.
|
|
23
|
+
* @param {number} oldVal
|
|
24
|
+
* @param {number} newVal
|
|
25
|
+
* @returns {{ change: number, percentChange: number }}
|
|
26
|
+
*/
|
|
27
|
+
function percentageChange(oldVal, newVal) {
|
|
28
|
+
const change = newVal - oldVal;
|
|
29
|
+
const percentChange = (change / Math.abs(oldVal)) * 100;
|
|
30
|
+
return { change, percentChange };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Calculate what percentage a part is of a whole.
|
|
35
|
+
* @param {number} part
|
|
36
|
+
* @param {number} whole
|
|
37
|
+
* @returns {number}
|
|
38
|
+
*/
|
|
39
|
+
function percentageOf(part, whole) {
|
|
40
|
+
return (part / whole) * 100;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// ─── FRACTIONS ────────────────────────────────────────────────────────────────
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Convert a fraction to a decimal.
|
|
47
|
+
* @param {number} numerator
|
|
48
|
+
* @param {number} denominator
|
|
49
|
+
* @returns {number}
|
|
50
|
+
*/
|
|
51
|
+
function fractionToDecimal(numerator, denominator) {
|
|
52
|
+
if (denominator === 0) throw new Error('Denominator cannot be zero.');
|
|
53
|
+
return numerator / denominator;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Convert a decimal to a fraction (approximate).
|
|
58
|
+
* @param {number} decimal
|
|
59
|
+
* @param {number} [tolerance=1e-6]
|
|
60
|
+
* @returns {{ numerator: number, denominator: number }}
|
|
61
|
+
*/
|
|
62
|
+
function decimalToFraction(decimal, tolerance = 1e-6) {
|
|
63
|
+
if (decimal === 0) return { numerator: 0, denominator: 1 };
|
|
64
|
+
const sign = decimal < 0 ? -1 : 1;
|
|
65
|
+
decimal = Math.abs(decimal);
|
|
66
|
+
let h1 = 1, h2 = 0, k1 = 0, k2 = 1;
|
|
67
|
+
let b = decimal;
|
|
68
|
+
do {
|
|
69
|
+
const a = Math.floor(b);
|
|
70
|
+
let aux = h1;
|
|
71
|
+
h1 = a * h1 + h2;
|
|
72
|
+
h2 = aux;
|
|
73
|
+
aux = k1;
|
|
74
|
+
k1 = a * k1 + k2;
|
|
75
|
+
k2 = aux;
|
|
76
|
+
b = 1 / (b - a);
|
|
77
|
+
} while (Math.abs(decimal - h1 / k1) > decimal * tolerance);
|
|
78
|
+
return { numerator: sign * h1, denominator: k1 };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Simplify a fraction to its lowest terms.
|
|
83
|
+
* @param {number} numerator
|
|
84
|
+
* @param {number} denominator
|
|
85
|
+
* @returns {{ numerator: number, denominator: number }}
|
|
86
|
+
*/
|
|
87
|
+
function simplifyFraction(numerator, denominator) {
|
|
88
|
+
const d = gcd(Math.abs(numerator), Math.abs(denominator));
|
|
89
|
+
return { numerator: numerator / d, denominator: denominator / d };
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ─── NUMBER THEORY ────────────────────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Greatest Common Divisor (Euclidean algorithm).
|
|
96
|
+
* @param {number} a
|
|
97
|
+
* @param {number} b
|
|
98
|
+
* @returns {number}
|
|
99
|
+
*/
|
|
100
|
+
function gcd(a, b) {
|
|
101
|
+
a = Math.abs(Math.round(a));
|
|
102
|
+
b = Math.abs(Math.round(b));
|
|
103
|
+
while (b !== 0) {
|
|
104
|
+
[a, b] = [b, a % b];
|
|
105
|
+
}
|
|
106
|
+
return a;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Least Common Multiple.
|
|
111
|
+
* @param {number} a
|
|
112
|
+
* @param {number} b
|
|
113
|
+
* @returns {number}
|
|
114
|
+
*/
|
|
115
|
+
function lcm(a, b) {
|
|
116
|
+
return Math.abs(a * b) / gcd(a, b);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Factorial of n.
|
|
121
|
+
* @param {number} n - Non-negative integer.
|
|
122
|
+
* @returns {number}
|
|
123
|
+
*/
|
|
124
|
+
function factorial(n) {
|
|
125
|
+
if (n < 0) throw new Error('Factorial is not defined for negative numbers.');
|
|
126
|
+
if (!Number.isInteger(n)) throw new Error('Factorial requires an integer.');
|
|
127
|
+
if (n === 0 || n === 1) return 1;
|
|
128
|
+
let result = 1;
|
|
129
|
+
for (let i = 2; i <= n; i++) result *= i;
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Check whether n is a prime number.
|
|
135
|
+
* @param {number} n
|
|
136
|
+
* @returns {boolean}
|
|
137
|
+
*/
|
|
138
|
+
function isPrime(n) {
|
|
139
|
+
if (n < 2) return false;
|
|
140
|
+
if (n === 2) return true;
|
|
141
|
+
if (n % 2 === 0) return false;
|
|
142
|
+
for (let i = 3; i <= Math.sqrt(n); i += 2) {
|
|
143
|
+
if (n % i === 0) return false;
|
|
144
|
+
}
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Get the prime factors of n.
|
|
150
|
+
* @param {number} n
|
|
151
|
+
* @returns {number[]}
|
|
152
|
+
*/
|
|
153
|
+
function primeFactors(n) {
|
|
154
|
+
const factors = [];
|
|
155
|
+
let d = 2;
|
|
156
|
+
while (n > 1) {
|
|
157
|
+
while (n % d === 0) {
|
|
158
|
+
factors.push(d);
|
|
159
|
+
n /= d;
|
|
160
|
+
}
|
|
161
|
+
d++;
|
|
162
|
+
}
|
|
163
|
+
return factors;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Generate the first n Fibonacci numbers.
|
|
168
|
+
* @param {number} n
|
|
169
|
+
* @returns {number[]}
|
|
170
|
+
*/
|
|
171
|
+
function fibonacci(n) {
|
|
172
|
+
if (n <= 0) return [];
|
|
173
|
+
if (n === 1) return [0];
|
|
174
|
+
const seq = [0, 1];
|
|
175
|
+
for (let i = 2; i < n; i++) seq.push(seq[i - 1] + seq[i - 2]);
|
|
176
|
+
return seq;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ─── ALGEBRA ──────────────────────────────────────────────────────────────────
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Solve a quadratic equation ax² + bx + c = 0.
|
|
183
|
+
* @param {number} a
|
|
184
|
+
* @param {number} b
|
|
185
|
+
* @param {number} c
|
|
186
|
+
* @returns {{ x1: number|null, x2: number|null, discriminant: number }}
|
|
187
|
+
*/
|
|
188
|
+
function quadratic(a, b, c) {
|
|
189
|
+
if (a === 0) throw new Error('Coefficient a cannot be zero for a quadratic equation.');
|
|
190
|
+
const discriminant = b * b - 4 * a * c;
|
|
191
|
+
if (discriminant < 0) {
|
|
192
|
+
return { x1: null, x2: null, discriminant };
|
|
193
|
+
}
|
|
194
|
+
const sqrtD = Math.sqrt(discriminant);
|
|
195
|
+
return {
|
|
196
|
+
x1: (-b + sqrtD) / (2 * a),
|
|
197
|
+
x2: (-b - sqrtD) / (2 * a),
|
|
198
|
+
discriminant,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Natural logarithm, or log to a custom base.
|
|
204
|
+
* @param {number} value
|
|
205
|
+
* @param {number} [base=Math.E]
|
|
206
|
+
* @returns {number}
|
|
207
|
+
*/
|
|
208
|
+
function logarithm(value, base = Math.E) {
|
|
209
|
+
if (value <= 0) throw new Error('Logarithm is only defined for positive values.');
|
|
210
|
+
return Math.log(value) / Math.log(base);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// ─── COMBINATORICS ────────────────────────────────────────────────────────────
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Number of permutations P(n, r).
|
|
217
|
+
* @param {number} n
|
|
218
|
+
* @param {number} r
|
|
219
|
+
* @returns {number}
|
|
220
|
+
*/
|
|
221
|
+
function permutations(n, r) {
|
|
222
|
+
if (r > n) return 0;
|
|
223
|
+
return factorial(n) / factorial(n - r);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Number of combinations C(n, r).
|
|
228
|
+
* @param {number} n
|
|
229
|
+
* @param {number} r
|
|
230
|
+
* @returns {number}
|
|
231
|
+
*/
|
|
232
|
+
function combinations(n, r) {
|
|
233
|
+
if (r > n) return 0;
|
|
234
|
+
return factorial(n) / (factorial(r) * factorial(n - r));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// ─── STATISTICS ───────────────────────────────────────────────────────────────
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Arithmetic mean of an array of numbers.
|
|
241
|
+
* @param {number[]} numbers
|
|
242
|
+
* @returns {number}
|
|
243
|
+
*/
|
|
244
|
+
function mean(numbers) {
|
|
245
|
+
if (numbers.length === 0) throw new Error('Array must not be empty.');
|
|
246
|
+
return numbers.reduce((sum, n) => sum + n, 0) / numbers.length;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Median of an array of numbers.
|
|
251
|
+
* @param {number[]} numbers
|
|
252
|
+
* @returns {number}
|
|
253
|
+
*/
|
|
254
|
+
function median(numbers) {
|
|
255
|
+
if (numbers.length === 0) throw new Error('Array must not be empty.');
|
|
256
|
+
const sorted = [...numbers].sort((a, b) => a - b);
|
|
257
|
+
const mid = Math.floor(sorted.length / 2);
|
|
258
|
+
return sorted.length % 2 !== 0
|
|
259
|
+
? sorted[mid]
|
|
260
|
+
: (sorted[mid - 1] + sorted[mid]) / 2;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Mode(s) of an array of numbers.
|
|
265
|
+
* @param {number[]} numbers
|
|
266
|
+
* @returns {number[]}
|
|
267
|
+
*/
|
|
268
|
+
function mode(numbers) {
|
|
269
|
+
if (numbers.length === 0) throw new Error('Array must not be empty.');
|
|
270
|
+
const freq = {};
|
|
271
|
+
numbers.forEach((n) => { freq[n] = (freq[n] || 0) + 1; });
|
|
272
|
+
const maxFreq = Math.max(...Object.values(freq));
|
|
273
|
+
return Object.keys(freq)
|
|
274
|
+
.filter((k) => freq[k] === maxFreq)
|
|
275
|
+
.map(Number);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Population and sample variance.
|
|
280
|
+
* @param {number[]} numbers
|
|
281
|
+
* @returns {{ population: number, sample: number }}
|
|
282
|
+
*/
|
|
283
|
+
function variance(numbers) {
|
|
284
|
+
if (numbers.length < 2) throw new Error('Array must have at least 2 elements.');
|
|
285
|
+
const m = mean(numbers);
|
|
286
|
+
const squaredDiffs = numbers.map((n) => (n - m) ** 2);
|
|
287
|
+
const sumSq = squaredDiffs.reduce((s, v) => s + v, 0);
|
|
288
|
+
return {
|
|
289
|
+
population: sumSq / numbers.length,
|
|
290
|
+
sample: sumSq / (numbers.length - 1),
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Population and sample standard deviation.
|
|
296
|
+
* @param {number[]} numbers
|
|
297
|
+
* @returns {{ population: number, sample: number }}
|
|
298
|
+
*/
|
|
299
|
+
function standardDeviation(numbers) {
|
|
300
|
+
const v = variance(numbers);
|
|
301
|
+
return {
|
|
302
|
+
population: Math.sqrt(v.population),
|
|
303
|
+
sample: Math.sqrt(v.sample),
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Z-score of a value given mean and standard deviation.
|
|
309
|
+
* @param {number} value
|
|
310
|
+
* @param {number} meanVal
|
|
311
|
+
* @param {number} stdDev
|
|
312
|
+
* @returns {number}
|
|
313
|
+
*/
|
|
314
|
+
function zScore(value, meanVal, stdDev) {
|
|
315
|
+
if (stdDev === 0) throw new Error('Standard deviation cannot be zero.');
|
|
316
|
+
return (value - meanVal) / stdDev;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/**
|
|
320
|
+
* Pearson correlation coefficient between two arrays.
|
|
321
|
+
* @param {number[]} xArray
|
|
322
|
+
* @param {number[]} yArray
|
|
323
|
+
* @returns {number}
|
|
324
|
+
*/
|
|
325
|
+
function correlation(xArray, yArray) {
|
|
326
|
+
if (xArray.length !== yArray.length) throw new Error('Arrays must have equal length.');
|
|
327
|
+
if (xArray.length < 2) throw new Error('Arrays must have at least 2 elements.');
|
|
328
|
+
const mx = mean(xArray);
|
|
329
|
+
const my = mean(yArray);
|
|
330
|
+
const num = xArray.reduce((s, x, i) => s + (x - mx) * (yArray[i] - my), 0);
|
|
331
|
+
const denX = Math.sqrt(xArray.reduce((s, x) => s + (x - mx) ** 2, 0));
|
|
332
|
+
const denY = Math.sqrt(yArray.reduce((s, y) => s + (y - my) ** 2, 0));
|
|
333
|
+
if (denX === 0 || denY === 0) throw new Error('Standard deviation of inputs cannot be zero.');
|
|
334
|
+
return num / (denX * denY);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// ─── GEOMETRY ─────────────────────────────────────────────────────────────────
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Area of a circle.
|
|
341
|
+
* @param {number} radius
|
|
342
|
+
* @returns {number}
|
|
343
|
+
*/
|
|
344
|
+
function circleArea(radius) {
|
|
345
|
+
return Math.PI * radius * radius;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* Circumference of a circle.
|
|
350
|
+
* @param {number} radius
|
|
351
|
+
* @returns {number}
|
|
352
|
+
*/
|
|
353
|
+
function circleCircumference(radius) {
|
|
354
|
+
return 2 * Math.PI * radius;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Area of a rectangle.
|
|
359
|
+
* @param {number} length
|
|
360
|
+
* @param {number} width
|
|
361
|
+
* @returns {number}
|
|
362
|
+
*/
|
|
363
|
+
function rectangleArea(length, width) {
|
|
364
|
+
return length * width;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Area of a triangle (base × height / 2).
|
|
369
|
+
* @param {number} base
|
|
370
|
+
* @param {number} height
|
|
371
|
+
* @returns {number}
|
|
372
|
+
*/
|
|
373
|
+
function triangleArea(base, height) {
|
|
374
|
+
return (base * height) / 2;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Area of a triangle using Heron's formula.
|
|
379
|
+
* @param {number} a - Side a.
|
|
380
|
+
* @param {number} b - Side b.
|
|
381
|
+
* @param {number} c - Side c.
|
|
382
|
+
* @returns {number}
|
|
383
|
+
*/
|
|
384
|
+
function triangleAreaHeron(a, b, c) {
|
|
385
|
+
const s = (a + b + c) / 2;
|
|
386
|
+
const area = Math.sqrt(s * (s - a) * (s - b) * (s - c));
|
|
387
|
+
if (isNaN(area)) throw new Error('The given sides do not form a valid triangle.');
|
|
388
|
+
return area;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Volume of a sphere.
|
|
393
|
+
* @param {number} radius
|
|
394
|
+
* @returns {number}
|
|
395
|
+
*/
|
|
396
|
+
function sphereVolume(radius) {
|
|
397
|
+
return (4 / 3) * Math.PI * radius ** 3;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Volume of a cylinder.
|
|
402
|
+
* @param {number} radius
|
|
403
|
+
* @param {number} height
|
|
404
|
+
* @returns {number}
|
|
405
|
+
*/
|
|
406
|
+
function cylinderVolume(radius, height) {
|
|
407
|
+
return Math.PI * radius * radius * height;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Volume of a cone.
|
|
412
|
+
* @param {number} radius
|
|
413
|
+
* @param {number} height
|
|
414
|
+
* @returns {number}
|
|
415
|
+
*/
|
|
416
|
+
function coneVolume(radius, height) {
|
|
417
|
+
return (1 / 3) * Math.PI * radius * radius * height;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Hypotenuse of a right triangle (Pythagorean theorem).
|
|
422
|
+
* @param {number} a
|
|
423
|
+
* @param {number} b
|
|
424
|
+
* @returns {number}
|
|
425
|
+
*/
|
|
426
|
+
function pythagorean(a, b) {
|
|
427
|
+
return Math.sqrt(a * a + b * b);
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// ─── TRIGONOMETRY ─────────────────────────────────────────────────────────────
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* Convert degrees to radians.
|
|
434
|
+
* @param {number} degrees
|
|
435
|
+
* @returns {number}
|
|
436
|
+
*/
|
|
437
|
+
function degreesToRadians(degrees) {
|
|
438
|
+
return (degrees * Math.PI) / 180;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Convert radians to degrees.
|
|
443
|
+
* @param {number} radians
|
|
444
|
+
* @returns {number}
|
|
445
|
+
*/
|
|
446
|
+
function radiansToDegrees(radians) {
|
|
447
|
+
return (radians * 180) / Math.PI;
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/**
|
|
451
|
+
* Sine of an angle in degrees.
|
|
452
|
+
* @param {number} degrees
|
|
453
|
+
* @returns {number}
|
|
454
|
+
*/
|
|
455
|
+
function sinDeg(degrees) {
|
|
456
|
+
return Math.sin(degreesToRadians(degrees));
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
/**
|
|
460
|
+
* Cosine of an angle in degrees.
|
|
461
|
+
* @param {number} degrees
|
|
462
|
+
* @returns {number}
|
|
463
|
+
*/
|
|
464
|
+
function cosDeg(degrees) {
|
|
465
|
+
return Math.cos(degreesToRadians(degrees));
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Tangent of an angle in degrees.
|
|
470
|
+
* @param {number} degrees
|
|
471
|
+
* @returns {number}
|
|
472
|
+
*/
|
|
473
|
+
function tanDeg(degrees) {
|
|
474
|
+
return Math.tan(degreesToRadians(degrees));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
// ─── EXPORTS ──────────────────────────────────────────────────────────────────
|
|
478
|
+
|
|
479
|
+
module.exports = {
|
|
480
|
+
// Percentage
|
|
481
|
+
percentage,
|
|
482
|
+
percentageChange,
|
|
483
|
+
percentageOf,
|
|
484
|
+
// Fractions
|
|
485
|
+
fractionToDecimal,
|
|
486
|
+
decimalToFraction,
|
|
487
|
+
simplifyFraction,
|
|
488
|
+
// Number theory
|
|
489
|
+
gcd,
|
|
490
|
+
lcm,
|
|
491
|
+
factorial,
|
|
492
|
+
isPrime,
|
|
493
|
+
primeFactors,
|
|
494
|
+
fibonacci,
|
|
495
|
+
// Algebra
|
|
496
|
+
quadratic,
|
|
497
|
+
logarithm,
|
|
498
|
+
// Combinatorics
|
|
499
|
+
permutations,
|
|
500
|
+
combinations,
|
|
501
|
+
// Statistics
|
|
502
|
+
mean,
|
|
503
|
+
median,
|
|
504
|
+
mode,
|
|
505
|
+
variance,
|
|
506
|
+
standardDeviation,
|
|
507
|
+
zScore,
|
|
508
|
+
correlation,
|
|
509
|
+
// Geometry
|
|
510
|
+
circleArea,
|
|
511
|
+
circleCircumference,
|
|
512
|
+
rectangleArea,
|
|
513
|
+
triangleArea,
|
|
514
|
+
triangleAreaHeron,
|
|
515
|
+
sphereVolume,
|
|
516
|
+
cylinderVolume,
|
|
517
|
+
coneVolume,
|
|
518
|
+
pythagorean,
|
|
519
|
+
// Trigonometry
|
|
520
|
+
degreesToRadians,
|
|
521
|
+
radiansToDegrees,
|
|
522
|
+
sinDeg,
|
|
523
|
+
cosDeg,
|
|
524
|
+
tanDeg,
|
|
525
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "calcufly-math",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Math and statistics calculator functions - percentages, fractions, geometry, algebra, statistics, trigonometry. By CalcuFly.com",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"keywords": ["math", "calculator", "statistics", "geometry", "algebra", "percentage", "fraction", "trigonometry", "area", "volume", "mean", "median", "standard-deviation", "quadratic", "gcd", "lcm"],
|
|
7
|
+
"author": "CalcuFly <contact@calcufly.com>",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"homepage": "https://calcufly.com",
|
|
10
|
+
"repository": { "type": "git", "url": "https://github.com/mansouewalks-bit2/calcufly-math" },
|
|
11
|
+
"bugs": { "url": "https://github.com/mansouewalks-bit2/calcufly-math/issues" }
|
|
12
|
+
}
|