147 lines
3.4 KiB
Markdown
147 lines
3.4 KiB
Markdown
# RPN Virtual Calculator
|
|
|
|
A browser-friendly RPN calculator built around a small, generic JavaScript API.
|
|
|
|
## Goal
|
|
|
|
This project defines a reusable RPN calculator engine with:
|
|
|
|
- a simple stack-based public API
|
|
- configurable numeric and UI behavior
|
|
- centralized command metadata
|
|
- a browser demo that uses the same public API as any consumer code
|
|
|
|
# RPN Virtual Calculator
|
|
|
|
A browser-friendly RPN calculator built around a small, generic JavaScript API.
|
|
|
|
## Goal
|
|
|
|
This project provides a reusable Reverse Polish Notation (RPN) calculator engine with:
|
|
|
|
- a simple stack-based public API
|
|
- configurable numeric behavior
|
|
- centralized command metadata
|
|
- browser demos that use the same public API as any consumer code
|
|
|
|
## Project structure
|
|
|
|
- `src/rpn-calculator.js`: calculator engine
|
|
- `samples/dev/index.html`: browser demo
|
|
- `samples/calc-01/index.html`: browser demo
|
|
- `samples/calc-XX/index.html`: browser demo
|
|
|
|
## Main features
|
|
|
|
- Single JavaScript class: `RpnCalculator`
|
|
- Configurable stack size via `maxSize` (default: `2048`)
|
|
- Configurable numeric base via `base` (default: `10`)
|
|
- Configurable angle mode via `angleMode`:
|
|
- `deg` (default)
|
|
- `rad`
|
|
- `grad`
|
|
- Optional command filtering through `enabledCommands`
|
|
- Public API limited to generic methods:
|
|
- `push(value)`
|
|
- `pop()`
|
|
- `clear()`
|
|
- `swap(index1, index2)`
|
|
- `remove(index)`
|
|
- `edit(index)`
|
|
- `isValidIndex(index)`
|
|
- `input(command)`
|
|
- `command(name, ...args)`
|
|
- `inputValue` is always stored as a string
|
|
- `isEditing` is exposed as a boolean
|
|
- Supported operations are centralized in one dictionary with metadata such as:
|
|
- `argCount`
|
|
- `category`
|
|
- `aliases`
|
|
- Supported categories are limited to:
|
|
- `Stack`
|
|
- `Arithmetic`
|
|
- `Trigonometry`
|
|
## Available constants
|
|
|
|
- `pi`
|
|
- `e`
|
|
|
|
## Supported commands
|
|
|
|
### Arithmetic
|
|
- `add` (`+`)
|
|
- `sub` (`-`)
|
|
- `mul` (`*`)
|
|
- `div` (`/`)
|
|
- `mod` (`%`)
|
|
- `pow` (`^`, `y^x`)
|
|
- `sqr`
|
|
- `neg`
|
|
- `sqrt`
|
|
- `recip` (`1/x`)
|
|
- `log`
|
|
- `ln`
|
|
|
|
### Trigonometry
|
|
- `sin`
|
|
- `cos`
|
|
- `tan`
|
|
- `asin`
|
|
- `acos`
|
|
- `atan`
|
|
|
|
### Stack
|
|
- `dup`
|
|
- `drop`
|
|
- `swap`
|
|
- `clear`
|
|
- `enter`
|
|
|
|
## Behavior rules
|
|
|
|
- `mod` is a percentage operator:
|
|
- `a b % => (a * b) / 100`
|
|
- `log` uses `Math.log10`
|
|
- `ln` uses `Math.log`
|
|
- `sqrt`, `asin`, `acos`, `log`, and `ln` throw explicit domain errors on invalid input
|
|
- Trigonometric behavior depends on `angleMode`
|
|
- In degree mode:
|
|
- `sin`, `cos`, `tan` convert degrees to radians internally
|
|
- `asin`, `acos`, `atan` return degrees
|
|
- `inputValue` remains a string to preserve future support for formats such as hexadecimal input
|
|
|
|
## Basic usage
|
|
|
|
### In a browser
|
|
- `rpn-calculator.js`: calculator engine
|
|
- `rpn-example.html`: example browser interface
|
|
|
|
## Main features
|
|
- Single JavaScript class
|
|
- Configurable stack size (`maxSize`, default: `2048`)
|
|
- Configurable numeric base (`base`, default: `10`)
|
|
- Configurable angle mode (`angleMode`, default: `deg`)
|
|
- Optional command filtering through `enabledCommands`
|
|
- Generic public API centered on:
|
|
- `push`
|
|
- `pop`
|
|
- `clear`
|
|
- `swap(index1, index2)`
|
|
- `remove(index)`
|
|
- `edit(index)`
|
|
- `isValidIndex(index)`
|
|
- `input(command)`
|
|
- `command(name, ...args)`
|
|
- `inputValue` is kept as a string to preserve future input formats
|
|
- `isEditing` is exposed as a boolean state
|
|
- All supported commands are described in one centralized dictionary
|
|
- Supported categories are limited to:
|
|
- `Stack`
|
|
- `Arithmetic`
|
|
- `Trigonometry`
|
|
|
|
## Basic usage
|
|
|
|
### In a browser
|
|
|