A reverse polish notation calculator written in Python 3.
A reverse polish notation calculator written in Python 3.
rpcalc is a stack-based reverse polish notation calculator based on the HP 11C Scientific Calculator with extended features, such as:
You will need Python 3.3 or higher to run rpcalc.
The latest stable version of rpcalc is available on the AUR as rpcalc-git. Install it as you would any other AUR software.
I am working to put rpcalc on many repositories of various linux distributions. Until then, simply download the necessary files from the git repository.
Switch to the rpcalc directory, and run the following from a command shell:
python3 setup.py install
It is possible that your system calls your Python 3.3 binary something different, such as python
(ArchLinux) or py33.exe
(some Windows). If this is the case, replace python3
in the above example with the proper executable.
rpcalc should now be installed. Type rpcalc into a command shell to run it.
If you would prefer not to install rpcalc to your system, follow these instructions.
Switch to the rpcalc directory, and run the following from a command shell:
python3 rpcalc.py
It is possible that your system calls your Python 3.3 binary something different, such as python
(ArchLinux) or py33.exe
(some Windows). If this is the case, replace python3
in the above example with the proper executable.
rpcalc uses a stack for all operations. Users unfamiliar with RPN or stack-based calculation are encouraged to read Basic Example below and a general RPN tutorial (short and sweet).
To multiply seven by the sum of two and one million:
2
Enter
. This puts two into the stack.1e6
+
. This adds what you just typed with the most recent stack entry.7
x
. This multiplies seven with the result of the last calculation.Your result, 7000014.0
, is now the only entry in the stack. You can confirm this by pressing p
or #
.
Enter
is pressed or an operation is entered.Enter
.The most recent and second-most recent stack entries will be denoted x and y respectively. You can always peek inside operators.py for a more objective explanation of these functions.
?
- enter interactive help modep
- prints the stackQ
- quits the program, no matter what
D
- drops x and pushes stack items to compensateC
- clears the stack#
- displays a message with the current stack length (does not push anything to the stack)w
- swaps x and y
Enter
(while no number is being entered) - duplicates x and pushes it+
-
*
/
- basic arithmetic operationsx
- shortcut for *
n
- returns ( x * -1 )%
- returns the remainder of the division of y by x
f
- floor-rounds x to an integerln
- returns the natural log of the most recent stack entry ( x )^
- returns y to the xth powersqrt
- returns the square root of x
abs
- returns the absolute value of x
!
- returns the factorial of x
S
- returns the sum of all stack entriesP
- returns the product of all stack entriesmean
- returns the arithmetic mean of all stack entriesmed
- returns the median of all stack entriesin rpcalc, constant operators begin with k
to prevent conflicts with other operators
ke
- returns Euler's number: the base of the natural logarithm and the exponential functionkpi
- returns pi: the ratio of a circle's circumference to its diameter==
- returns 1 if x is equal to y, otherwise returns 0=!
- returns 0 if x is equal to y, otherwise returns 1<
- returns 1 if y is less than x, otherwise returns 0>
- returns 1 if y is greater than x, otherwise returns 0=<
- returns 1 if y is less than or equal to x, otherwise returns 0=>
- returns 1 if y is greater than or equal to x, otherwise returns 0deg
- converts x (radians) to degreesrad
- converts x (degrees) to radianssin
- returns the sine of x (radians)cos
- returns the cosine of x (radians)tan
- returns the tangent of x (radians)asin
- returns the arcsine of x (radians)acos
- returns the arccosine of x (radians)atan
- returns the arctangent of x (radians)rand
- returns a random number between 0 and 1Other operations can easily be added by modifying operators.py
.
Results are designated with >>>
, but these are really stored in the stack and displayed only because they are the current x value.
1 Enter 2 +
>>> 3.0
2 Enter Enter Enter * *
>>> 8.0
10 ln
>>> 2.302585092994046
256 ln 2 ln /
>>> 8.0
2 Enter 256 ln w ln /
>>> 8.0
2 Enter 3 ==
>>> 0
2 Enter Enter ==
>>> 1
-s
or --stack-size
- limits the stack size to the integer provided. Empty stack entries become 0 when using a limited-size stack.-i
or --initial-values
- takes space-separated numbers and pushes them to the stack.-e
or --exclusive
- sets the stack length to the amount of numbers provided with -i
I found a bug! Let me email that to you...
Thank you, but please don't email me the bug! Make sure it's not a known issue, and write me a bug report here or if you're familiar with git: fork, fix, and file a pull request.
How do I do log base x?
A log
function is not scheduled to be implemented because the functionality is already there and because there is no reason to memorize which stack item will be the base and on which item the log will operate.
You will need to use the change of base formula:
256 ln 2 ln /
There's
mean
andmed
, so why are there no mode, maximum, or minimum operators?
Arithmetic mean and median return one result, which is pushed into the stack. Mode, maximum, and minimum all have the capacity to produce more than one result. There is no non-arbitrary method of deciding the order in which the multiple answers would be pushed into the stack. Answers like these would be no good if the user didn't know which answer s/he wanted or where it went in the stack. Procedurally, these functions would be easy to write, but there is no clear way to display the answer to the user.
#DEBUG
tags. This is intentional; these operators will be removed once rpcalc graduates from version zero.At time of writing (August 2013), no stack-based RPN existed with the features and extensibility for which the author was looking. This project is meant to be a test of git, GitHub, vim, Python 3, and Object-Oriented Programming in general.