Reverse Polish Notation And Its Mildly Confusing Elegance

The best rummage sale purchase I ever made was a piece of hardware that used Reverse Polish Notation. I know what you’re thinking… RPN sounds like a sales gimmick and I got taken for a fool. But I assure you it’s not only real, but a true gem in the evolution of computing.

Best rummage sale find ever!
Best rummage sale find ever!

Sometime in the 1980s when I was a spotty teen, I picked up a calculator at a rummage sale. Protected by a smart plastic case, it was a pretty good condition Sinclair Scientific that turned out when I got it home to have 1975 date codes on its chips, and since anything with a Sinclair badge was worth having it became mine for a trifling amount of money. It had a set of corroded batteries that had damaged one of its terminals, but with the application of a bit of copper strip I had a working calculator.

And what a calculator! It didn’t have many buttons at a time when you judged how cool a scientific calculator was by the prolific nature of its keyboard. This one looked more akin to a run-of-the-mill arithmetic calculator, but had button modes for trigonometric functions and oddly an enter key rather than an equals sign. The handy sticker inside the case explained the mystery, this machine used so-called Reverse Polish Notation, or RPN. It spent several years on my bench before being reverently placed in a storage box of Sinclair curios which I’ve spent half a day turning the house over to find as I write this article.

Jan Łukasiewicz. Anonymous, [Public domain].
[Jan Łukasiewicz]. Anonymous, [Public domain].
I was reminded of my Sinclair Scientific recently when I read a Hackaday comment which introduced the inventor of RPN, Jan Łukasiewicz, as an engineer rather than a mathematician. I had not previously encountered him so this opened up a fascinating exploration of his work, plus a revisiting of RPN as an adult rather than as a soldering-iron-wielding teen. Simply put, RPN is a specific method of writing math equations that is still human while being suitable for input into a computer with limited memory.

The first thing to learn about RPN is that the “Polish” part comes from Łukasiewicz’s nationality. Perhaps we Anglophones should put in a bit of effort to learn how to pronounce his name. Then the second lesson is that the “Reverse” part is important, but a smokescreen that relates only to the direction in which it is written. Write the expression one way round and it’s Polish notation, the other way and it’s Reverse Polish notation.

The feature which makes PN and RPN special is that the operator  does not come between operands, but follows them. So for example, where you might be used to writing  2+2=  to get 4, in PN you would write  + 2 2 , and in RPN you would write  2 2 + .

[Łukasiewicz] was a logician, and his notation was designed with the purpose of removing parentheses when each function has a constant number of arguments. Thus, we’re used to writing  (2+2)*3  to get 12. In PN we write * + 2 2 3 or * 3 + 2 2 , and in RPN we write  2 2 + 3 *  or 3 2 2 + * . (The latter versions work b/c multiplication is commutative.)

PN and RPN might be one of those slightly interesting mathematical factoids some of us in our community have heard about in passing some time in our education. But in fact is it much less obscure than that because RPN has a very useful property. It lends itself to use with a stack, to perform calculations. The operands and operator are in effect ready lined-up to be to be distinguished from each other by their order of precedence, meaning that a device such as a calculator could be produced more easily using RPN than conventional notation. While those of us who still use a physical calculator are used to conventional notation as an input, some earlier calculators used RPN to do more with less.

Which brings us back to my Sinclair Scientific mentioned above. When people think of an RPN calculator, it’s likely that they’ll single out Hewlett-Packard as a manufacturer. You can still buy an HP that uses RPN, and indeed not so long ago their adherence to RPN was considered a significant selling point by HP. But the Sinclair is a little different. Clive Sinclair always made a speciality of extracting the maximum performance from the minimum of components to deliver the lowest price, even if sometimes the quality or utility of his products suffered as a result.

This calculator then is a Sinclair product at its finest, featuring a cheap arithmetic calculator chip from Texas Instruments, the TMS0805, coaxed into offering trigonometric and logarithmic functions by some astounding coding from Sinclair employee Nigel Searle. The result was a single chip scientific calculator at a fraction of the cost of those from TI or HP, but with the penalty of reduced speed and floating-point accuracy. We covered [Ken Shirriff]’s reverse engineering of a Sinclair Scientific a few years ago, even today it’s an intriguing machine.

In use it’s a frustrating exercise in learning RPN again on the fly, that turns into something close to a mathematical puzzle as the minutes tick by and that LED display drinks power from its 4 AAA batteries. That would have been no laughing matter in 1975, as a set of 4 alkaline AAAs would have been considerably more expensive in real terms than they are now. Once you are in the RPN Zone it really is extremely logical and easy to use, but the same can’t quite be said for the Sinclair. A complex sequence of keys is required for most of its functions, meaning that its novelty wears off after a while. Still, with early ’70s design aesthetic, at least it’s quite attractive to look at.

Technically the Cambridge has 3 chips, the TMS0805 and a couple of display drivers. But they’re ancillary to the processor, and the HP calculator at the time used a 5-part processor chipset.

HP9100A image, the first Hewlett-Packard calculator to use RPN, Rama, [CC BY-SA 2.0].

131 thoughts on “Reverse Polish Notation And Its Mildly Confusing Elegance

  1. I recall the Sinclair Scientific (made mine from a kit that cost just shy of 50 pounds in 1974) was a little inaccurate in places. I found that common logarithms, for instance log (base 10) of 2 came out as 0.30111 rather than 0.30103 .. it did, however help me through my first year of an Electronics degree course.

  2. I wrote my first morse code trainer on my hp48sx, which i still have. I even dialled into the internet with a vt100 terminal emulator over a 9600 baud modem. The best trick was using the infrared port one time in a maths class to clone the teacher’s TV remote. In the middle of the class, the TV turns on…. As the confused teacher gets up… It turns off…. He sits…. TV turns on again…. Then he unplugged it….

    I even managed to program the saturn chipset in assembler. Fun times!

  3. In the early 80’s, I bought a HP9100A at Boeing Surplus in Renton, WA., for $20. Got it home, fired it up and what do ya know, the display lit up and the functions worked! I stored it in the attic for 2 decades, dug it out again, fired it up and it still worked! Put it on eBay, and it went for somewhere between $600 and $700. The buyer, from the East coast somewhere, flew out, drove to my home, witnessed it run, and paid for a separate airline seat to get it back home, not trusting the mail or parcel delivery services at all. He said that once the displays die on these, they are trash. I’ve seen a couple since then go for well over $1000 in working order.

  4. The article states ‘(The latter versions work b/c multiplication is commutative.)’. Not quite correct they work because the values are pushed onto the stack when the enter key is pressed – in this case bettween the digits where the space is. There is a proper symbol for the enter operation of a down stroke with a left facing arrow on the bottom a bit like a reversed capital L with an arrowhead.

    The HP9825A was an interesting and useful desktop programmable calculator as well – it used HPL and was set up to read and control external hardware. HPL was an extension of RPN also it used an assiment arrow so rather than n=n+1 that is illogical n+1 –> n where –> was the assignment operation.

    I still have and use three HP RPN calculators though the battery pack in the HP45 has long died.

  5. I’ve used my RPN HP almost daily since 1988.
    Or to be precise, the first one got ugly after 20 years, so I found a mint condition one on eBay that I use now.
    I thought of getting a third for spare, so I will never have to use anything else in my entire career, but it turned out that there’s an Android app that emulate my HP. So I don’t actually need more.

  6. In 1975 my Boss, the Head of Engrg., had a expensive (bought by the company) HP RPG calculator. I purchased myself a
    Litronix 2230 Calculator with Algebraic Logic and ran Rings around his “Piece of Junk”. We had a big job that required thousands of Trig. calcs. With the Laws of Sines & Cosines in my memory and my “Cheap” calculator (with no Trig. functions) I got the job done and soon became The Boss.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.