Recent

Author Topic: fpexprpars Invalid FLT/invalid floating point operation  (Read 548 times)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 350
fpexprpars Invalid FLT/invalid floating point operation
« on: January 29, 2025, 03:16:47 pm »
if I paste string here https://www.wolframalpha.com/input?i2d=true&i=Divide%5B%5C%2840%29-%5C%2840%292*%5C%2840%29-1.04196816208394*0--1.04196816208394*0-0%5C%2841%29%5C%2841%29%2Bsqrt%5C%2840%29Power%5B%5C%2840%292*%5C%2840%29-1.04196816208394*0--1.04196816208394*0-0%5C%2841%29%5C%2841%29%2C2%5D-4*%5C%2840%29%5C%2840%29-Power%5B1.04196816208394%2C2%5D%2B1%5C%2841%29%5C%2841%29*%5C%2840%29Power%5B0%2C2%5D-Power%5B200%2C2%5D%2BPower%5B0%2C2%5D-200%2BPower%5B0%2C2%5D%5C%2841%29%5C%2841%29%5C%2841%29%2C2%5D*%5C%2840%29%5C%2840%29-Power%5B1.04196816208394%2C2%5D%2B1%5C%2841%29%5C%2841%29 it calculates -5.02.....


Code: Pascal  [Select][+][-]
  1. uses
  2.   fpexprpars, fpexprpars_addon;  
  3.  
  4. var
  5.   FParser: TFPExpressionParser;
  6.   parserResult: TFPExpressionResult;
  7.   Parser: TFpExpressionParser;
  8.   mathExpression:AnsiString;
  9. begin
  10.    Parser := TFpExpressionParser.Create(self);
  11.    Parser.Builtins := [bcMath];
  12.  
  13.    Parser.Expression := '(-(2*(-1.04196816208394*0--1.04196816208394*0-0))+sqrt((2*(-1.04196816208394*0--1.04196816208394*0-0))^2-4*((-1.04196816208394^2+1))*(0^2-200^2+0^2-200+0^2)))/2*((-1.04196816208394^2+1))';
  14.    parserResult := Parser.Evaluate;
  15.  
  16.  
  17. end;
  18.  
  19.  
« Last Edit: January 29, 2025, 03:19:02 pm by BubikolRamios »
lazarus 3.2-fpc-3.2.2-win32/win64

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 350
Re: fpexprpars Invalid FLT/invalid floating point operation
« Reply #1 on: January 29, 2025, 04:07:37 pm »
it appears, rounding all those many digits numbers with RoundTo(x,4), solves problem.
lazarus 3.2-fpc-3.2.2-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 12683
Re: fpexprpars Invalid FLT/invalid floating point operation
« Reply #2 on: January 29, 2025, 05:21:44 pm »
Rounding without knowing what you do will never result in a correct value...

The real problem with this expression is that the argument of the sqrt function is negative. If you remember your math lessons, it is not possible to calculate the sqrt of a negative value. Mathematicians therefore introduced the so-called "imaginary numbers" to be able to do this: the sqrt(-1) is i ("imaginary unit"), and if you look at the Wolfram Alpha result you'll see the result is -5.03 i.

FPExpressionParser works only on real number, imaginary numbers are not supported.

But if you look at the expression before the sqrt you'll see that this part is zero, and the expression reduces to something like sqrt(-a)/b. Knowing that sqrt(-1) = i you still can calculate the value if you negate the expression under the sqrt: sqrt(-a) / b = sqrt(a) / b i.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   fpexprpars;
  4. var
  5.   parser: TFPExpressionParser;
  6.   res: TFPExpressionResult;
  7. begin
  8.   parser := TFPExpressionParser.Create(nil);
  9.   try
  10.     parser.BuiltIns := [bcMath];
  11.     Parser.Expression := '(-(2*(-1.04196816208394*0--1.04196816208394*0-0))+sqrt(-((2*(-1.04196816208394*0--1.04196816208394*0-0))^2-4*((-1.04196816208394^2+1))*(0^2-200^2+0^2-200+0^2))))/2*((-1.04196816208394^2+1))';
  12.     //                                                               added here  ||                                                                                                      |
  13.     res := parser.Evaluate;
  14.     WriteLn(res.ResFloat);         // -5.0299816639126842E+000
  15.   finally
  16.     parser.Free;
  17.   end;
  18. end.

 Taking the output and appending the complex unit i we get -5.0299816639126842 i, and Wolfram Alpha reports -5.029981663913... i - the same.

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: fpexprpars Invalid FLT/invalid floating point operation
« Reply #3 on: February 16, 2025, 01:15:12 pm »
The real problem with this expression is that the argument of the sqrt function is negative.
Sorry if I didn't understand something, but the expression is calculated as -5.02998166391268. The same goes for the link above. And sqrt is calculated from a positive value.

wp

  • Hero Member
  • *****
  • Posts: 12683
Re: fpexprpars Invalid FLT/invalid floating point operation
« Reply #4 on: February 16, 2025, 01:39:32 pm »
When I paste the user's expression into a small console application and replace the '^' by '**' I get a FLT INVALID OPERATION error.

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   Math;
  4. var
  5.   x: Double;
  6. begin
  7.   x := (-(2*(-1.04196816208394*0--1.04196816208394*0-0))+sqrt((2*(-1.04196816208394*0--1.04196816208394*0-0))**2-4*((-1.04196816208394**2+1))*(0**2-200**2+0**2-200+0**2)))/2*((-1.04196816208394**2+1));
  8.   WriteLn(x);
  9. end.  
And when I paste only the part under the sqrt I get a negative value. Therefore the user's expression is trying the calculate the sqrt of a negative number.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses
  3.   Math;
  4. var
  5.   x: Double;
  6. begin
  7.   // The expression under the sqrt only
  8.   x := (2*(-1.04196816208394*0--1.04196816208394*0-0))**2-4*((-1.04196816208394**2+1))*(0**2-200**2+0**2-200+0**2);
  9.   WriteLn(x);
  10. end.

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: fpexprpars Invalid FLT/invalid floating point operation
« Reply #5 on: February 16, 2025, 03:20:17 pm »
When I paste the user's expression into a small console application and replace the '^' by '**' I get a FLT INVALID OPERATION error.
You're right. It's strange how I did it the first time without an error.

 

TinyPortal © 2005-2018