News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

help with reverse poliish notation

Started by gammaman, June 26, 2009, 01:55:57 PM

Previous topic - Next topic

gammaman

Hello all, I know this is not an masm question but I received very good help the last time I was here so I figured I would ask again.  I just want to know if I did this correctly

Problem:
(8-2)*[20/(8+2)+10]


Answer:


82-82+20/10+*

Note: To avoid problems.  Let the top of the stacks read from left to right

8
2,8
6
2,8,6
10,6
20,10,6
2,6
10,2,6
12,6
72

The final answer I got was 72.






dedndave

well - the final answer is 72
so, you must have done something right - lol

gammaman

Quote from: dedndave on June 26, 2009, 02:02:48 PM
well - the final answer is 72
so, you must have done something right - lol

Are you saying that 72 is the answer you got, cus when I said 72 was my final answer that was just a first guess.

dedndave

i am saying that 72 is the correct evaluation of the expression (8-2)*[20/(8+2)+10]

gammaman

Ok good, so I do know what I am doing.

dedndave

i dunno - lol
it has been several years since i have used an RPN calculator - lol
HP used to make some pretty fancy ones that cost big bucks
they were programmable and some of them could store a few sizable programs

for the last 20+ years, i have used the same casio fx-451 batteryless calculator
it does hex and binary conversions, as well as all the usual scientific stuff
i treat it very gingerly, as i'd be lost without it

dedndave

mine is so old - lol
it folds up for the pocket
i never fold mine and i am afraid that if i did, the flex-circuit would develop breaks
a couple keys don't work, as it is


hutch--

How about a HP 11c running on its original set of batteries, bought it in 1984 and it still runs fine. Lives in my workshop somewhere and the only sign of age is the front panel discoloured slightly.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

lol Hutch
we are alike in many ways
what will you ever do if the batteries die before you ?

my game plan is to just simply stop calculating stuff - lol

tenkey

Quote from: gammaman on June 26, 2009, 01:55:57 PM
Problem:
(8-2)*[20/(8+2)+10]

Answer:

82-82+20/10+*
The correct answer depends on which way your division goes. Your answer has the division reversed from the way the subtraction works.

Conventional RPN keeps the operands in the same order:

8 2 - 20 8 2 + / 10 + *
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

Jimg

Hey Dave-

Your fx-451 is in much better shape than my fx-450, but it still works flawlessly.
Mine lives in the toolchest in the shop.
Your photography skills are much better also.  I tried to get a picture, but too much glare off the screen made it unreadable.
The 451 had much better solar cells. Mine needed too much light to get the display to work for the picture, thus the glare.

dedndave

lol Jim
my photo skills involve googling fer a pic
mine isn't that pretty, but it always sets here on the desk, so isn't too bad
the buttons that don't work are the Engineering notation buttons, i kind of miss them, but they are not critical

@tenkey
there is something called "order of operations"
it says you do the division prior to the addition (in this case)

tenkey

Look again. In RPN, the operators are performed in the order written.

The (8 + 2) is bracketed, so it is performed before division. That's why it's + /.

The reversal is that x-y is written x y -, but x/y was written y x / by OP. Convention is to write x/y as x y /.

Since division is not commutative, order of operands is important.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

dedndave

#13
lol
i don't care if you use a sideways Scotish typewriter
the order of operations is a rule of mathematics
if you don't enter them in the right order for your calculator, you may get a wrong answer
that doesn't change the rules of math
we all know about resolving things inside parens first (also part of the order of operations rules)

http://en.wikipedia.org/wiki/Order_of_operations

also, i think with RPN, you are supposed to resolve things in parenthesis and brackets first, then work your way out
this may not be the same as
QuoteIn RPN, the operators are performed in the order written.

at any rate, what i was refering to was this...
Quote[20/(8+2)+10]
the division (by 8+2, or 10) should be performed first, then add the 10 to that result afterwards
this portion of the expression evaluates to 12
if you performed the addition first, it would evaluate to 1
we know to perform the division prior to the addition due to the order of operations rules

igndenok

if i remember correctly, when i make calculator for my school project using Borland Delphi, my teacher taught about this http://en.wikipedia.org/wiki/Tree_traversal
using PreOrder for our project