MathExpression! Help


Contents:
  • About MathExpression!
  • Implemented functions
  • API
  • Feedback


    About MathExpression!

    MathExpression! it's a free, extandable, easy to use run-time mathematical expression evaluator.
    It solves expressions like '(5^3-2.7)*2 + cos(5)^2 - (ln(e))^10' (usefull in building graphs).



    Implemented functions

    Operations:
    +, -, *, /
    ^, ** - power
    ! - factorial
    (, ) - brackets


    Functions:
    sin, cos, tan, ctg
    abs
    sqrt
    ln


    Constants:
    pi, e


    API

    function CalcExpression(Value: String; var Res: String): Word;
    Evaluates Value, result is stored in Res (use StrToFloat function from SysUtils unit to convert string value into it's float representation). Returns C_NOERROR if there is no error, otherwise it will return one of error codes.
    Example:
      procedure TForm1.EvaluateClick(Sender: TObject);
      var
        ErrorCode: Word;
        Res: String;
      begin
        Memo1.Lines.Clear;
        ErrorCode := CalcExpression('1+2', Res);
        if ErrorCode <> C_NOERROR then
        begin
          Memo1.Lines.Add('Cannot evaluate expression: ' + CalcErrorToStr(ErrorCode));
          Exit;
        end else
          Memo1.Lines.Add('Result: ' + Res)
      end;
    
      Memo1 should contain 'Result: 3'
      
    function CalcGraphExpression(Value: String; X: Extended; var Res: String): Word;
    Works as CalcExpression but replaces x in Value with extended variable X.
    Example:
      procedure TForm1.EvaluateClick(Sender: TObject);
      var
        ErrorCode: Word;
        Res: String;
      begin
        Memo1.Lines.Clear;
        ErrorCode := CalcExpression('sin(pi/2)', Res);
        if ErrorCode <> C_NOERROR then
        begin
          Memo1.Lines.Add('Cannot evaluate expression: ' + CalcErrorToStr(ErrorCode));
          Exit;
        end else
          Memo1.Lines.Add('Result: ' + Res)
      end;  
    
      Memo1 should contain 'Result: 1'
      
    function CalcErrorToStr(Error: Word): String;
    Converts error code returned by CalcExpression and CalcGraphExpression to it's string representation.


    Feedback

    If you've got some questions/bugreports/etc, I'll answer them by mail alex@ritlabs.com or ICQ: #4024781.
    For latest versions checkout http://www.cobans.net


    MathExpression! 1.0
    (C) Alex Demchenko, 2002, Moldova, Chishinev.