Postfix Math Expressions


The example is 82 2 / 4 + 5 /, which gives 9. 82 2 / gives 82/2 or 41; 41 4 + gives 41+4 or 45; 45 5 / gives 45/5 or 9.

Can anyone give me suggestions of operators to add?

All the operators

I plan to add ^ and unary - and +; what others should I add?

You could add factorial and functions such as sin and cos

Yeah! sin cos sec csc tan cot and a+each of those.

How about XCH ie exchange the last 2 elements on the stack because they happen to be in the wrong place for division,
how about a SQRT function., Y^ X , 1/X, ln X, e^x, log X, 10^x, Fract(ional oart), Trunc(ate the fraction part), CHS ( change sign, this will do urany minus), etc
(Just look up HP RPN calculator in Google)

82 2 / should give 82/2 or 41, which it does.

i see you're coming from a infix to postfix translation so presumably everyhthing would already be in intended order. however if/when your routine eventually becomes part of a calculator where users enter in postfix form, you may end up with 2 and 82 in the stack, but you really want to do 82/2 but there's no operator, that's when you apply XCH (2 82) becomes (82 2), before you apply (/)

So someone might want to get b then a then try to get a/b?

correct. it happens a lot when using an RPN calculator... you do a string of numbers (the result is left on the stack), do anothere string of calcutations - the result now goes to the stack too, but you want to divide the 2 - but you entered them in the wrong order - thats were XCH comes in

Maybe I should borrow a symbol from Befunge, namely \ to swap the top two values on the stack.

if you find a symbol with 2 arrows 1 pointing left, the other pointing right, that will do nicely

But how'd people type it into the input easily? I don't want to use non-keyboard characters.

i thought you were going to use keys to click on.
if you're using the keyboard, you can probaby use \ for exchange - how about Alt-/ , can you detect that?
how will you handle sin,cos, etc? the user types "sin", etc.

I'm having people type the math expression into the SOLVE MATH EXPRESSION block.

Yes. 1 atan would give 45, 45 tan would give 1, etc.

maybe it could be a word-type operator also , use "SWAP".
or re-use the equal sign (=) as swap, since "=" wont be used anymore in postfix. BTW swap can also be used in those cases where you have a and b on the stack but you really plan to do b-a, so you need SWAP before you press "-"

Yeah, that sounds better.

??? I'll have = for checking equality -- 2 2 4 root = would give true. (2 4 root is the square (2) root of 4. I might have sqrt, though.)

you're right about = being used for equals. out of curiosity, how will you handle the result of a compare? just show true or false? how will you control what is the THEN part, and the ELSE part? you better not include logical expressions for now, unless you also want to write a postffix compiler.


What "then" and "else" parts?

Oh and I've added "swap" in the latest version.