Recent

Author Topic: [SOLVED] Weird behaviour of "in [set]" test at run time  (Read 440 times)

Davo

  • Full Member
  • ***
  • Posts: 141
[SOLVED] Weird behaviour of "in [set]" test at run time
« on: July 01, 2026, 03:16:08 pm »
First time I have encountered this. Any suggestions as to why would be much appreciated.

Integer value testing such as "if testInteger in [6..12]" is not working properly at run time.

The relevant unit has the following extracts :

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  3.   ExtCtrls, Windows, Unit1, bidfirst;
  4.  
  5.     if supportedHCP < 9 then
  6.       {Chart 11}
  7.       begin
  8.         if Opening = OneH then
  9.           begin
  10.             memo1.lines.add('2 Hearts');
  11.             result := TwoH;
  12.           end
  13.         else begin
  14.                memo1.lines.add('2 Spades');
  15.                result := TwoS;
  16.              end;
  17.         if debug then
  18.           memo1.lines[memo1.lines.count - 1] :=
  19.           memo1.lines[memo1.lines.count - 1] + ' [Chart 11]';
  20.         memo1.lines.add(space + ': because I have more than three ' + openingStr);
  21.         memo1.lines.add(space + '  and ' + openingStr + ' was led by my partner');
  22.         memo1.lines.add(space + '  and my supported HCP is between 6 and 8.');
  23.         memo1.lines.add(' ');
  24.       end;
  25.     if supportedHCP in [9..12] then
  26.       {Chart 12};
  27.       begin
  28.         if Opening = OneH then
  29.           begin
  30.             memo1.lines.add('3 Hearts');
  31.             result := ThreeH;
  32.           end
  33.         else begin
  34.                memo1.lines.add('3 Spades');
  35.                result := ThreeS;
  36.              end;
  37.         if debug then
  38.           memo1.lines[memo1.lines.count - 1] :=
  39.           memo1.lines[memo1.lines.count - 1] + ' [Chart 12]';
  40.         memo1.lines.add(space + ': because I have more than three ' + openingStr);
  41.         memo1.lines.add(space + '  and ' + openingStr + ' was led by my partner');
  42.         memo1.lines.add(space + '  and my supported HCP is between');
  43.         memo1.lines.add(space + '      9 and 12.');
  44.         memo1.lines.add(' ');
  45.       end;]
  46.  
  47. Attached is a screenshot of the result of running that code. The supplied value of the variable supportedHCP was 8 as correctly displayed nearly halfway down the
  48. screenshot. The above code supplies the text in the lower part of the screenshot. But as you can see not only was the test "if supportedHCP < 9" correctly triggered
  49. but the test "if supportedHCP in [9..12]" was incorrectly triggered as well.
  50.  
  51. The problem is not confined to the code shown above. It applies generally in the unit used.
  52.  
  53.  
« Last Edit: July 01, 2026, 10:22:47 pm by Davo »

Zvoni

  • Hero Member
  • *****
  • Posts: 3441
Re: Weird behaviour of "in [set]" test at run time
« Reply #1 on: July 01, 2026, 03:26:16 pm »
Cannot reproduce
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3. Uses Classes, Sysutils;
  4. Const
  5.   i:Integer=10;
  6.   j:Integer=8;
  7. Var b:Boolean;
  8.  
  9. begin
  10.   b:=i in [9..12];
  11.   Writeln(i,' in [9..12] --> ',b);
  12.   b:=j in [9..12];
  13.   Writeln(j,' in [9..12] --> ',b);
  14.   Readln;
  15. end.                          

Returns
Code: [Select]
10 in [9..12] --> TRUE
8 in [9..12] --> FALSE

FWIW: There is also "InRange"
https://www.freepascal.org/docs-html/rtl/math/inrange.html
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Davo

  • Full Member
  • ***
  • Posts: 141
Re: Weird behaviour of "in [set]" test at run time
« Reply #2 on: July 01, 2026, 03:46:10 pm »
Thanks Zvoni. For what it is worth I tried using InRange but that also fails. It looks like the problem is not with the written code but lies somewhere else more generally.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12948
  • FPC developer.
Re: Weird behaviour of "in [set]" test at run time
« Reply #3 on: July 01, 2026, 03:53:57 pm »
Code: Pascal  [Select][+][-]
  1.     end;
  2.     if supportedHCP in [9..12] then
  3.       {Chart 12};     //   <---         this ; is the problem.
  4.       begin
  5.         if Ope....
  6.  
  7.  

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12540
  • Debugger - SynEdit - and more
    • wiki
Re: Weird behaviour of "in [set]" test at run time
« Reply #4 on: July 01, 2026, 03:56:06 pm »
Just a note: Is any of you numbers outside of 0..255 ?

The [42..124] notation is a "set", not an integer range. Sets are limited to 256 elements. If any integer is outside the range (either in the in the set, or the value tested) then the behaviour is undefined.

Zvoni

  • Hero Member
  • *****
  • Posts: 3441
Re: Weird behaviour of "in [set]" test at run time
« Reply #5 on: July 01, 2026, 04:15:11 pm »
Code: Pascal  [Select][+][-]
  1.     end;
  2.     if supportedHCP in [9..12] then
  3.       {Chart 12};     //   <---         this ; is the problem.
  4.       begin
  5.         if Ope....
  6.  
  7.  
Bingo!
Semicolon after the comment ends the If-Statement, with the following Begin/End being free for all
Missed it myself
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Paolo

  • Hero Member
  • *****
  • Posts: 746
Re: Weird behaviour of "in [set]" test at run time
« Reply #6 on: July 01, 2026, 08:31:38 pm »
Could make sense that an hint is emitted in case of "then ;" ?

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12540
  • Debugger - SynEdit - and more
    • wiki
Re: Weird behaviour of "in [set]" test at run time
« Reply #7 on: July 01, 2026, 08:45:15 pm »
Could make sense that an hint is emitted in case of "then ;" ?

Until it is (or isn't), there is a way to catch many of them (not all, and probably not the one you had here).

User defined markup https://wiki.freepascal.org/New_IDE_features_since#Multiple_user_defined_word_highlight/markup https://wiki.freepascal.org/IDE_Window:_Editor_User_Defined_Words

I have lists defining "words" like "then;", "then ;", then  ;" (0 to 2 spaces) => and those get a real annoying color. => they don't catch multiline, nor if a comment is in the middle.

I also have semicolon after: else begin try do
As well as ";;" and ";)".
The ";)" can actually happen in const record declarations... But it is rare.

And you can add anything else that you may mistype occasionally, if it isn't valid code...

You could add "};" But not sure if that will get you false positives....


Davo

  • Full Member
  • ***
  • Posts: 141
Re: Weird behaviour of "in [set]" test at run time
« Reply #8 on: July 01, 2026, 10:21:56 pm »
Thank you marcov, Martin_fr, Zivoni and Paolo for the time and trouble that you have taken to reply. I'm really grateful.

Marcov you are spot on. Removing the offending semicolons clears the matter up. My stupidity. The semicolons were not intended but crept in by mistake. To make amends and admit that I should not be wasting your time with silly mistakes I will donate $50 to a local charity. You will have to trust me on that but you have my word.

Davo

  • Full Member
  • ***
  • Posts: 141
Re: [SOLVED] Weird behaviour of "in [set]" test at run time
« Reply #9 on: July 02, 2026, 06:27:24 am »
Receipt for donation attached. Salvos is the local familiar name for the Salvation Army.

cdbc

  • Hero Member
  • *****
  • Posts: 2869
    • http://www.cdbc.dk
Re: [SOLVED] Weird behaviour of "in [set]" test at run time
« Reply #10 on: July 02, 2026, 10:07:14 am »
Hi Davo
Good on you mate -- Nice =^
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018