feat: add root operation to RPN calculator

This commit is contained in:
2026-05-15 22:58:21 +02:00
parent 4e8155b5f0
commit cb45efff43
2 changed files with 16 additions and 2 deletions
+15 -1
View File
@@ -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'],
};
}