TryStrToFloat lazarus / 21-General ---------------------------------------------------------------------- BubikolRamios · 2024-09-08 22:38 Hmm, no message. Note there is nothing after '.'. I assume it thinks strtofloat('0.') is 0. How to check then if string or decimal ? 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  fs: TFormatSettings;  x: Float;begin  x := NaN;  fs := FormatSettings;  fs.DecimalSeparator := '.';       if not TryStrToFloat('0.',x, fs)then  begin    showmessage('foo');  end; ---------------------------------------------------------------------- paweld · 2024-09-09 04:36 if the TryStrToFloat function returns TRUE, it means that the string was successfully converted to a number, and if FALSE, the conversion failed.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  fs: TFormatSettings;  x: Double;begin  fs.DecimalSeparator := '.';  if TryStrToFloat('0.', x, fs) then    ShowMessage('It''s float: ' + FloatToStr(x))  else    showmessage('It'' string');end; ---------------------------------------------------------------------- Thaddy · 2024-09-09 04:54 Should it not be '0.0';? I mean it is not called a separator for nothing: it expects two decimal values either side of the dot. ---------------------------------------------------------------------- BubikolRamios · 2024-09-09 06:03 Quote from: Thaddy on September 09, 2024, 06:54:07 amShould it not be '0.0';? I mean it is not called a separator for nothing: it expects two decimal values either side of the dot. Checking that OnKeyUp on TEdit control... Press backspace on something like '0.2' ---------------------------------------------------------------------- BubikolRamios · 2024-09-09 06:12 test: 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 showmessage(FloatToStr(StrToFloat('0.'))); //--> 0showmessage(FloatToStr(StrToFloat('01')));//--> 1           Not very logical. ---------------------------------------------------------------------- wp · 2024-09-09 07:20 Quote from: Thaddy on September 09, 2024, 06:54:07 amI mean it is not called a separator for nothing: it expects two decimal values either side of the dot. Even good-old Turbo Pascal detects '0.' as 0, and '.1' as 0.1 There's nothing wrong with these numbers (go to the U.S., or look at an Americal Youtube math-related video, and you'll see them all over the place). ---------------------------------------------------------------------- Zvoni · 2024-09-09 08:11 ?!?!?!? TryStrToFloat calls TextToFloat, which calls Val 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 Sysutils;Var  x:Double;  fs: TFormatSettings;begin  fs:=FormatSettings;  fs.DecimalSeparator:='.';  Writeln(TryStrToFloat('0.',x,fs),' = ',x);end. Returns Code: [Select]TRUE =  0.0000000000000000E+000 ---------------------------------------------------------------------- Warfley · 2024-09-09 08:15 The sysutil conversion functions all rely on the internal call to the val directive, which does a few interesting things. Basically it can parse everything that you can write into your pascal code as number constant, and even more: 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   val('$FF', i, err);  val('%10', i, err);  val('&43', i, err);  val('1.', d, err);  val('.1', d, err);  val('3e4', d, err); If you want only "normal" number notation, you should simply write your own function: 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 type  TNumberType = (ntNotANumber, ntInteger, ntFloat); function isNumber(const AString: String): TNumberType;var  c: Char;begin  if Length(AString) = 0 then    Exit(ntNotANumber);  Result := ntInteger;  for c in AString do    if (c='.') and (Result=ntInteger) then      Result:=ntFloat    else if not (c in ['0'..'9']) then      Exit(ntNotANumber);end; Which you can further customize to your liking (e.g. by adding a last if statement to rule out a . without followup) ---------------------------------------------------------------------- Bart · 2024-09-09 08:33 Quote from: Warfley on September 09, 2024, 10:15:22 amThe sysutil conversion functions all rely on the internal call to the val directive, which does a few interesting things. Basically it can parse everything that you can write into your pascal code as number constant, and even more: And it can even parse enums. Bart ---------------------------------------------------------------------- jcmontherock · 2024-09-09 15:25 "StrToFloatDef", "StrToIntDef" could also be used ---------------------------------------------------------------------- BubikolRamios · 2024-09-12 19:27 Quote from: jcmontherock on September 09, 2024, 05:25:30 pm"StrToFloatDef", "StrToIntDef" could also be used on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ? 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   fs := FormatSettings;  fs.DecimalSeparator := '.';    if StrToFloatDef('dfsdf',-100000, fs) = -100000 then  begin    Exit;  end; ---------------------------------------------------------------------- dseligo · 2024-09-12 19:48 Quote from: BubikolRamios on September 12, 2024, 09:27:52 pmQuote from: jcmontherock on September 09, 2024, 05:25:30 pm"StrToFloatDef", "StrToIntDef" could also be used on if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ? 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   fs := FormatSettings;  fs.DecimalSeparator := '.';    if StrToFloatDef('dfsdf',-100000, fs) = -100000 then  begin    Exit;  end;     I get 'exit' on my screen: 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 SysUtils; var  fs: TFormatSettings; begin  fs := FormatSettings;  fs.DecimalSeparator := '.';   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then  begin    WriteLn('exit');    Exit;  end;   WriteLn('end');end. ---------------------------------------------------------------------- TRon · 2024-09-12 20:19 Quote from: BubikolRamios on September 12, 2024, 09:27:52 pmon if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ? Which is exactly working as advertised: QuoteStrToFloatDef tries to convert the string S to a floating point value, and returns this value. If the conversion fails for some reason, the value Default is returned instead. ---------------------------------------------------------------------- dseligo · 2024-09-12 21:12 Quote from: TRon on September 12, 2024, 10:19:24 pmQuote from: BubikolRamios on September 12, 2024, 09:27:52 pmon if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ? Which is exactly working as advertised: QuoteStrToFloatDef tries to convert the string S to a floating point value, and returns this value. If the conversion fails for some reason, the value Default is returned instead. But he said it doesn't return default: Quoteon if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ? ---------------------------------------------------------------------- TRon · 2024-09-12 21:25 Quote from: dseligo on September 12, 2024, 11:12:59 pmBut he said it doesn't return default: That is not exactly 100% TS' words Quoteon if line it does nothing does not go paste if, does not go into exit, nothing. No errors or exceptions. ?Indeed: "it does not go into exit" and "it does nothing" (with the conclusion, no errors or exceptions). Last part is covered by the documentation as linked. The statement "it does nothing" can be analyzed as follows: the first line that gets executed when the condition is met is the line containing the exit statement. In the particular example of TS, the condition is true thus exit gets executed (as shown per your example as well). And indeed that does nothing (further) except for executing the exit statement (no idea what TS is expecting to happen). The statement "it does not go into exit" deserves further explanation from TS, because the context is important there. An exit statement only exits a function (and if that function happens to be the main function it will exit the program). With the code as shown by TS it is impossible for TS to conclude that the exit statement is not executed. ---------------------------------------------------------------------- silvercoder70 · 2024-09-12 22:06 And you cannot really compare floating numbers with equality either... ---------------------------------------------------------------------- BubikolRamios · 2024-09-13 00:03 Put breakpoint onto exit, do tell why it does not stop there ? It does work right, just does not stop on breakpoint. 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  fs: TFormatSettings; begin  fs := FormatSettings;  fs.DecimalSeparator := '.';   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then  begin    //showmessage('exit');    Exit;  end;   showmessage('end');end; ---------------------------------------------------------------------- dseligo · 2024-09-13 05:43 Quote from: BubikolRamios on September 13, 2024, 02:03:48 amPut breakpoint onto exit, do tell why it does not stop there ? It does work right, just does not stop on breakpoint. 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  fs: TFormatSettings; begin  fs := FormatSettings;  fs.DecimalSeparator := '.';   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then  begin    //showmessage('exit');    Exit;  end;   showmessage('end');end;   OK, that's a different thing. What optimisation level do you have in your project's options under Compilation and linking? Set it to '0 (no optimization, for debugging)'. What do you have checked in your project's options (see attachment)? How does the 'ball' in the line you've set breakpoint look like? Does it have checkmark inside? See 2nd attachment. What happens if you go step by step? ---------------------------------------------------------------------- H₂SO₄ · 2024-09-13 05:58 Quote from: silvercoder70 on September 13, 2024, 12:06:13 amAnd you cannot really compare floating numbers with equality either... In general it's a bad idea, yes. But in this case, where you're checking for a sentinel value that's guaranteed to not have been altered by any floating-point operations, it's perfectly fine. Here both instances of the  -100000  literal are converted to floats by the same compiler in close proximity; they must be bitwise equivalent (otherwise I'd file a compiler bug report). With that being said, I wouldn't trust comparisons between a literal value and i.e. a float converted from user input via some conversion function, since the conversion method isn't guaranteed to be the same. ---------------------------------------------------------------------- Thaddy · 2024-09-13 09:23 That is why we have SameValue, which has a configurable approximation. I agree with your reasoning, but getting floats exact is simply not possible using the floating point registers. https://www.freepascal.org/docs-html/rtl/math/samevalue.html Higher precision is only possible with arbitrary float libraries, which rely on big integers and still will have rounding errors at the edges. To summarize: you can get higher precision, but not absolute precision in all cases. The pitfalls of binary vs analog. ---------------------------------------------------------------------- TRon · 2024-09-13 14:30 Quote from: silvercoder70 on September 13, 2024, 12:06:13 amAnd you cannot really compare floating numbers with equality either... I completely overlooked that... thank you for the addition/correction. ---------------------------------------------------------------------- BubikolRamios · 2024-09-13 18:56 Quote from: dseligo on September 13, 2024, 07:43:57 amQuote from: BubikolRamios on September 13, 2024, 02:03:48 amPut breakpoint onto exit, do tell why it does not stop there ? It does work right, just does not stop on breakpoint. 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  fs: TFormatSettings; begin  fs := FormatSettings;  fs.DecimalSeparator := '.';   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then  begin    //showmessage('exit');    Exit;  end;   showmessage('end');end;   OK, that's a different thing. What optimisation level do you have in your project's options under Compilation and linking? Set it to '0 (no optimization, for debugging)'. What do you have checked in your project's options (see attachment)? How does the 'ball' in the line you've set breakpoint look like? Does it have checkmark inside? See 2nd attachment. What happens if you go step by step? Set it to '0 (no optimization, for debugging)'. It appears that was it. Thanks.