views:

470

answers:

5

Duplicate

Best algorithm for evaluating a mathematical expression?

Is there a built-in Delphi function which would convert a string such as '2*x+power(x,2)' or any equation to float? StrToFloat raises an exception because of the char X and power.

Thanks.

+1  A: 

No, it's impossible except of parsing string. And how can you convert unknown number x to float?

x2
+2  A: 

You are looking for something that can evaluate an expression.

Since Delphi is a compiled language, it does not have built-in support for that.

However, there are external tools that can help you with that.

For instance: the Free Pascal Scripting engine from RemObjects can do what you want.

--jeroen

Jeroen Pluimers
+1  A: 

Bestcode.com : TbcParser - the Math Parser for Delphi and C++ Builder version 3.5

Delphi
+5  A: 

The free JCL includes TEvaluator, a parser written by one of the current Delphi compiler engineers. It will likely be far more efficient than an expression evaluator based on Windows Script Host.

Craig Stuntz
+1  A: 

In our SMImport suite we wrote the own expression parser/evaluator which is based on original TFatExpression component by Gasper Kozak, [email protected]

Works very good.