Skip to content

dc

RPN stands for Reverse Polish Notation. Reverse Polish Notation was developed in 1920 by Jan Lukasiewicz as a way to write a mathematical expression without using parentheses and brackets. It takes a few minutes to learn RPN but you will soon see that this entry method is superior to the algbraic format.


Introduction

RPN pocket calculators became popolar with the HP calculators. HP used this entry method already for it’s first calculator in 1968. If you search the internet you will find that there is a whole fan club behind those HP calculators. I have made a number of very good links available at the end of this article. Most of those HP calculators are today collectors items and sell now for much more than their original price. In this article will present some of the RPN desktop calculators available for Linux. We will not only look at the HP emulators which are available but also other RPN calculators which are totally independent of HP. What is RPN? Why RPN? RPN calculators use a stack and all mathematical operations are executed immediately on the lower level of the stack. The stack is used as a memory to save results which you need to further evaluate your formula. Therefore you do not need any brackets on an RPN calculator. You first enter the numbers, push them up the stack, and then you say what you want to do with those numbers. Let’s say you want to calculate: (( 3 + 1 )^2 + 1) * 4 To do this you would type the following: 3 enter 1 + (immediately you see the result of this operation: 4) x^2 (immediately you see the result of this operation: 16) 1+ (immediately you see the result of this operation: 17) 4* (final result: 68) So to enter this formula you needed 9 key strokes and you could see all the intermediate results. It is basically how you would evaluate a formula when you evaluate it without calculator in your brain. In other words the calculator is much more "natural". It works the same way that your "brain works". If you compare this to an algebraic calculator where you punch in the formula as written you would need 12 key strokes and you would not see all the intermediate results. The advantages are in other words: RPN saves time and keystrokes. You never have to account for the parentheses while doing calculations. The process is similar to the way you learned math on paper. You can see the intermediary results as you perform your computations rather than just the answer at the end. This is an extremely helpful byproduct. An intermediate result allows the user to check the results and correct errors more easily. It’s easier to follow the stream of calculation. The user defines the priority of operators. RPN is logical because the user first gives the number and then tells what to do with it. Because subexpressions are evaluated as they are entered, entry errors are more obvious with RPN. On an algebraic calculator, omitting an opening parenthesis, may not lead to a calculation error until much later when an entire subexpression is evaluated. The calculators Let’s start with some simple calculators which you can just test in your webbrowser while you are reading this article and then further down I will present more powerful "machines". rpnjcalc This calculator uses Javascript. It is my own design and runs in any modern (!) webbrowser (does not work properly with opera ⅚, netscape 4, konquerer). It is different from many other javascript calculators because you can use also the keyboard to type in numbers and basic mathematical operators. Just click here to play with rpnjcalc

1 4 / p # Divide 1 by 4 and print the result 0 # Set the precision to 0 decimal places 1 k # Keep 1 decimal place 1 4 / p # Divide 1 by 4 and print the result with 1 decimal place 3 k # Keep 3 decimal places 1 4 / p # Divide 1 by 4 and print the result with 3 decimal places 16 63 5 / + p # Divide 63 by 5, add 16, and print the result 16 63 5 + / p # Add 63 and 5, then divide by 16 and print the result" | dc

echo -e "1 4 / p\n0\n1 k [ Keep 1 decimal place ]s.\n 1 4 / p\n3 k [ Keep 3 decimal places ]s.\n 1 4 / p\n16 63 5 / + p\n16 63 5 + / p" | dc

printf '%s\n' "16i[q]sa[ln0=aln100%Pln100/snlbx]sbA293A207[rewop]swsnlbxq" | dc

echo -e "1 4 / p\n0\n1 k\n 1 4 / p\n3 k\n 1 4 / p\n16 63 5 / + p\n16 63 5 + / p" | dc

https://tldp.org/pub/Linux/docs/ldp-archived/linuxfocus/English/Archives/lf-2004_01-0319.pdf