I wrote a calculator that can do decimal and hexadecimal arithmetic conveniently.

https://calc.stuffedcow.net/ (New window)

As a computer engineer, I often do calculations with a mix of decimal and hexadecimal numbers. But I haven’t been able to find a calculator that can work in both bases conveniently. Many calculators don’t support hexadecimal at all (e.g., HP 30b). Some calculators do support it, but it’s buried in layers of menus with no easy way to enter A through F, so it’s much slower than computing in decimal (e.g., HP 42S).

Some software calculators in Programmer mode (e.g., Windows 10, Gnome calculator, KCalc) are better, but have some weird constraints. For example, both Windows calculator and KCalc only allow integers in Programmer mode, and Gnome calculator does not allow switching bases within the same calculation. And, oddly, all three calculators’ programmer modes are missing the typical scientific calculator functions. Programmers don’t science?

So I wrote yet another calculator.

The primary goal is to be able to switch between hexadecimal and decimal quickly while using keyboard (numeric keypad) input. There’s also a variant for touch devices that has more buttons on the screen and relies less on Shift and Ctrl modifier keys.

The calculator has both RPN and infix modes. I prefer RPN, but I suspect most people are more familiar with infix. Not surprisingly, writing the infix mode is many times harder than RPN. RPN mode simply performs each operation on the first two numbers on the stack immediately when an operation button is pressed. Infix notation needs to deal with operator precedence, operator associativity (right-associative vs. left-associative), unary vs. binary operators, and incrementally building and evaluating a syntax tree, along with a way to edit this tree to implement a backspace key. Some calculators even let you edit in the middle of an expression, but mine only allows deleting from the end.

For the GPU architects out there who stare at IEEE 754 numbers all day, there are buttons to convert the internal floating-point value to and from 32-bit or 64-bit IEEE 754 numbers.

The user interface is HTML, CSS, and JavaScript. The calculator itself is written in C++ and compiled to WebAssembly, with the GNU MPFR (**m**ultiple-**p**recision **f**loating point with correct **r**ounding) library actually doing most of the arithmetic operations. All calculations are done client-side in the web browser.

## Leave a Reply