A computational problem lazarus / 21-General ---------------------------------------------------------------------- lazpas · 2022-10-02 01:26 44950-7693*4.77=? The code looks like this: 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 ......parser := TFPExpressionParser.Create(nil);  try    parser.BuiltIns := [bcMath];    parser.Expression := '44950-7693*4.77';    parserResult := parser.Evaluate;    Caption:= FloatToStr(parserResult.ResFloat);  finally    parser.Free;  end;  The result is:8254.39000000001 The correct result is:8254.39 Is there a bug in my code? Thanks ---------------------------------------------------------------------- Bogen85 · 2022-10-02 02:20 Quote from: lazpas on October 02, 2022, 03:26:33 am44950-7693*4.77=? ... The result is:8254.39000000001 The correct result is:8254.39 Is there a bug in my code? Thanks No. Your code is fine. That is normal for binary to decimal conversion. You can't get an exact representation of the floating point number when you convert it to decimal due to base 10 not being a power of 2. You need to display it with less precision then trim trailing zeros to get something that is more reasonable. ---------------------------------------------------------------------- MarkMLl · 2022-10-02 07:04 Or rescale the numbers before the calculation and handle them as integers. 64 bits should be enough to handle most financial calculations for at least a few more months :-/ MarkMLl ---------------------------------------------------------------------- jcmontherock · 2022-10-02 07:19 What about parenthesis in your expression ? ---------------------------------------------------------------------- Bogen85 · 2022-10-02 07:40 Quote from: jcmontherock on October 02, 2022, 09:19:19 amWhat about parenthesis in your expression ? The result was off by 0.0000000001 according to @lazpas. That is a normal floating point to decimal conversion issue. Adding parenthesis around the 7693*4.77 is not going to make floating point to decimal conversion issues magically disappear.  This problem is not limited to Pascal, specifically Free Pascal in this example. You would get the same results with C/C++/python/etc if you don't limit the precision on the displayed output. ---------------------------------------------------------------------- Bogen85 · 2022-10-02 08:16 Quote from: lazpas on October 02, 2022, 03:26:33 am44950-7693*4.77=? ... The result is:8254.39000000001 The correct result is:8254.39 Is there a bug in my code? Or just use sysutils.format. It will correct the display issues for you. https://www.freepascal.org/docs-html/rtl/sysutils/format.html 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 {$mode delphi} program float_precision; uses  fpexprpars,  sysutils;   procedure primary (const parser: TFPExpressionParser);     procedure do_sample (const tag, fmt, expression: string);      begin        parser.expression := expression;        writeln (tag, ': ', expression, ' = ', format (fmt, [parser.evaluate.resFloat]));      end;     begin       do_sample ('1', '%f', '44950-7693*4.77');      do_sample ('2', '%f', '(44950-7693)*4.77');      do_sample ('3', '%f', '44950-(7693*4.77)');      do_sample ('4', '%g', '44950-7693*4.77');    do_sample ('5', '%e', '44950-7693*4.77');       parser.free;    end;   function newParser: TFPExpressionParser;    begin      result := TFPExpressionParser.create (nil);      result.builtIns := [bcMath];    end; begin  primary (newParser);end. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc lazforum/60783/float_precision.pas && ./lazforum/60783/float_precision Free Pascal Compiler version 3.3.1 [2022/10/01] for x86_64Copyright (c) 1993-2022 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling lazforum/60783/float_precision.pasLinking lazforum/60783/float_precision36 lines compiled, 0.4 sec, 658752 bytes code, 361144 bytes data1: 44950-7693*4.77 = 8254.392: (44950-7693)*4.77 = 177715.893: 44950-(7693*4.77) = 8254.394: 44950-7693*4.77 = 8254.39000000000675: 44950-7693*4.77 = 8.2543900000000067E+003 Results from 1 or 3 are what you are looking for. ---------------------------------------------------------------------- jamie · 2022-10-02 13:26 Currency type WriteStr(.... with Format speciifiers) Fixed Point math unit etc. ---------------------------------------------------------------------- J-G · 2022-10-02 16:51 I've taken to using my own two procs. to circumvent this issue. In case it may be of interest : 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 Var  TmpStr :    String; procedure StripZero(Var S : String;NoTrail : Boolean);begin  while S[length(S)] = '0' do    begin      S := copy(S,1,Pred(Length(S)));    end;  if S[length(S)] ='.' then    begin      if NoTrail then        S := copy(S,1,Pred(Length(S)))      else        S := S+'0';    end;  if Length(S) = 0 then    S := '0';end; procedure MakeStr(Val : single;Trail : boolean;DP : byte);begin  Str(Val:4:DP,TmpStr);  StripZero(TmpStr,trail);end; ---------------------------------------------------------------------- jamie · 2022-10-02 18:14 Quote from: J-G on October 02, 2022, 06:51:24 pmI've taken to using my own two procs. to circumvent this issue. In case it may be of interest : 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 Var  TmpStr :    String; procedure StripZero(Var S : String;NoTrail : Boolean);begin  while S[length(S)] = '0' do    begin      S := copy(S,1,Pred(Length(S)));    end;  if S[length(S)] ='.' then    begin      if NoTrail then        S := copy(S,1,Pred(Length(S)))      else        S := S+'0';    end;  if Length(S) = 0 then    S := '0';end; procedure MakeStr(Val : single;Trail : boolean;DP : byte);begin  Str(Val:4:DP,TmpStr);  StripZero(TmpStr,trail);end;  Ah, you mean this 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 RemoveTrailingChars(S,['0','.']);   and this 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 procedure TForm1.Button1Click(Sender: TObject);Var  S:String;begin   WriteStr(S,8254.000000001:0:2);   RemoveTrailingChars(S,['0','.']);   Caption := S;end;                                   Etc ---------------------------------------------------------------------- jamie · 2022-10-02 18:28 To add a little bit to it 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 Function RemoveTrailingCharsF(S:String; C:TSysCharSet;EmptyStr:String='0'):String;Begin   RemoveTrailingChars(S,C);   If S ='' then S :=EmptyStr;   Result := S;end; procedure TForm1.Button1Click(Sender: TObject);Var  S:String;begin   WriteStr(S,8254.000000001:0:2);   Caption := RemoveTrailingCharsF(S,['0','.']);end; ---------------------------------------------------------------------- Arioch · 2022-10-02 18:55 Quote from: jamie on October 02, 2022, 08:28:22 pm   If S ='' then S :=EmptyStr; end; but why??? with short strings this is  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 if s[0] = #0 then s[0] := #0; with long strings this should be equal to  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 if s = nil then s := nil; in both cases nothing was gained... ---------------------------------------------------------------------- jamie · 2022-10-02 19:22 now just think about that for a moment. Why limit the function? ---------------------------------------------------------------------- Arioch · 2022-10-02 19:35 Ouch... 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 EmptyStr:String='0'): EmptyStr is parameter here.... There is RTL constant with same name AFAIR ---------------------------------------------------------------------- jamie · 2022-10-02 20:08 What if I wanted the Empty Value to be something other than a single digit char ? what if it's a UTF8 for special symbols etc.? ---------------------------------------------------------------------- lazpas · 2022-10-26 08:16 Thank you all for your reply. ---------------------------------------------------------------------- lazpas · 2022-10-26 08:24 Quote from: Bogen85 on October 02, 2022, 10:16:10 amQuote from: lazpas on October 02, 2022, 03:26:33 am44950-7693*4.77=? ... The result is:8254.39000000001 The correct result is:8254.39 Is there a bug in my code? Or just use sysutils.format. It will correct the display issues for you. https://www.freepascal.org/docs-html/rtl/sysutils/format.html 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 {$mode delphi} program float_precision; uses  fpexprpars,  sysutils;   procedure primary (const parser: TFPExpressionParser);     procedure do_sample (const tag, fmt, expression: string);      begin        parser.expression := expression;        writeln (tag, ': ', expression, ' = ', format (fmt, [parser.evaluate.resFloat]));      end;     begin       do_sample ('1', '%f', '44950-7693*4.77');      do_sample ('2', '%f', '(44950-7693)*4.77');      do_sample ('3', '%f', '44950-(7693*4.77)');      do_sample ('4', '%g', '44950-7693*4.77');    do_sample ('5', '%e', '44950-7693*4.77');       parser.free;    end;   function newParser: TFPExpressionParser;    begin      result := TFPExpressionParser.create (nil);      result.builtIns := [bcMath];    end; begin  primary (newParser);end. Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i $ fpc lazforum/60783/float_precision.pas && ./lazforum/60783/float_precision Free Pascal Compiler version 3.3.1 [2022/10/01] for x86_64Copyright (c) 1993-2022 by Florian Klaempfl and othersTarget OS: Linux for x86-64Compiling lazforum/60783/float_precision.pasLinking lazforum/60783/float_precision36 lines compiled, 0.4 sec, 658752 bytes code, 361144 bytes data1: 44950-7693*4.77 = 8254.392: (44950-7693)*4.77 = 177715.893: 44950-(7693*4.77) = 8254.394: 44950-7693*4.77 = 8254.39000000000675: 44950-7693*4.77 = 8.2543900000000067E+003 Results from 1 or 3 are what you are looking for. Hi,Bogen85.Thanks for help. I'm writing a calculator. The number of decimal places is indeterminate. ---------------------------------------------------------------------- lazpas · 2022-10-26 08:27 Quote from: J-G on October 02, 2022, 06:51:24 pmI've taken to using my own two procs. to circumvent this issue. In case it may be of interest : 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 Var  TmpStr :    String; procedure StripZero(Var S : String;NoTrail : Boolean);begin  while S[length(S)] = '0' do    begin      S := copy(S,1,Pred(Length(S)));    end;  if S[length(S)] ='.' then    begin      if NoTrail then        S := copy(S,1,Pred(Length(S)))      else        S := S+'0';    end;  if Length(S) = 0 then    S := '0';end; procedure MakeStr(Val : single;Trail : boolean;DP : byte);begin  Str(Val:4:DP,TmpStr);  StripZero(TmpStr,trail);end; Thanks for sharing these codes. I will learn it.