Subroutine revpolnot
~/math/afor/p/rpn.f - manual
page
Main program
RPN -> ~/bin/test-rpn
USAGE:
call revpolnot (string)
Caveat: No test yet for
division by zero or complex-valued results,
program
will just fail. Integer mode is still inconsistent., quite like Mode
Indigo the movie.
ENTRIES:
call revpolnot_dbg (.true.)
result = revpolnot_r(k,q)
PURPOSE:
Evaluates a mathematical expression given in Reverse
Polish Notation.
The stack is extended upward, k shall denote the stack
level. k=1 at return
but may be >1 if widowed levels remain. You can check the last
level with revpolnot_r
Returns result in string
To some extent, keeps integer calculations automatically integer
(no warranty).
Format code I#.# or
last operator i or n
will force integer output.
Entry _dbg requests some diagnostic printing.
ARGUMENT:
string = 'RPN-expression[
:format]'
format - Fortran code for one floating point or
integer number.
Delimiter is a blank plus a
colon.
A default format can be
specified in the environment, e.g.
setenv
RPN_FORMAT 1p,e12.4
RPN-expression may contain
numbers, symbolic constants, and operators
of which some are functions.
Constants:
pi 2pi 4pi pih piq e rad deg
pih =
/2
and piq = π/4
rad =
π/180 and deg = 1/rad
Operators or entries that push the stack
number
degreeo
immediate conversion to radians
d
duplicate
ran uniform random
number 0<y<1, seed from clock's millisecond
Operators ( | alternatives) on the last
stack position, not pulling stack:
^p
potentiation to power of p
^/p potentiation
to power of 1/p
ln | lg nat. and dec.
logarithm
o fast
conversion radians to degrees
sqr
square-root
cbr cubic-root
exp exp
sin
cos
asin
acos
tan |
tg
atan | atg
sinh
cosh
tanh | tgh
asinh | arsh
acosh | arch
atanh | arth |
ath
10^ 10**
I | int
integer floor
n | i | nint
integer round
| | abs absolute
value
f float
(last operator; under `*´-format, default, switches to float
printing)
./ reciprocal
s
revert sign
rnd uniform random
number 0<y<1, seed from stack
Operators acting on the last two stack positions, with one
stack pull
X swap
(no stack pull)
/
divide
* | . | m multiply
M | % modulus
stack(k-1),stack(k) ( % cannot be used in tsf-edit
command files )
+ add
-
subtract stack(k-1) - stack(k)
atg2 arctan2
stack(k-1) / stack(k) e.g. RPN 0.5 0.75
sqr atg2 rad / => 30.0
** | ^
potentiation stack(k-1)**stack(k)
sqs sum of squares
amp square-root sum of
squares
Operators acting on the entire stack, collapsing it to 1
gs grand sum
ga grand average
Function revpolnot_r(k,q)
Returns
as a real*8 value the result of the most recent call revpolnot
k - integer -
returned, the stack level at return time.
q - logical - true if
the value has been written to string as an
integer.
EXAMPLES:
There's a simple main program RPN
(~/math/afor/p/m/tst-rpn.f , RPN is a
symlink)
The examples following have been made with it.
-12 -12 atg2 deg . :f10.3
225.000
-30 deg
/ sin
-0.500000000000000
5 2
^ :I3.3
025
5 2 ^
:f5.1
25.0
5 2 ^
25
ran
1e8 . i
27896279
1 2 3 4
5 gs 2 /
7.50000000000000
RPN main program
USAGE:
RPN value
operator ...
OPTIONS:
DBG - debug
-q 'string' - an
alternative to unquoted arguments. The entire formula must be
specified in one quoted string
.bye