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).

AlgorithmDijkstra's Shunting-yard Algorithm
TrigonometryRadians & Degrees Dual Modes
PrecisionAccurate to 64 significant decimal digits
Zero Dependencies100% Vanilla JS & CSS
# 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
+ , -1LeftBinary Addition / Subtraction
* , / , %2LeftMultiplication / Division / Modulo
^ (Power)3RightExponentiation (Right-associative)
sin, cos, tan, sqrt4RightUnary 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;
}
Epsilon SmoothingCorrects boundary binary rounding drift
Max Digit BufferUp to 16 decimal places of precision
Zero Division TrapCatches n / 0 and returns localized Undefined
Domain ProtectionProtects sqrt(-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
MCMemory ClearMemory_Register = 0
MRMemory RecallInjects current Memory_Register value into active expression
M+Memory AddMemory_Register = Memory_Register + Current_Result
M-Memory SubtractMemory_Register = Memory_Register - Current_Result

Supported Functions & Constants

Symbol / Function Description Example Expression
sin(x), cos(x), tan(x)Trigonometric functionssin(π / 4)
asin(x), acos(x), atan(x)Inverse trigonometric functionsatan(1)
sqrt(x) / cbrt(x)Square / Cube rootssqrt(144) = 12
log(x) / ln(x)Base-10 and Natural Logarithmsln(e) = 1
π (Pi)Archimedes' constant ~3.141592652 * π * r
e (Euler)Euler's number ~2.71828182e ^ 2

Keyboard Map Reference

Keyboard Key Calculator Function
0-9Digit Input
+ - * /Basic Arithmetic Operations
Enter / =Compute & Save to History
BackspaceDelete Last Character
EscapeClear All (AC)
s / c / tQuick-type sin / cos / tan