Labsco
carlomagnoglobal logo

toRustCalcMCP

from carlomagnoglobal

A Rust port of calc (Landon Curt Noll's arbitrary-precision calculator)

πŸ”₯πŸ”₯πŸ”₯βœ“ VerifiedFreeQuick setup

toRustCalcMCP

A Rust port of calc (Landon Curt Noll's arbitrary-precision calculator) that works as:

  • rcalc β€” a calc-compatible command-line calculator
  • Web REPL β€” browser-based interactive calculator
  • MCP server β€” JSON-RPC 2.0 over stdio for LLM/agent integration

Three interfaces, one engine. The numeric core uses exact rational arithmetic (num-rational over num-bigint), which is the same model calc uses natively β€” so 1/3 * 3 is exactly 1, and 2^256 is computed to the last digit.

πŸ‘‰ Getting Started in 2 minutes β€” new to rcalc? Start here. πŸ‘‰ Install as Claude Desktop MCP β€” integrate with Claude Desktop.

Build

cargo build --release
# binaries:
#   target/release/toRustCalcMCP   (auto-detects MCP vs CLI)
#   target/release/rcalc           (always the CLI)

Optionally symlink: ln -s toRustCalcMCP rcalc β€” when argv[0] is rcalc, toRustCalcMCP behaves as the calculator.

Language supported

  • Operators: + - * / (exact), // (integer divide), % (modulus), ^/** (power), comparisons == != < <= > >= (yield 1/0), unary -/+.
  • Variables and assignment: x = 7; x^2.
  • User-defined functions: define f(x) = x^2; f(5) β†’ 25.
  • Control flow: if/else, while, for loops, blocks with {}.
  • ;-separated statements; the value of each is printed (calc behaviour).
  • Numeric literals: integers, a/b rationals, decimals, 1.2e-3, 0x/0b.
  • Lists: list(1,2,3); append(x,4); slice(x,1,3).
  • Complex numbers: sqrt(-1) β†’ i; arithmetic with +, -, *, /.
  • String literals: "hello"; strlen(s); index(haystack, needle).
  • 483 builtin names (full parity with calc's 350, minus 15 documented interpreter internals, plus extensions) β€” see implementation status below.

Precision model

Numbers are exact rationals. Irrational results are approximated to within the session epsilon (default exact 1/10^20), exactly like calc. Transcendentals (exp, ln, sin, cos, tan) are computed at arbitrary precision via Taylor series and Newton's method. sqrt, sin, cos, etc. converge until term < epsilon. pi/e are 60-digit constants. A leading ~ in real-mode output marks an inexact (rounded/non-terminating) rendering, as in calc.

Implementation Status β€” full upstream parity βœ…

calc upstream (lcn2/calc func.c) defines 350 builtins. This port registers 483 builtin names covering 335 of the 350 upstream builtins (the other 148 that were long missing were added in the upstream-parity batches below), plus dozens of extensions and aliases beyond upstream (statistics, hashing, bit tricks, system info, and more).

Intentionally not implemented (15 interpreter internals)

These upstream names are artifacts of calc's C interpreter and have no meaningful mapping to this port's architecture; they remain unimplemented rather than shipping fake stubs:

access, calc_tty, calclevel, calcpath, custom, dp, estr, inputlevel, memsize, name, param, prompt, protect, saveval, stoponerror

Known deviations from upstream

  • Out-parameter builtins return values instead: d2dm/d2dms (and the g/h family) return [deg, min] / [deg, min, sec] lists; quomod returns [q, r]; search/rsearch return an index or null.
  • In-place/lvalue builtins return new values: modify, copy, swap (builtins receive values, not references).
  • base2() reads as 0 (no secondary base); setting it errors β€” the renderer has a single output base.
  • free*() are no-ops: nothing is cached, values are computed on demand.

Full language features

  • βœ… User-defined functions (define name(params) = expr), higher-order builtins (select/forall/modify call function values)
  • βœ… Control flow (if/else, while, for loops)
  • βœ… Variables and scoping
  • βœ… Lists and indexing (0-based, negative indices supported)
  • βœ… Complex numbers with full arithmetic
  • βœ… String literals and a complete string-function suite
  • βœ… Base conversion (2-36, input and output)
  • βœ… Arbitrary-precision arithmetic (exact rationals; exact 1/10^20 epsilon)
  • βœ… File loading (-f filename), file I/O builtins, REPL, pipe mode

Scope β€” Architecture & Design

calc upstream is ~92,000 lines of C with ~350 builtins and a full, Turing-complete scripting language. This port is a faithful core, structured for incremental, additive expansion:

  • βœ… Exact-rational numeric engine β€” matches calc's native model
  • βœ… Complete lexer/parser β€” handles the full expression syntax
  • βœ… Tree-walk evaluator β€” with user-defined functions, control flow, scoping
  • βœ… Builtin registry β€” extensible function map with auto-cataloging
  • βœ… CLI & MCP server β€” two front-ends, one engine
  • πŸ”„ Incremental builtins β€” each category slots in cleanly without rework

The architecture is stable; adding more functions is straightforward.

License

LGPL-2.1, matching calc upstream.

Test CI workflow