* * *

Author Topic: fpExpressionParser  (Read 1162 times)

wp

  • Hero Member
  • *****
  • Posts: 558
fpExpressionParser
« on: September 20, 2012, 12:36:39 am »
Two questions regarding fpExpressionParser:
  • Following the examples with the built-in functions I added a user-defined function "power" to the parser by calling "Identifiers.AddFunction('power', 'F', 'FF', @ExprPower)". The functions works fine as long as I use a floating point number for the exponent, i.e. "power(x, 2.0)" is ok, but with integer exponents (i.e. "power(x, 2)") I get the error that a float is expected. Is there a way to automatically convert integers to floats?
  • I'd like to add a new symbol "^" as a short-cut to the power function. Is this possible?

Lazarus trunk / fpc 2.6.2 / Win32

Leonor

  • New member
  • *
  • Posts: 16
Re: fpExpressionParser
« Reply #1 on: September 20, 2012, 01:19:02 am »
Hello wp, I don't know if there is a way to convert automatically  integers to floats, but I do this way:

procedure integersToFloat(iValue: integer; var DblResult: Double);
     var
        VrnValue: Variant;
begin
     VrnValue := iValue;
     DblResult :=  VrnValue;
end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 4296
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: fpExpressionParser
« Reply #2 on: September 20, 2012, 05:45:55 am »
Quote
I'd like to add a new symbol "^" as a short-cut to the power function. Is this possible?
Looking at the source code, it seems impossible. The operators are hardcoded and no user defined operator handling provided.

wp

  • Hero Member
  • *****
  • Posts: 558
Re: fpExpressionParser
« Reply #3 on: September 20, 2012, 10:53:17 pm »
@Leonor: This is a stand-alone procedure (BTW: wouldn't a simple assignment "floatVariable := integerVariable" have the same effect?). I am seeking a way how to tell fpExpressionParser to do such a conversion internally.

It looks as if fpExpressionParser is not prepared for this feature. So it seems that I'll have to patch the sources.
Lazarus trunk / fpc 2.6.2 / Win32

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1677
Re: fpExpressionParser
« Reply #4 on: September 21, 2012, 09:21:48 am »
Or use the "symbolic" expression parser.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads