Recent

Author Topic: TryStrToFloat  (Read 1328 times)

silvercoder70

  • New Member
  • *
  • Posts: 48
    • Tim Coates
Re: TryStrToFloat
« Reply #15 on: September 13, 2024, 12:06:13 am »
And you cannot really compare floating numbers with equality either...
P Plate on FPC | YouTube - https://www.youtube.com/@silvercoder70

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 299
Re: TryStrToFloat
« Reply #16 on: September 13, 2024, 02:03:48 am »
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][+][-]
  1. var
  2.   fs: TFormatSettings;
  3.  
  4. begin
  5.   fs := FormatSettings;
  6.   fs.DecimalSeparator := '.';
  7.  
  8.   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then
  9.   begin
  10.     //showmessage('exit');
  11.     Exit;
  12.   end;
  13.  
  14.   showmessage('end');
  15. end;  
  16.  
lazarus 3.2-fpc-3.2.2-win32/win64

dseligo

  • Hero Member
  • *****
  • Posts: 1365
Re: TryStrToFloat
« Reply #17 on: September 13, 2024, 07:43:57 am »
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][+][-]
  1. var
  2.   fs: TFormatSettings;
  3.  
  4. begin
  5.   fs := FormatSettings;
  6.   fs.DecimalSeparator := '.';
  7.  
  8.   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then
  9.   begin
  10.     //showmessage('exit');
  11.     Exit;
  12.   end;
  13.  
  14.   showmessage('end');
  15. end;  
  16.  

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?

Khrys

  • Jr. Member
  • **
  • Posts: 91
Re: TryStrToFloat
« Reply #18 on: September 13, 2024, 07:58:46 am »
And 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

  • Hero Member
  • *****
  • Posts: 15646
  • Censorship about opinions does not belong here.
Re: TryStrToFloat
« Reply #19 on: September 13, 2024, 11:23:56 am »
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.
« Last Edit: September 13, 2024, 11:29:23 am by Thaddy »
If I smell bad code it usually is bad code and that includes my own code.

TRon

  • Hero Member
  • *****
  • Posts: 3236
Re: TryStrToFloat
« Reply #20 on: September 13, 2024, 04:30:47 pm »
And you cannot really compare floating numbers with equality either...
I completely overlooked that... thank you for the addition/correction.
All software is open source (as long as you can read assembler)

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 299
Re: TryStrToFloat
« Reply #21 on: September 13, 2024, 08:56:18 pm »
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][+][-]
  1. var
  2.   fs: TFormatSettings;
  3.  
  4. begin
  5.   fs := FormatSettings;
  6.   fs.DecimalSeparator := '.';
  7.  
  8.   if StrToFloatDef('dfsdf',-100000, fs) = -100000 then
  9.   begin
  10.     //showmessage('exit');
  11.     Exit;
  12.   end;
  13.  
  14.   showmessage('end');
  15. end;  
  16.  

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.
lazarus 3.2-fpc-3.2.2-win32/win64

 

TinyPortal © 2005-2018