feat(math-engine): implement ScientificFunctions object with all whitelisted mathematical functions
This commit is contained in:
@@ -0,0 +1,81 @@
|
|||||||
|
/**
|
||||||
|
* Scientific functions implementation for the calculator
|
||||||
|
* Provides implementations for all whitelisted mathematical functions
|
||||||
|
*/
|
||||||
|
|
||||||
|
const ScientificFunctions = {
|
||||||
|
// Trigonometric functions (expect radians)
|
||||||
|
sin: Math.sin,
|
||||||
|
cos: Math.cos,
|
||||||
|
tan: Math.tan,
|
||||||
|
asin: Math.asin,
|
||||||
|
acos: Math.acos,
|
||||||
|
atan: Math.atan,
|
||||||
|
|
||||||
|
// Hyperbolic functions
|
||||||
|
sinh: (x) => (Math.exp(x) - Math.exp(-x)) / 2,
|
||||||
|
cosh: (x) => (Math.exp(x) + Math.exp(-x)) / 2,
|
||||||
|
tanh: (x) => (Math.exp(x) - Math.exp(-x)) / (Math.exp(x) + Math.exp(-x)),
|
||||||
|
asinh: (x) => Math.log(x + Math.sqrt(x * x + 1)),
|
||||||
|
acosh: (x) => Math.log(x + Math.sqrt(x * x - 1)),
|
||||||
|
atanh: (x) => 0.5 * Math.log((1 + x) / (1 - x)),
|
||||||
|
|
||||||
|
// Exponential and logarithmic
|
||||||
|
exp: Math.exp,
|
||||||
|
log: (x) => Math.log10(x), // base 10
|
||||||
|
ln: Math.log, // natural log
|
||||||
|
|
||||||
|
// Root and power
|
||||||
|
sqrt: Math.sqrt,
|
||||||
|
cbrt: Math.cbrt,
|
||||||
|
// Exponentiation handled via ** operator in expression parser
|
||||||
|
|
||||||
|
// Absolute value and rounding
|
||||||
|
abs: Math.abs,
|
||||||
|
ceil: Math.ceil,
|
||||||
|
floor: Math.floor,
|
||||||
|
round: Math.round,
|
||||||
|
|
||||||
|
// Factorial
|
||||||
|
factorial: (n) => {
|
||||||
|
if (n < 0) return NaN;
|
||||||
|
if (!Number.isInteger(n)) return NaN;
|
||||||
|
if (n === 0 || n === 1) return 1;
|
||||||
|
let result = 1;
|
||||||
|
for (let i = 2; i <= n; i++) {
|
||||||
|
result *= i;
|
||||||
|
if (!Number.isFinite(result)) return Infinity;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Combinations and permutations
|
||||||
|
nCr: (n, r) => {
|
||||||
|
if (n < 0 || r < 0 || !Number.isInteger(n) || !Number.isInteger(r) || r > n) return NaN;
|
||||||
|
if (r === 0 || r === n) return 1;
|
||||||
|
// Use symmetry to reduce computation
|
||||||
|
r = Math.min(r, n - r);
|
||||||
|
let result = 1;
|
||||||
|
for (let i = 1; i <= r; i++) {
|
||||||
|
result = result * (n - r + i) / i;
|
||||||
|
if (!Number.isFinite(result)) return Infinity;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
nPr: (n, r) => {
|
||||||
|
if (n < 0 || r < 0 || !Number.isInteger(n) || !Number.isInteger(r) || r > n) return NaN;
|
||||||
|
if (r === 0) return 1;
|
||||||
|
let result = 1;
|
||||||
|
for (let i = 0; i < r; i++) {
|
||||||
|
result *= (n - i);
|
||||||
|
if (!Number.isFinite(result)) return Infinity;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Random number
|
||||||
|
random: Math.random
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = ScientificFunctions;
|
||||||
Reference in New Issue
Block a user