Terminal RPN Calculator
Screenshots
Preface and history ๐
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.
Installation ๐ฆ
Linux installation ๐ง
- 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
Windows installation ๐ช๏ธ
- 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.
Usage ๐ก
Disclaimer โ ๏ธ
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.
General Usage ๐
- 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).
User Interface ๐ฅ๏ธ
The colors in RPN calc are taken from the default colors of the terminal shell itself. No other distracting fancy colors.
- Example to use colors as MidnightCommander:
xterm -bg "#3465A4" -fg white -e rpn - Example to use green on black as back in the days:
xterm -bg black -fg "#00FF00" -e rpn
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.
Hotkeys โจ๏ธ
- 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 โก
Info about commands ๐๏ธ
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.
General commands ๐ข๏ธ
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)
Stack commands ๐
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)
Basic Arithmetic Operations ๐งฎ
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 (+/-)
Math functions ฦ(๐ฅ)
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)
Constants โ
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)
Memory commands ๐ง
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.
Modes ๐ง
Display Modes ๐๏ธ
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
Base Modes โ๏ธ
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
Notation of dec, hex or bin value(s) โ๏ธ
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
Conversion Modes ๐ช
angle conversions
deg2rador `d>r or deg>rad โ Degrees-to-radians conversion.rad2degor `r>d or rad>deg โ radians โ degrees
temperature conversions
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.
time conversions
>hmsor2hmsโ decimal hours โ H.MMSSss>hor2hoursโ H.MMSSss โ decimal hours
length conversions
english conversions
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
metric conversions
km2morkm>m: kilometers โ metersm2kmorm>km: meters โ kilometersm2cmorm>cm: meters โ centimeterscm2morcm>m: centimeters โ metersm2mmorm>mm: meters โ millimetersmm2mormm>m: millimeters โ meters
japanese conversions
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
Limitations โฒ โผ
- 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.
To Doโs and Niceties ๐ง
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
Error reporting ๐
Errors and bugs are possibly included. Error reports or corrections are welcome: โ๏ธ sery@solnet.ch
Special Thanks ๐ค
- 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.
see also
bc(1), dc(1)