Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to evaluate arithmetic expressions in Skill

Status
Not open for further replies.

micpro

Member level 2
Member level 2
Joined
May 8, 2009
Messages
45
Helped
7
Reputation
14
Reaction score
7
Trophy points
1,288
Activity points
1,615
I'm looking for a solution to evaluate mathematical arithmetic expressions in Skill.

Due to the mix of infix and postfix notations in Skill, evalstring doesn't always work. For example, "(3)*(4)" is a valid arithmetical expression, but evalstring can't evaluate it. Same for "2*(cos(17))".

Or consider these examples:

> evalstring("(4 +5)*3")
> 27
> evalstring("(4 -5)*3")
*Error* eval: not a function - 4

> evalstring("+4*3")

+4*3
^
SYNTAX ERROR found at line 1 column 2 of file *string*
*Error* lineread/read: syntax error encountered in input
> evalstring("4*(-5)")
-20
> evalstring("4*(+5)")

4*(+5)
^
SYNTAX ERROR found at line 1 column 5 of file *string*
*Error* lineread/read: syntax error encountered in input

and so on ...
Any ideas?
 

Hai micpro,

Here is a solution for u..

case 1:-
evalstring("(4 -5)*3")

u can execute like this.. It will work

evalstring("plus(4 -5)*3")

Case 2:-
evalstring("+4*3")

This will work

evalstring("minus(-4)*3")

Case 3:-

evalstring("4*(+5)")

use like this

evalstring("4*minus(-5)")

Hope u will be happy:razz:
 

Hi vijay,

thank you for your response. I should have mentioned a detail in my post: the string to be evaluated is coming from an external program and my program does not know it in advance. My program is evaluating it.
Assume the string is inside a variable t_input. If my program simply calls

result=evalstring(t_input)

then it will fail for the cases that I mentioned in my post. How can I calculate the result of an arithmetic expression given inside a string-valued variable, the content of which I don't know when writing the software?

Regards,
Michael
 

Then it is too easy micpro compare to the past. u will not know only the value of a variable..,, but u know whether its string or numerical.. If u are sure that every input will be string then


mention like this


but dont forget it should be mentioned only for sting.. For numerical value dont use evalstring

case 1:-

(evalstring("4") + evalstring("-5")) * evalstring("3")

case 2:-

(evalstring("4") * evalstring("3"))

case 3:-

(evalstring("-4") * evalstring("3"))

use evalstring separately u can achieve what u want..

optional case:-
if u are not sure that its a numerical variable or string variable then just

store it in a string variable if its a numerical variable or string variable and then pass it to above evalstring statement..
 
Last edited:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top