| title | Terminal RPN Calculator |
|---|---|
| subtitle | an RPN calculator shell for the terminal |
| section | 1 |
| author | Simon Widmer |
| date | 31.05.2026 |
| lang | en |
| license | GNU GPL3 |
| cover-image | rpn-calc.svg |
| description | an RPN calculator for the terminal |
| header | RPN |
| footer | RPN |
an RPN calculator shell for the terminal

rpn calc v1.9 running in xfce4-terminal

rpn calc v1.9 with custom colorscheme in xterm
First of all: This calculator is not another exactly emulated Hewlett Packard ™ calculator nor is it using ROM files to provide the full functionality. At the current state, it is not able to store and execute RPL-programs. Even so, if you are used to those calculators, you might remember and recognize the most useful commands. Lets say, the calculator mimics its great predecessors. In addition to the usual abbreviated commands, these can also be written as full words. Note that some commands are not implemented in the famous calculators but I found them to be useful anyway.
I am using RPN calculators since I was a teenager and still appreciate it. Hence, it is my intention to program a calculator which supports RPN (reverse polish notation) on the one hand and is executable in a terminal on the other hand. It is written in pure python and designed to be a simple and distraction free terminal shell tool with no dependencies. Despite the simple design, it has become a quite powerful calculator with a lot of functions.
(Actually, this calculator has come a long way: I originally planned to create a pure Bash implementation using bc in the background, but ultimately decided against it during the implementation process. The current Python implementation is much more powerful and easier to maintain than the original Bash version would have been.)
Again, keep in mind: This is not the real and famous HP42S™ or HP48SX™ that is supporting programming, unit conversions, graphs, equation solvers, libraries and lots of things I do not even know of. Those real calculators are an industrial and technical work of art I admire and may never be compared with.
- Run
sudo apt install python3for Debian. Installation for other distributions may vary. There is no other dependencies. - run
./rpn.pyto start the calculator 🚀 - to install it permanently including man page, run
apt install pandocmakesudo make install
- to uninstall it, run
sudo make uninstall
- install python (Tested with Windows 11 only)
- use
run.batto start the calculator or open a cmd-shell and executepy ./rpn.py🚀 - Occasionally, I create an executable file, which can also be used and makes installing Python unnecessary. It is stored in the
bin/directory.
Don't use this calculator if you plan to fly to the moon. I'm not kidding — expect that some results might be wrong. Testing all functions and edge cases is a huge task.
- If you are not familiar with rpn calculators and with reverse polish notation, you might not want to learn this notation unless you know the advantage of this concept and are willing to rethink.
- Typing in either small or capital letters is valid.
- Errors are shown in red and cleared after 2 seconds.
- Decimal numbers are separated by a period.
- the size of the stack is unlimited (in theory).
CTRL-N for help, CTRL-X to exit
┌──────────────────────────────────────────────┐
│ Terminal RPN Calculator vX.x.x │
├──────────────────────────────────────────────┤
│ 4: │
│ 3: 64│
│ 2: 100│
│ 1: 45054│
└──────────────────────────────────────────────┘
input > ▒
Example Text User interface above with some numbers entered in the visible stack and cursor after input.
- CTRL-X → exit
- CTRL-L → clears entire stack
- BACKSPACE →drop stack 1
- DEL → drop stack 1
- CTRL-K → drop actual input >
- TAB → swap stack 1 and stack 2
- ENTER → duplicate stack 1 (if input is empty) else perform the operation or function
- CTRL-E → edit stack 1
- CTRL-N or ? → help
Hint (ℹ): If a hotkey is not working, this is most likely due to the terminal shell in use and it's predefined hotkeys. Workaround: enter the according command at the input prompt and hit ENTER.
commands are executed by typing in and hit ENTER. This might feel rather like a shell therefore.
- Advantages:
- no polling for keys but just waiting for input.
- No CPU usage at this point.
- Simple Code.
- shell feeling
- commands should be mostly the sames as the commands of RPN calculators
- easy to memorize
- Disadvantages:
- typing commands for a calculator might feel unusual at first
- no polling for keys but just waiting for input.
exitorquitor justofffollowed by hitting the ENTER-key to leave the rpn calculator.helpor?orhlpto get a quite detailed help.aboutorinfoto get a little bit info about myself, the calculator and the licenserefreshto clear a destroyed UI.getkeyto check the key code (used for debugging purposes only)
clrorclearwill clear the entire stack.swaporswporx<>ywill swap stack 1 and 2.dropwill delete stack 1.drop2will delete stack 1 and stack 2.duporduplicateorenterwill duplicate stack 1dup2will duplicate stack1 and stack 2editto edit stack 1depth→ number of elements in stack.avgoraverage→ average of entire stack.save→ save entire stack to~/stack.txtin decimal values. Windows:C:\Users\YourUserName\stack.txtsave <filename>→ save entire stack to~/<filename>in decimal valuessort→ sort entire stack in ascending order and if already, in descending order.sum→ sum of entire stack.rollup→ Moves the very last element of the stack to Level 1 (Bottom to Top).rolldown→ Moves the element from Level 1 to the very bottom of the stack (Top to Bottom).over→ copies stack 2 to stack 1 (without deleting stack 1).pick→ copy stack[n] to top (n from stack 1, 1-based)
As with the real rpn calculators, hitting the operators does execute an immediate operation. You do NOT have to hit the ENTER-key to do so.
+orplus→ addition-orminus→ subtraction*ormul→ multiplication/or:ordiv→ division%→ percentage^orpow→ exponentiation (yˣ)_→ changes the sign of a number (+/-)
The following functions can be used followed by hitting ENTER:
- Sign and basic operation ⊕/⊖
negorchs→ negationabs→ absolute valuecomborcombination(s)→ returns the number of combinations of two numbers in the stack (nCr)iporintorinteger→ integer of a given value (by cutting, not rounding)mantormantissa→ mantissa of a numberxpon→ exponent of argument (floor of log10 of abs value)factorfactorial→ factorial (x! = Γ(x + 1)). Note that the returned value can be very high, as the factorial grows very fast.fracorfracorfractional→ fractional part of a number (x - ip(x))ceil→ returns next greater integerfloor→ returns next smaller integermodormodulo→ returns modulo of two numbers in the stackpermorpermutation(s)→ returns the number of permutations of two numbers in the stack (nPr)ranrandorrandom→ returns random number0 < x < 1rndorround→ Rounds number as specified in level 1.sign→ sign of x → -1, 0, or 1gcd→ greatest common divisorlcm→ least common multiple
- Powers and roots 📈
reciorreciprocalorinv→ reciproke (1/𝑥)poworpower→ exponentiation (yˣ)sqorsquare→ square (𝑥²)sqrtorsquareroot→ square root (√𝑥)rootorrtorxroot→ ⁿ√𝑥exp→ eˣ (inverse of ln)exp10oralog→ 10ˣ (inverse of log10)expm→ eˣ - 1 (more accurate for small x)
- Logarithms 📏
ln→ natural (base e) logarithmlog10orlog→ common logarithm (base 10)log2→ binary logarithm (base 2)
- Trigonometric functions (in degrees) 📐
sinorsine→ sineasinorarcsine→ arc sinesinhorsinushyperbolicus→ hyperbolic sinecosorcosine→ cosineacosorarccosine→ arc cosinecoshorcosinushyperbolicus→ hyperbolic cosinetanortangent→ tangentatanarctangent→ arc tangenttanhtangenthyperbolicus→ hyperbolic tangent
- Logic operations ✅❌
setwordsizeorstws→ sets word size for logic ops (the default word size is 64 bits)recallwordsizeorrcws→ recalls word size to stackand→ Logical or binary AND.or→ Logical or binary ORxor→ Logical or binary exclusive OR.not→ Logical or binary NOT.slorlsl→ logical shift left 1 bitsrorlsr→ logical shift right 1 bitslb→ shift left 1 byte (8 bits)srb→ shift right 1 byte (8 bits)asr→ arithmetic shift right (sign bit replicated)rl→ rotate left 1 bitrr→ rotate right 1 bitrlb→ rotate left 1 byte (8 bits)rrb→ rotate right 1 byte (8 bits)
The following constants are implemented for now and can be entered followed by hitting ENTER:
pi: circle constant (π = 3.1415927…)tau: "true" circle constant (𝜏 = 2π = 6.2831853…)corlightspeed: speed of light in a vacuum (𝑐) 299792458 m/seuler: 2.7182818… (ℯ)gravityorg: 9.80665m/s² (𝑔)phyorgoldenratio: 1.6180339887… (φ = (1 + √5) / 2)
memormemory→ shows all available memory of the OS, if supported.stoorstorewill store value of stack 1 into memory and delete the value in the stack.rclorrecallwill recall value from memory and append it to the stack. Memory contains0by default.mcormemclrormclearwill clear the memory and set it to0.
fix→ Fixed-Decimal (default), will set and showndisplay digits by the stack 1 value. E.g.3 *ENTER* fix *ENTER*will limit to 3 digits. Allowed value is0-16eng→ Engineering notation, will set and showndisplay digits by the stack 1 valuesci→ Scientific notation, will set and showndisplay digits by the stack 1 value
decwill show the stack in decimal valueshexwill show the stack in hexadecimal valuesbinwill show the stack in binary valuesoctwill show the stack in octal values- the current mode is indicated below the stack at the input prompt if not in decimal-mode
The notation of a hexadecimal or binary number can be typed in as follows:
- decimal notation of exponential numbers: (e.g. 3e6, 1.5E4, 5.2e-3, 1E+6, 3E-5 …)
- binary: e.g.:
0b1111,0b10101010, … - hexadecimal : e.g. like
0xabcd,0xAFAEBD, … or directly without leading 0b or 0x.
Further notice 💬:
- Mixing decimal, hexadecimal and binary values in the stack is not possible.
- if in decimal mode, all entered values with leading
0x,0bor0oare directly converted into the decimal value. - same behavior and conversion for the other modes
deg2rador `d>r or deg>rad → Degrees-to-radians conversion.rad2degor `r>d or rad>deg → radians → degrees
c2forc>for°c2°for°c>°forcelsius2fahrenheit: Celsius → Fahrenheitf2corf>cor°f2°cor°f>°corfahrenheit2celsius: Fahrenheit → Celsiusc2korc>kor°c2°kor°c>°korcelsius2kelvin: Celsius → Kelvink2cork>cor°k2°cor°k>°corkelvin2celsius: Kelvin → Celsiusf2korf>kor°f2°kor°f>°korfahrenheit2kelvin: Fahrenheit → Kelvink2fork>for°k2°for°k>°forkelvin2fahrenheit: Kelvin → Fahrenheit
Note that values below absolute zero (-273.15°C, -459.67°F or 0 K) are returned as error.
>hmsor2hms→ decimal hours → H.MMSSss>hor2hours→ H.MMSSss → decimal hours
inch2cmorinch>cm: inches → centimeterscm2inchorcm>inch: centimeters → inchesinch2mmorinch>mm: inches → millimetersmm2inchormm>inch: millimeters → inchesinch2morinch>m: inches → metersm2inchorm>inch: meters → inchesfoot2morfoot>m: feet → metersm2footorm>foot: meters → feet{RESET}mile2kmormile>km: miles → kilometerskm2mileorkm>mile: kilometers → milesmile2mormile>m: miles → metersm2mileorm>mile: meters → milesseamile2kmorseamile>km: miles → kilometerskm2seamileorkm>seamile: kilometers → miles
km2morkm>m: kilometers → metersm2kmorm>km: meters → kilometersm2cmorm>cm: meters → centimeterscm2morcm>m: centimeters → metersm2mmorm>mm: meters → millimetersmm2mormm>m: millimeters → meters
sun2morsun>m: sun → metersm2sunorm>sun: meters → sunken2morken>m: ken → metersm2kenorm>ken: meters → kenshaku2morshaku>m: shaku → metersm2shakuorm>shaku: meters → shakushaku2cmorshaku>cm: shaku → centimeterscm2shakuorcm>shaku: centimeters → shakushaku2mmorshaku>mm: shaku → millimetersmm2shakuormm>shaku: millimeters → shakuri2morri>m: ri → metersm2riorm>ri: meters → ri
- the digits are limited and tested to the value
16.
I'm developing this pocket calculator in my spare time, so you'll notice that many features and functions — such as the following — are missing:
- programming
- matrix
- plotting
- variables
- Equation solving
- arrays
- complex number handling
- Unit conversion (some conversions implemented)
- IO and many other fancy features
- small subset of hotkeys — Esc-Key and F-Keys are non-functional
For the moment, I do not have intentions to implement these features. There might be other apropriate tools to use.
The following features are on my to do list:
- missing commands 🕵️
- incr, decr (increment and decrement stack 1 by 1)
- sto stack as array elements
- beep
- date and time functions (date, date+, ddays)
- key (would be nice for debugging new hotkeys)
- max, min, maxr, minr
- sto+-/* (are those useful commands?)
- missing but cool features 🌟
- getting track of command-history to implement hotkey "↑" (up) to recall the commands for the "shell-feeling"
- autocompletion and suggestions
- status of stored value
Errors and bugs are possibly included. Error reports or corrections are welcome: ✉️ sery@solnet.ch
- Jan Łukasiewicz for inventing RPN.
- Bill Joy for his eternal editor vi and Bram Moolenaar et al for Vim, the improved Version of vi for Linux and other platforms.
- Chris Kempson for the wonderful and calm colorscheme Tomorrow-Night-Blue.vim.
- John Gruber in collaboration with Aaron Swartz for Markdown
- John MacFarlane for the document conversion tool pandoc that seems to be from outer space with all its functionality.
- Hewlett Packard for the great calculators (that can be programmed with RPL) over all the years.
- Bob Bemer for the invention of ANSI Escape Code that is still supported on many platforms including Windows 11. Unbelievable after all those years!
- Guido van Rossum for inventing the Python programming language.
- Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy and Joe Ossanna et all for inventing the Unix operating system and all the developers who contributed to the Unix ecosystem. Without Unix, Linux would possibly have never been invented.
- Richard Stallman for inventing the GNU operating system and all the developers who contributed to the GNU ecosystem. Without GNU, Linux would be just a kernel and not a full operating system.
- Linus Torvalds for inventing the Linux operating system kernel and all the developers who contributed to the Linux ecosystem.
bc(1), dc(1)