News:

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

Solving expressions

Started by RuiLoureiro, December 11, 2010, 06:58:37 PM

Previous topic - Next topic

oex

#30
The link was for future reference as you develop the algorithm.... That PHP expression table has many other operator types that you dont have yet....

With regard to your current problem everything in brackets should be calculated first bottom level up returning

2^4+16+2^2

I have downloaded your code but not had a chance to look at it properly yet

That example doesnt require bracket precidence though I think suggesting it is a different issue.... 2*2^3 is already calculated before additions

Do you mean? 2^4 * (2+2^3) + 2^2 = 36 (Calcula16 gives 44)

At least that's the only way I can make 44.... If so you are simply not calculating bracketed expressions first.... Find the lowest level brackets and work up ie

2*(2+2*(2+2)) ->

Loop to find first bracket ( then mark position.... Repeat this task until you reach ) then resolve marked position to ) giving

2*(2+2*4)

Repeat until no brackets
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

RuiLoureiro

Quote
With regard to your current problem everything in brackets should be
calculated first bottom level up returning

        This is exactly what i do

Quote
Do you mean? 2^4 * (2+2^3) + 2^2 = 36 (Calcula16 gives 44)

        No.   Not 2+  but 2*: This: 2^4 + (2*2^3) + 2^2 = 36 (Calcula16 gives 44)
        (I know very well why calcula16 gives 44)

        Calcula16 solve this correctly: 2^4 + (2+2^3) + 2^2 = 16+(2+8)+ 4 =16+2+8+4=30

        Why ? Simply because this 2^4 + (2+2^3) + 2^2 = 2^4 + 2+2^3 + 2^2. This is:
        we can remove brackets
       
Quote
...If so you are simply not calculating bracketed expressions first

       Yes. Expressions that involve powers (or exponentials) inside brackets.
       If we have not powers inside brackets we have not problems

Ok, thanks oex

oex

OK you told me this: 2^4 + (2*2^3) + 2^2 = 36 (Calcula16 gives 44) -> Without brackets this = 36
So I assumed you meant this: 2^4 * (2+2^3) + 2^2 = 36 (Calcula16 gives 44)    -> Without brackets this = 44

I cant see how you get 44 otherwise :lol But only just loaded the code.... My assumption is that if the former function is correct then you aren't calculating powers with precidence over both * and + but I will check

The PHP site table also shows operator precedence
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

RuiLoureiro

Quote
OK you told me this: 2^4 + (2*2^3) + 2^2 = 36 (Calcula16 gives 44)
-> Without brackets this = 36
            Calcula16 gives a wrong result
            With or Without brackets this = 36 too
            Without brackets we solve powers first, than * or / ... +,-
           
Calcula16 does this:  2^3 + 2 * 2^4 + 2^2 = 8 + 2 * 16 + 4 = 12 + 32 = 44
It changes 2^3 with 2^4 because it gets 2^4 first when try to solve (2*2^3) - gets 2^4 instead -
and than 2^3 and than 2^2 !

Quote
I cant see how you get 44 otherwise 
            Now you know how to get 44  :lol
    EDIT:
    If we remove brackets and want to solve 2^4 + 2*2^3 + 2^2, calcula16 gives 36
    (correct)

RuiLoureiro

#34
Hi all

Calcula17 is only one step more to solve the problem
I dont know if the problem is completely solved or not

Meanwhile, now, it solves correctly the expressions like

2^4 + (2*2^3) + 2^2 = 36        - powers inside brackets
and
(1-2*3)^2+(5-2*3)+2^2+1=29      - note that the base can be an expression:(1-2*3)^2
                                                    but cannot have exponentials inside
                                                    exponent must be 0 to 31

It must solve all other cases (i hope)

    EDIT:
            One little problem when base=1  (-1)^4=-1 (error)
            or exponent = 0                 (-1)^0=-1 (error)
           
            NEW calcula17: now (-1)^4 = 1 and (-1)^0= 1


RuiLoureiro

The LookPower algorithm
-----------------------------------
The LookPower algorithm is used with expressions without brackets

Let the expression be pointed by ESI and the length is ECX
Let _ResultZ the string where we put the final result (the ascii code)
Let SignZ the sign of the result and OperandZ the value of the result
SignZ and OperandZ are 2 dwords
Let _TblPower an array of 400 dwords (for 200 results)

a) From right to left scan the expression to find ^ operator
   This is done from ecx-1 to ecx=0

    _loop:      sub     ecx, 1
                cmp     byte ptr [esi + ecx],'^'
                je      short _found1
                ;
                cmp     ecx, 0
                jne     short _loop
                ;
                ; not found / done
                ; »»»»»»»»»»»»»»»»
                clc
                exit
                ;         
    _found1:    cmp     ecx, 0
                je      short goto error
                ;
                ; found -> try to find Base and Exponent
                ; »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
                If error goto error; If overflow goto overflow

                Calculate Power=Base^Exponent   (do not destroy ESI or ECX)
                If overflow goto overflow
                (the result is in SignZ and OperandZ )
               
                Save the result (in _TblPower)  (do not destroy ESI or ECX)
                Replace expresssion by #        (do not destroy ESI or ECX)
                jump to _loop

b) For each Power, save the result in an array _TblPower (from top to bottom)
    and replace the power expression by #

c) When ecx=0 than exit with clc

    When error or overflow than exit with stc
    If error set the string "ERROR" to _ResultZ
    If overflow set the string "OVERFLOW" to _ResultZ

;---***
Example:  expression = "4+5^3+2+5^2+2*3-5^1"

          the final expression is this:
          (note that when the sing is "-" we put this sign in SignZ)
         
          expression = "4+#+2+#+2*3+#" where _TblPower is this:

                                dd 3        <--- number of values in the table
                    _TblPower   dd 1       
                                dd 5
                                ;
                                dd 0        <--- sign 0= +    1= -1
                                dd 25
                                ;
                                dd 0
                                dd 125

    When we call SolveExpress to solve this last expression because
    SolveExpress solves it from left to right, the first # at the left
    is in the last in the _TblPower ... and the last in the first in _TblPower.
   
    When SolveExpress finds the first # it replaces it by (0,125) and
    _TblPower is now equal to this
     
                                dd 2 <--- number of values in the table
                    _TblPower   dd 1       
                                dd 5
                                ;
                                dd 0        <--- sign 0= +    1= -1
                                dd 25
                                ;
                                dd 0
                                dd 125

    When SolveExpress finds the second # it replaces it by (0,25) and
    _TblPower is now equal to this
     
                                dd 1 <--- number of values in the table
                    _TblPower   dd 1       
                                dd 5
                                ;
                                dd 0        <--- sign 0= +    1= -1
                                dd 25
                                ;
                                dd 0
                                dd 125

    When SolveExpress finds the third # it replaces it by (1,5) and
    _TblPower is now equal to this
     
                                dd 0 <--- number of values in the table
                    _TblPower   dd 1       
                                dd 5
                                ;
                                dd 0        <--- sign 0= +    1= -1
                                dd 25
                                ;
                                dd 0
                                dd 125

    If it tries to find onother #, there is an error because the
    number of values in the table is 0

    After we invoke LookPower, we invoke SolveExpress and SolveExpress
    solves the expression = "4+#+2+#+2*3+#" and replaces it by @ in
    the original expression 

    Note that LookPower is called inside LookExpress whenever it
    finds an expression with brackets.

RuiLoureiro

The LookPower algorithm (with some details)
-------------------------------------------

    _start:     sub     ecx, 1
                cmp     byte ptr [esi + ecx],'^'
                je      short _found1
                ;
    _loop:      cmp     ecx, 0
                jne     short _start
                ;
                ; not found / done
                ; »»»»»»»»»»»»»»»»
                clc
                exit
                ;         
    _found1:    cmp     ecx, 0
                je      short error
                ;
                ; found -> try to find Base and Exponent
                ; »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
                If error goto error; If overflow goto overflow

                Call    Exponent    (do not destroy ESI or ECX)
                Call    GetBase     (define the leftmost ECX)
               
                Calculate Power=Base^Exponent   (do not destroy ESI or ECX)
                If overflow goto overflow
                (the result is in SignZ and OperandZ )
               
                Save the result (in _TblPower)  (do not destroy ESI or ECX)
                Replace expresssion by #        (do not destroy ESI or ECX)
                ;
                jump to _loop       ; goto test ECX

RuiLoureiro

Hi
    My algorithm seems to be finished.
    Now, Calcula18 solves expressions like

                 2*3+(7-2*3+2^3-2^2)^3-10 (the base is an expression with powers)

RuiLoureiro

The algorithm to calculate an expression with Sub(-), Add(+), Mul (*), Div(/),
Remainder(\) and Powers (^) is finished.

I decided to show all Calcula19 project

Calcula19.zip   is = .asm.inc, .glb  & .exe
You can assemble it: Use Console Assemble & Link

Express your opinion or comments if you want
or tell me bugs if exist

Farabi

Those who had universe knowledges can control the world by a micro processor.
http://www.wix.com/farabio/firstpage

"Etos siperi elegi"

TmX

Hi RuiLoureiro,

I found a simple bug:
QuoteType an expression and press enter [ ex: -2 + 3*(7 - 2*8) + 3^3 ]
2^(3-3)
This is your expression: 2^(3-3)
This is your expression: 2^(3-3)
This is your Result:  ERROR
2^(3-3)=ERROR

It should be 1, right?

dedndave

20 = 1

but, how do you evaluate 00 ?   :P

oex

Quote from: dedndave on December 30, 2010, 03:52:59 AM
20 = 1

but, how do you evaluate 00 ?   :P

.if Pow=0
mov eax, 1
.endif
We are all of us insane, just to varying degrees and intelligently balanced through networking

http://www.hereford.tv

dedndave

many mathematicians would not agree
some say 0, some say 1, and i think most say it is undefined

TmX

Quote from: dedndave on December 30, 2010, 04:15:25 AM
many mathematicians would not agree
some say 0, some say 1, and i think most say it is undefined

0/0=0, because 0 = 0 x 0 ... (1)
0/0=1, because 0 = 0 x 1 ... (2)

by (1) and (2), we can conclude that 1 = 0  :dazzled: