Building an Expression Calculator in Rust — Pratt Parsing Turns Precedence into Two Numbers, and Rationals Kill Float Error (opens in new tab)
A terminal calculator where 1 + 2 * 3 is 7, 2^3^2 is 512 (= 2^9), and 0.1 + 0.2 is exactly 0.3 — not 0.30000000000000004. I wrote pcalc in Rust. Two implementation hinges: (1) a Pratt parser that expresses operator precedence as two "binding power" numbers per operator instead of a tower of grammar rules, and (2) exact rational arithmetic — every value is a reduced fraction — so floating-point rounding error simply doesn't exist. 📦 GitHub: (It's a CLI — no live demo. Run with cargo run or Doc...
Read the original article