Goodbye dc, welcome luka: a new RPN calculator for the Terminal
The Reverse Polish Notation (RPN) is a method for performing calculations without the need for parentheses. RPN was popularized in the ’70s and ’80s by Hewlett-Packard (HP), that used it in all its scientific and financial calculators.
When using calculators from rival Texas-Instruments, which all relied on parentheses, it was easy to lose track of how many parentheses had been opened or closed, often forcing users to re-enter the entire expression from scratch. Those who used an RPN calculator didn’t have these problems, although they had to overcome a small initial learning curve to get used to the new notation.
Today, physical calculators are rarely used, and almost everyone prefers to use an app on their phone or computer. However, there are still some die-hard nerds who refuse to give in to the graphical interface and insist on using the Terminal. Why? For example, because it’s much easier to enter numbers and complex operations with a keyboard than to press the glass keys of a virtual calculator.
– Image generated by Google Gemini.
A bit of history
Poland gave us Nicolaus Copernicus, Fryderyk Chopin, Joseph Conrad, Marie Curie, Arthur Rubinstein, and Stanislaw Lem. But it also gave us Jan Lukasiewicz, a mathematical logician (and philosopher) who, about a hundred years ago, invented a mathematical notation that did not use parentheses.
In this Polish notation, all operation symbols ( \( + \), \( - \), \( \times \), \( / \) ) precede the numbers they apply to. The same applies more generally to mathematical functions, such as \( \sqrt{} \), \( \sin \), \( \cos \), \( \exp \) (operators) and the numbers or variables (operands) they act on.
For example, \( 3 + 4 \) is written in Polish notation as \( + \ 3 \ 4 \), while \( (5 + 2) \times (5 - 2) \) becomes \( \times + 5 \ 2 - 5 \ 2 \).
For many years, this remained just an odd idea, because the benefit of not using parentheses didn’t seem to outweigh the unusual and more difficult-to-read notation.
Then, in the late ’50s, John McCarthy brought back Polish (or prefix) notation for LISP, one of the most influential programming languages of all time,1 developed with the goal of being applied to the development of programs in the field of nascent artificial intelligence.
In fact, prefix notation not only greatly simplified the implementation of the LISP interpreter, but also made it possible to represent both code and data in the same form, an invaluable feature for a language designed to make computers intelligent. But better not to digress.
Reverse Polish Notation
But the real breakthrough came by inverting the order between operands and operators, that is, by writing the numbers first and then the operations to be performed on them, so \( 3 + 4 \) becomes \( 3 \ 4 \ + \) and \( (5 + 2) \times (5 - 2) \) becomes \( \ 5 \ 2 + 5 \ 2 - \times \).
This Reverse Polish Notation (RPN) was discovered and rediscovered at least three times in twenty years, but it only became popular in the early ’60s when Friedrich Bauer, the inventor of the concept of stack and one of the developers of the ALGOL computer language, and Edsger Dijkstra, famous for solving the “shortest path problem” and for a thousand other things, took up the concept again, because it reduced access to memory, which at the time was scarce and slow, and ensured that all operations were performed within a well-defined memory area (the stack), that was therefore easier to manipulate.
The advantages of RPN for performing mathematical calculations were so evident that HP used it in practically all its scientific and financial calculators, at least until the early ’90s. The development of larger memories and much bigger displays made it possible to easily view and modify the entire entered operation – not just the last number entered – thus reducing the necessity of using RPN.
A calculator in the Terminal
Those who wish to use a traditional calculator in the macOS or Linux Terminal have an almost mandatory choice: bc
, the basic calculator, that has been a part of UNIX since 1975, a full 50 years. The current version, available on Linux and macOS, has been completely rewritten, but the original was just an interface for dc
(desk calculator), one of the oldest utilities present in UNIX and even older than the C language.
And dc
was… an RPN calculator. A choice that confirmed HP’s intuition: RPN notation was more efficient and easier to implement, and therefore also ideal for the limited-resource computers of the ’70s.2
Today, RPN is no longer a choice forced by hardware limitations, but rather a choice driven largely by convenience. Once you’ve spent half an hour learning to use it, you discover that it’s really much more convenient to perform calculations with this notation than with the traditional algebraic notation.
And then, maybe, there’s also a certain pleasure in going against the flow: in a world where everyone conforms to the dictates of (real or presumed) social media influencers, using an RPN calculator becomes a way to stand out, to feel different, almost a declaration of intellectual independence.
dc
is still available on Linux and macOS and can be used in the Terminal, but it’s quite a hassle, its fifty years clearly show. The results of the operations are not visible unless you explicitly print them on the screen with p
. The stack isn’t visible either, unless you explicitly request it with f
. Negative numbers are indicated with an underscore _
and not with the usual -
sign, so that \( - 4 \) must be written as \( _4 \). And so on.
But starting (almost) today, there’s dc2
, or rather, luka
.
The 21st-century RPN calculator
Since a few weeks we have a new RPN calculator for the macOS and Linux Terminal, initially called dc2
, in homage to the venerable, now over-50-year-old calculator.
However, dc2
sounded too much like a rewrite of the original, while this is a completely new project, written from scratch in C
. It therefore deserved its own name, luka
, in honor of Jan Lukasiewicz, the inventor of the Polish notation (see the box above).
Compared to dc
, luka
is truly from another century, and it shows as soon as you start trying it out. It’s interactive, it shows the content of the stack at all times, it has a history of the operations, variables, a concise but complete inline help, a crystal-clear manual page (if only there were many more like this!)
And luka
is also made in Italy. At a time when national identity is exalted for mostly trivial reasons, it’s nice to see something that really deserves to be valued.

The program currently has a small bug related to the display of the history, but the author crunches code much faster than I write words, so it’s very likely the bug will be fixed ASAP (vacations permitting).
Evolution of the code
The development of dc2
/luka
has been incredibly fast. Behind the project is Davide Mastromatteo, known among his pen pals as @mastro35 and author of the excellent blog The Python Corner.
In just a few days, Davide went from version 0.1.0, which was perfectly functional but had a still-raw user interface,
to version 0.2.0, with a much clearer view of the stack, the ability to use degrees and radians, and to change the number representation format, as well as a more compact help.
Version 0.3.0 introduced the history of operations and a further revised help,
while version 0.4.0, which coincided with the program’s name change, brought us variables and several new keyboard commands, as can be seen in the first two images of this post.
Using luka
Installing luka
on a Mac or Linux is a breeze, at least for those who use homebrew
(and those who don’t should start doing so immediately)
brew install mastro35/homebrew-mastro35/luka
Otherwise, just download the latest release of the source code, unzip it, and run
make clean && make
from the Terminal, obviously after navigating to the directory that contains the source code.3
Needless to say, anyone who has make
and a C compiler installed on Windows can also compile luka
on that operating system.
I almost forgot: soon there will be packages for Linux, in both deb
and rpm
formats, and who knows, maybe also aur
. Perhaps even an installer for Windows.
There’s little to add about using the calculator itself. Those familiar with RPN will feel right at home and quickly use luka
’ to its full potential. Those who don’t know RPN might be tempted to try it, if only to try firsthand how much more convenient it can be to perform calculations with an RPN calculator compared to a traditional algebraic one.
There are countless guides to RPN, but unfortunately, they are all oriented toward explaining its use with a physical calculator, such as the now legendary vintage HPs, or with graphical emulators for Android or iOS.4
Compared to these guides, there are some differences to consider. The main one is that, lacking physical keys, luka
(but also dc
) requires you to press ENTER
each time to insert a number or function. Some function names are also different, as are the commands for managing the stack or for saving and retrieving variables. For example, luka
uses store
and load
for variables, while physical or emulated calculators generally use keys named STO
and RCL
(but if the author reads this post, maybe he will change them 😂). However, apart from these tiny details, the underlying logic doesn’t change.
For my part, I tried in every way to make luka
crash, and never succeeded. I tested it with factorials of absurdly large or small numbers, divided by zero, performed the infamous\( 0 / 0 \) and then asked to calculate the reciprocal of the result. But no matter what, luka
always returned the correct result. In short, the quality is definitely there.
Conclusions
Trying luka
costs nothing. It installs (and uninstalls) in a few seconds, works on macOS, Linux, and, for those who want, even on Windows. The results it produces are exact (and this is the most important thing). The developer is skilled and open to suggestions. In short, it’s an ideal tool for anyone who wants to perform calculations from the Terminal.
Sure, it’s a niche tool, aimed at a select group of sophisticated users who appreciate RPN notation and are not afraid of the Terminal. But that’s also what what gives it its unique charm.
And then, what about the beauty of seeing a program evolve before our very eyes, maybe even participating in its development with suggestions or criticisms?
And beyond that, there’s something special about watching a program evolve right before our eyes, especially when you can join the development with your own suggestions or feedback.
-
And one of the few languages, along with FORTRAN and COBOL, to still be alive and well. ↩︎
-
Not by chance, one of the most efficient programming languages ever is FORTH, which is based on a stack and uses RPN. ↩︎
-
What, you don’t even have
make
? Then hurry up and installhomebrew
! ↩︎ -
For iOS, I recommend without hesitation PCalc Lite or, even better, the full version Pcalc, which costsbout as much as a sandwich and a small bottle of water at a café. Alternatively, WP 34s, which manages to compact everything onto a single screen, or i41CX, perfect for those who once dreamed of having one. ↩︎