Recent

Author Topic: [SOLVED] Subrange question  (Read 3716 times)

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
[SOLVED] Subrange question
« on: September 02, 2012, 07:28:29 pm »
Why the second condition never happen, even if "Exp" is obviously =0 ?

Simple project with one button.
Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;

type
  TExponent = 1..30;
 
  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  Exp: TExponent;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
begin
  WriteLn('Exp is: '+inttostr(Exp));
  if Exp=0 then WriteLn('Exp is ZERO');
end;

end.       

Output is only "Exp is: 0" and that's all.
Probably it is optimalized ...
Thanks.

EDIT: Directive {R+} does not help either.
« Last Edit: September 02, 2012, 08:58:16 pm by Blaazen »
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Knipfty

  • Full Member
  • ***
  • Posts: 232
Re: Subrange question
« Reply #1 on: September 02, 2012, 08:08:04 pm »
Hi Blaazen,

Exp is never assigned a value.  Whatever is in it is random.  Exp can never be Zero, so my guess is the compiler is optimizing away the if statement since it can't possibly execute.  I duplicated your example with the following code in a FormShow event handler:

Code: [Select]
type TExp = 1..30;
var Exp: TExp;
begin
  if Exp=0 then showmessage('zero');

I verified that when the if statement is reached, the value is zero, but the showmessage is never executed.  Also, I tried to put Exp := 0 right before the IF statement, and the compiler generated an error as it should have.

Knipfty
64-bit Lazarus 2.2.0 FPC 3.2.2, 64-bit Win 11

xamsin

  • New member
  • *
  • Posts: 9
Re: Subrange question
« Reply #2 on: September 02, 2012, 08:43:27 pm »
so it's understud, it's happend because
you compare in directive if two different
types, integer type and user type;
so you can type
 if integer(Exp)=0 then writeln(....
or
 Type
   TExp:integer=1..30;
or change if direction to case direction
 case Exp of
  0: writeln(...);
 end;

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Subrange question
« Reply #3 on: September 02, 2012, 08:57:45 pm »
Thanks, retype to Integer solves my problem.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Re: [SOLVED] Subrange question
« Reply #4 on: September 05, 2012, 07:12:45 pm »
Just for the record.. Always check the warnings generated. For your original code it will generate even three warnings:

Warning: Variable "Exp" does not seem to be initialized
Warning: Comparison might be always false due to range of constant and expression
Warning: unreachable code

When you don't initialize a variable it'll simply be initialized to zero, even though that violates subrange bounds. It might be a good idea if the compiler treated this as an error in the future, if 0 isn't in the subrange..

 

TinyPortal © 2005-2018