fpexprpars Invalid FLT/invalid floating point operation lazarus / 21-General ---------------------------------------------------------------------- BubikolRamios · 2025-01-29 14:16 if I paste string here https://www.wolframalpha.com/input?i2d=true&i=D ivide%5B%5C%2840%29-%5C%2840%292*%5C%2840%29-1.04196816208394*0--1.041 96816208394*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%29P ower%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%284 0%29%5C%2840%29- Power%5B1.04196816208394%2C2%5D%2B1%5C%2841%29%5C%2841%29 it calculates -5.02..... Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  fpexprpars, fpexprpars_addon;   var  FParser: TFPExpressionParser;  parserResult: TFPExpressionResult;  Parser: TFpExpressionParser;  mathExpression:AnsiString;begin   Parser := TFpExpressionParser.Create(self);   Parser.Builtins := [bcMath];    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))';   parserResult := Parser.Evaluate;  end; ---------------------------------------------------------------------- BubikolRamios · 2025-01-29 15:07 it appears, rounding all those many digits numbers with RoundTo(x,4), solves problem. ---------------------------------------------------------------------- wp · 2025-01-29 16:21 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses  fpexprpars;var  parser: TFPExpressionParser;  res: TFPExpressionResult;begin  parser := TFPExpressionParser.Create(nil);  try    parser.BuiltIns := [bcMath];    Parser.Expression := '(-(2*(-1.04196816208394*0-- 1.04196816208394*0-0))+sqrt(-((2*(-1.04196816208394*0--1.0419681620839 4*0-0))^2-4*((-1.04196816208394^2+1))*(0^2-200^2+0^2-200+0^2))))/2*((- 1.04196816208394^2+1))';    //                                                             added here  ||                                                                                                    |  res := parser.Evaluate;    WriteLn(res.ResFloat);         // -5.0299816639126842E+000  finally    parser.Free;  end;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 · 2025-02-16 12:15 Quote from: wp on January 29, 2025, 05:21:44 pmThe 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 · 2025-02-16 12:39 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses  Math;var  x: Double;begin  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));  WriteLn(x);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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;uses  Math;var  x: Double;begin  // The expression under the sqrt only  x := (2*(-1.04196816208394*0--1.04196816208394*0-0))**2- 4*((-1.04196816208394**2+1))*(0**2-200**2+0**2-200+0**2);  WriteLn(x);end. ---------------------------------------------------------------------- ASerge · 2025-02-16 14:20 Quote from: wp on February 16, 2025, 01:39:32 pmWhen 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.