e23373fee0c601cf5f8e73c879ffa5c7a7877331
RPN Virtual Calculator
A browser-friendly RPN calculator implemented as a single JavaScript class, with a simple API and an example HTML interface.
Overview
The project includes:
rpn-calculator.js: calculator enginerpn-example.html: browser demo
Highlights
- Self-contained JavaScript class
- Configurable stack size (
maxSize, default: 2048) - Configurable numeric base (
base, default: 10) - Configurable angle mode (
angleMode, default:deg) - Optional command enabling via
enabledCommands - Generic public API centered on
push,pop,clear,swap,remove,edit,isValidIndex,input, andcommand inputValuestays a string to keep hexadecimal input possible laterisEditingtracks typed input mode- Operations are centralized with
argCount, category, and aliases - Categories are limited to
Stack,Arithmetic, andTrigonometry - Clear domain errors for invalid inputs
- Degree-based trig demo in the example HTML
Quick start
In the browser
<script src="rpn-calculator.js"></script>
<script>
const calc = new RpnCalculator();
calc.input('1');
calc.input('2');
calc.command('enter');
calc.command('add');
console.log(calc.stack);
</script>
With options
const calc = new RpnCalculator({
maxSize: 1024,
base: 10,
angleMode: 'deg',
enabledCommands: ['add', 'sub', 'mul', 'div', 'enter', 'clear'],
});
Public API
Properties
stack: current stack, with the top item at index0inputValue: input text as a stringisEditing: whether input is currently being edited
Generic methods
push(value)pop()clear()swap(index1, index2)remove(index)edit(index)isValidIndex(index)input(command)command(name, ...args)
Supported commands
Stack
enterdupdropswapclear
Arithmetic
add/+sub/-mul/*div//mod/%pow/^/y^xsqrnegsqrtrecip/1/xlogln
Trigonometry
sincostanasinacosatan
Important behavior
%behaves as the RPN percentage operator:a b % => (a * b) / 100sqrt,asin,acos,log, andlnthrow clear errors for invalid inputslogusesMath.log10lnusesMath.logsin,cos, andtanconvert degrees to radians in the default demoasin,acos, andatanreturn degrees indegmode
Example HTML
The example UI groups buttons into Stack, Arithmetic, and Trigonometry sections, and calls command(...) to execute operations.
License
To be completed according to your project.
Description
Languages
JavaScript
82.6%
Shell
17.4%