jHP49 Calculator Plugin |
By Slava Pestov & Romain Guy
sp@gjt.org - guy.romain@bigfoot.com |
The calculator plugin implements a simple RPN calculator. There are
to ways to perform a calculation.
The first way is to invoke Plugins>Calculate, which displays a dialog
containing a five levels stack and a text field where an expression can be entered.
Even if only 5 levels of the stack are displayed, the stack can contain much more.
In fact, this first way acts exactly as the HP48/HP49 calculator in RPN mode.
The second way is to execute Calculator:expression
from the console (or any other EditBus-aware plugin).
The calculator does not support infix notation (for example, 2 + 3 * 7).
Instead, it uses the slightly harder to use but much easier to parse
RPN notation (the above example would be written as 3 7 * 2 +). In RPN
notation, the input is a sequence of tokens. Number tokens are pushed onto
a stack, and function tokens operate on the topmost elements of the
stack, replacing them with the result when done.
For example, consider the simple expression 3 7 * 2 +.
Entering: 3 (enter) 7 (enter) * (enter) 2 (enter) + (enter) is equivalent.
This is what actually happends when it is executed:
- 3 is pushed onto the stack
- 7 is pushed onto the stack, so the stack now contains {7, 3}
- * multiplies the two topmost elements of the stack, so the stack
now contains {21}
- 2 is pushed onto the stack, so the stack now contains {2, 21}
- + adds the two topmost elements of the stack, so the stack now
contains {23}
The following primitive functions are currently implemented:
- +, -, *, /, % (modulo), ^ (exponentation): basic arithmetic operators.
The left-hand side operand is the topmost element of the stack, the
right-hard side operand is the next element down. After the calculation,
the two operands are removed from the stack, and the answer is inserted
- clear: clears the whole stack
- drop: deletes the topmost element from the stack
- dup: duplicates the topmost element of the stack, so if the stack
contained {2, 3} it would contain {2, 2, 3} after a dup
- swap: swaps the two topmost elements on the stack, so if the stack
contained {10.3, 8.9, 9} it would contain {8.9, 10.3, 9} after a swap.
- peek: pick topmost element and place it in the command field
- poke: pick topmost element, place it in the command field and remove it from stack
- pi, e: push the contains Pi and E onto the stack
- sin, cos, tan: trigonometric functions. All angles are in radians
- arcsin, arccos, arctan: inverse trigonometric functions. All angles are
in radians
The following higher-level functions are also defined:
- neg: changes sign of topmost element. Equivalent to 0 -
- ^2, ^3: raises topmost element to the power of 2 and 3. Equivalent to
swap 2 ^ and swap 3 ^
- root3: calculates cube root of topmost element. Equivalent to 3 1/ swap ^
- +2+: calculates length of triangle hypotenuse where the two topmost
elements of the stack are the side lengths. Equivalent to ^2 swap ^2 + sqrt
- -2-: calculates a side length of a triangle if the hypotenuse and other
side length is known. Equivalent to ^2 swap ^2 - sqrt
- +3+: calculates length of 3-dimensional triangle hypotenuse where the
three topmost elements of the stack are the side lengths. Equivalent to
^2 swap ^2 + swap ^2 + sqrt
- exp: raises E to the topmost element's power. Equivalent to e ^
- rad2deg, deg2rad: converts radians to degrees and degrees to radians.
Equivalent to pi swap / 180 * and 180 swap / pi *
- cosec, sec, cot: trigonometric functions. Equivalent to
sin 1/, cos 1/ and tan 1/
- arccosec, arcsec, arccot: inverse trigonometric functions. Equivalent to
1/ arcsin, 1/ arccos, 1/ arccot
- Carea: calculates area of circle with the radius being the topmost
element of the stack. Equivalent to ^2 pi *
- Ccirc: calculates circumference of circle with the radius being the
topmost element of the stack. Equivalent to 2 pi * *
- Svol: calculates volume of sphere with the radius being the topmost element
of the stack. Equivalent to 3 ^ pi 0.75 * *
- Ssurf: calculates surface area
of sphere with the radius being the topmost element
of the stack. Equivalent to ^2 pi 4 * *
Variables can be used to store values, in a slightly clumsy way.
The get function retreives the variable named by the topmost element
of the stack. The set function sets the variable named by the topmost
element of the stack to the value of the next element.