Official Technical Documentation
CalcCosmos Calculator Documentation
Technical documentation covering Dijkstra Shunting-yard expression parsing, floating point error mitigation, and memory registers.
CalcCosmos Architecture Overview
CalcCosmos is a zero-dependency scientific calculation engine built with modern JavaScript. It replaces naive expression evaluation with a robust mathematical tokenizer and Shunting-yard algorithm to safely produce an Abstract Syntax Tree (AST).
# Clone the repository
git clone https://github.com/dev-hints/CalcCosmos.git
cd CalcCosmos
# Run locally in any web browser
open index.html
Shunting-Yard Expression Parser
Infix mathematical expressions (e.g. sin(π / 6) + 4 * 2 / (1 - 5)^2) are converted into Reverse Polish Notation (RPN) using Dijkstra's operator precedence algorithm before linear stack evaluation:
// 1. Tokenizer pass
Infix: "3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3"
Tokens: [NUM(3), OP(+), NUM(4), OP(*), NUM(2), OP(/), LPAREN, NUM(1), OP(-), NUM(5), RPAREN, OP(^), NUM(2), OP(^), NUM(3)]
// 2. Shunting-Yard output queue (RPN)
RPN: "3 4 2 * 1 5 - 2 3 ^ ^ / +"
| Operator | Precedence | Associativity | Semantics |
|---|---|---|---|
+ , - | 1 | Left | Binary Addition / Subtraction |
* , / , % | 2 | Left | Multiplication / Division / Modulo |
^ (Power) | 3 | Right | Exponentiation (Right-associative) |
sin, cos, tan, sqrt | 4 | Right | Unary Function Evaluation |
IEEE-754 Precision Mitigation Engine
Standard JavaScript binary 64-bit floating point arithmetic suffers from binary fraction conversion errors (e.g. 0.1 + 0.2 = 0.30000000000000004). CalcCosmos eliminates this through dynamic scaling and epsilon-rounded fixed precision formatting:
// Precision serialization formula
function safeRound(value, precision = 12) {
const factor = Math.pow(10, precision);
return Math.round((value + Number.EPSILON) * factor) / factor;
}
n / 0 and returns localized Undefinedsqrt(-x) and ln(0)Memory Registers (M+, M-, MR, MC)
CalcCosmos provides hardware-equivalent accumulator memory registers for multi-step scientific workflows:
| Register | Operation | Formula |
|---|---|---|
MC | Memory Clear | Memory_Register = 0 |
MR | Memory Recall | Injects current Memory_Register value into active expression |
M+ | Memory Add | Memory_Register = Memory_Register + Current_Result |
M- | Memory Subtract | Memory_Register = Memory_Register - Current_Result |
Supported Functions & Constants
| Symbol / Function | Description | Example Expression |
|---|---|---|
sin(x), cos(x), tan(x) | Trigonometric functions | sin(π / 4) |
asin(x), acos(x), atan(x) | Inverse trigonometric functions | atan(1) |
sqrt(x) / cbrt(x) | Square / Cube roots | sqrt(144) = 12 |
log(x) / ln(x) | Base-10 and Natural Logarithms | ln(e) = 1 |
π (Pi) | Archimedes' constant ~3.14159265 | 2 * π * r |
e (Euler) | Euler's number ~2.71828182 | e ^ 2 |
Keyboard Map Reference
| Keyboard Key | Calculator Function |
|---|---|
0-9 | Digit Input |
+ - * / | Basic Arithmetic Operations |
Enter / = | Compute & Save to History |
Backspace | Delete Last Character |
Escape | Clear All (AC) |
s / c / t | Quick-type sin / cos / tan |