🦀 Rust WASM Calculator

Memory: 0
0

History (0)

Powered by Rust 🦀 + WebAssembly
GitHub

Rust WASM Calculator: what it is and what it computes

The Rust WASM Calculator is a free browser calculator whose arithmetic engine is written in Rust and compiled to WebAssembly. It computes addition, subtraction, multiplication, division, square root, squaring, and factorial, and it keeps a running memory register and a list of recent calculations. Every number is held as a 64-bit float. Factorial is computed in 64-bit unsigned integer arithmetic inside Rust and crosses back to JavaScript as a float, which is exact for every value it accepts. Nothing you type is sent anywhere: the WebAssembly module is downloaded once and every calculation runs locally inside the page.

What this calculator computes

The on-screen keypad exposes 24 buttons. Four of them drive the memory register, four drive arithmetic operators, one clears the display, ten enter digits, one enters a decimal point, and the rest cover square root, squaring, equals, and factorial. Pressing an operator stores the current display value in the Rust Calculator struct; pressing equals sends the second operand across the WebAssembly boundary and writes the result back to the display.

Two further functions ship inside the WebAssembly module and are importable from JavaScript but have no keypad button: percentage(value, percent) and compound_interest(principal, rate, years, compounds_per_year). They are documented on the percentage and compound interest pages.

Why Rust and WebAssembly

The arithmetic lives in Rust rather than JavaScript so that the failure cases are part of the type system instead of an afterthought. Division and square root return a Rust Result, and factorial uses checked_mul so an overflow becomes an error value rather than a silently wrapped number. Those Result types cross into JavaScript as thrown exceptions, which means the page cannot display a wrong answer where it should have displayed an error.

WebAssembly is what lets that Rust code run in a browser tab with no server round trip. The crate is built with wasm-pack build --target web and compiled with opt-level = "z" and link-time optimization enabled, which keeps the shipped module small enough to fetch on first paint. The full reasoning is on the why Rust and WebAssembly page.

Reference pages

Each operation has its own page with the exact behaviour, the edge cases, and the error text you will see if an input is out of range.

There are also pages on how the project is put together, for anyone reading the source rather than using the calculator.

Questions and answers

Is the Rust WASM Calculator free to use?

Yes. The calculator is free, requires no account, and its full source code is published under the pigfox/rust-wasm-calc repository on GitHub.

Does this calculator send my calculations to a server?

No. The WebAssembly module is downloaded once and every calculation runs inside your browser tab. There is no calculation API and no request is made when you press a button.

What operations does the calculator support?

Addition, subtraction, multiplication, division, square root, squaring, and factorial, plus a four-function memory register and a list of the five most recent calculations. The WebAssembly module additionally exports percentage and compound interest functions that have no keypad button.

Why is the arithmetic written in Rust instead of JavaScript?

So that error cases are enforced by the compiler. Division and square root return a Rust Result type, and factorial uses checked multiplication, so overflow and domain errors surface as explicit errors rather than as NaN or a silently wrapped number.