feat: add root operation to RPN calculator
This commit is contained in:
+15
-1
@@ -46,6 +46,20 @@ class RpnCalculator {
|
||||
aliases: ['^', 'y^x'],
|
||||
execute: (calc, a, b) => Math.pow(a, b),
|
||||
},
|
||||
root: {
|
||||
category: 'Arithmetic',
|
||||
argCount: 2,
|
||||
aliases: ['y√x', 'yroot', 'nroot'],
|
||||
execute: (calc, a, b) => {
|
||||
if (b === 0) {
|
||||
throw new Error('Invalid input for root');
|
||||
}
|
||||
if (a < 0 && b % 2 === 0) {
|
||||
throw new Error('Invalid input for root');
|
||||
}
|
||||
return Math.pow(a, 1 / b);
|
||||
},
|
||||
},
|
||||
sqr: {
|
||||
category: 'Arithmetic',
|
||||
argCount: 1,
|
||||
@@ -178,7 +192,7 @@ class RpnCalculator {
|
||||
static getOperationsByCategory() {
|
||||
return {
|
||||
Stack: ['dup', 'drop', 'swap', 'clear', 'enter'],
|
||||
Arithmetic: ['add', 'sub', 'mul', 'div', 'mod', 'pow', 'sqr', 'neg', 'sqrt', 'recip', 'log', 'ln'],
|
||||
Arithmetic: ['add', 'sub', 'mul', 'div', 'mod', 'pow', 'root', 'sqr', 'neg', 'sqrt', 'recip', 'log', 'ln'],
|
||||
Trigonometry: ['sin', 'cos', 'tan', 'asin', 'acos', 'atan'],
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user