Author Topic: Order of Operations  (Read 7819 times)

0 Members and 1 Guest are viewing this topic.

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Order of Operations
« Reply #15 on: April 06, 2014, 09:02:04 am »
Ok, ill talk to Xeda then. But, for now, let's talk algorithm.


step 1: error codes. Count up # of ( and # of ). If ( > ), then too many (, or if ( < ), then too many )
step 2: find first ). move backwards to first ( we find. That's the first thing to evaluate. Move expression contained to some other area, rinse and repeat until there are no more () sets.
step 3: dump whatever is left into some other area
step 4: parse some other area, with * and / first.


how difficult would it be to add support for other variables. like a,b,c,y,z and so on.
« Last Edit: April 06, 2014, 12:25:17 pm by ACagliano »

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Order of Operations
« Reply #16 on: April 06, 2014, 06:29:35 pm »
Support for other variables is just  find and replace. You would probably do that when you parse the numbers to convert them into whatever format you plan on using, just replace the variable with the value it holds.

Step 2 and 4 will use  the same code to parse, since there won't be any parentheses to evaluate in step 2 (you've found the innermost parenthesis), so you should just need one last call to the parse routine (that handles the multiplication, division, etc.). I think step 3 is unnecessary.

EDIT: Also, there are more possible errors than just the parentheses, for example you need to make sure there is a number/expression before and after every operator (2+3* is invalid, as is +2+3*4) and ideally you'd have something in between the parentheses (not "2+3*( )" ), but i would set that aside for now. I wrote a little parsing routine last night just to see how difficult this project would be, currently it just converts the numbers into a 2-byte number (that fits inside hl/de/bc/etc.) and finds the first parenthesis.
« Last Edit: April 06, 2014, 08:04:33 pm by chickendude »

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Order of Operations
« Reply #17 on: April 06, 2014, 08:09:46 pm »
For the other variables, I'm talking about something to this effect:
3x+7y=-2x-14y
the calculator sees the =, and then the presence of x AND y, and automatically determines its a system of equations problem and solves for x and y.

As for the second part, if you're evaluating step 2 in my algorithm, you should still have () in there, or you should be on to step 3?


response to edit: yeah, those are also invalid. let's get the algorithm working, then we can work with error finding :p
« Last Edit: April 06, 2014, 08:11:51 pm by ACagliano »

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Order of Operations
« Reply #18 on: April 07, 2014, 08:28:59 am »
I'm just saying that once you're at step 3, you should have a simplified equation, so all you need to do is pass that last equation back to your routine for evaluating expressions once more. You can just overwrite the space you're working with (it should be smaller anyway), there's no need to copy it someplace else, evaluate that, then copy it back. Just evaluate what you have there the same as you did for all the parenthesis pairs, just like treating it as if there were one final set of parentheses around the equation: ( 2+4*(3/(7+1))+3 ).

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Order of Operations
« Reply #19 on: April 07, 2014, 08:36:26 am »
Ok, so my final question, for now, is this: this program is designed to simplify polynomial expressions. so for example, simplifying this: (x-1)(x+1) should result in x^2-1. How much more complicated is make it involve the x as an entity?


Also, when Axe command lists say a command takes EXPR as an argument, what does that mean exactly? What's the difference between EXPR and an Axe list, or an array of bytes?

Offline Runer112

  • Moderator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2289
  • Rating: +639/-31
    • View Profile
Re: Order of Operations
« Reply #20 on: April 07, 2014, 12:22:59 pm »
Also, when Axe command lists say a command takes EXPR as an argument, what does that mean exactly? What's the difference between EXPR and an Axe list, or an array of bytes?

In the command list, EXP signifies any expression that returns a numerical result.

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Order of Operations
« Reply #21 on: April 08, 2014, 02:16:25 pm »
I'm not sure, if that's how you're planning on doing it, you might want to reconsider your algorithm. I'm not sure what the best way to do that might be, maybe working one "group" at a time? For example "3+x(2x-3(1+x))-3x(x^2+2)+2x", you would first work "x(2x-3(1+x))" then "3x(x^2+2)" and finally you'd put it all together and try to simplify it.

That seems a good deal more complicated, i'm not really sure how i'd go about it.

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Order of Operations
« Reply #22 on: April 08, 2014, 06:34:26 pm »
Deep Thought gave me another way to do it, which I'll modify slightly.

Create a stack, with number (or var), operator, and precedence,

where precedence is  OpPrec + ( 2 * N).
OpPrec = 0 for addition or subtraction, 1 for multiplication and division
N = nesting level; every time we hit a (, we add 1 to N. Every time we hit a ), we minus 1 from N

Imagine the following:  7x + 3x^2 - (x^2 + (2x - 1))
7 is pushed as the the number
implicit multiplication, so * pushed
precedence = 1 + (2*0)
x is pushed as number
+ pushed
precedence 0
... and so on until...
hit the first (. Add 1 to N. N=1
x^2 is the value
operator is +
precedence is 0 + (2*1) = 2
hit next (. Add 1 to N. N=2
2 is the value
implicit multiplication.
precedence is 1 + (2 * 2) = 5
... and so on

Then, you take that stack, and process it in order of descending precedence.

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: Order of Operations
« Reply #23 on: April 09, 2014, 12:36:10 am »
Wait, DT is still around ? O.O

Offline ACagliano

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 919
  • Rating: +32/-2
    • View Profile
    • ClrHome Productions
Re: Order of Operations
« Reply #24 on: April 09, 2014, 01:56:44 am »
clrhome.org. He owns. Im one of the admins. I have ways to get a hold of him lol.

Offline Streetwalrus

  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3821
  • Rating: +80/-8
    • View Profile
Re: Order of Operations
« Reply #25 on: April 09, 2014, 04:04:38 am »
Oh LOL yeah forgot about that.

Offline chickendude

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 817
  • Rating: +90/-1
  • Pro-Riot Squad
    • View Profile
Re: Order of Operations
« Reply #26 on: April 09, 2014, 09:09:32 pm »
That sounds pretty much like what we were talking about before, only the other way didn't require a special stack as it's processed on the fly. That sounds like a fine method to me, personally i would start working on converting something like "3+2(4-1)" into "3 + 2*4 + 2*-1". Once you get to that point, adding X shouldn't be too difficult.