rand unit (was: us_Timer in TimeStamp)_(re)

Tue, 28 Oct 1997 20:15:31 GMT



Kevin A. Foss wrote:

> On Mon, 27 Oct 1997 16:47:56 +0100 (MET), Peter Gerwinski wrote:
> 
> >On 21 September, Frank Heckenbach posted a Unit `Rand' to this list
> >which implements a `random' and a `randomize' function just this
> >way.  You can extract it from the GPC mailing list archives (see
> >http://home.pages.de/~GNU-Pascal/support.html) or download it from
> >ftp://agnes.dida.physik.uni-essen.de/gnu-pascal/contrib/rand.pas.
> 
> IMHO, this code is still broken.

True, to some extent. See my previous mail.

> On a non-DOS platform it tries to
> pass an integer to a function expecting a cardinal and always returns a
> type check error and doesn't compile.

Assigning an integer to a cardinal is *NOT* a type check error, since the
different integer types are assignment compatible. It could cause a range
check error -- as soon as range checking will be implemented in GPC.

The fact that GPC does report an "incompatible type" here (it didn't in
earlier versions, otherwise I'd have noted when I wrote the unit), is indeed
a bug in GPC. I'll add it to the bugs section of the To-Do list. Thanks for
the (accidental ;-) bug report!

Here is a short example program for this bug. This program should compile,
but currently doesn't (it works, however, when I replace the functions v and
w by variables):

program x;

procedure a(v:integer;w:cardinal);
begin
  writeln(v,' ',w)
end;

function v:cardinal;
begin
  v:=1
end;

function w:integer;
begin
  w:=2
end;

begin
  a(v,w)
end.

> Is there some advanced switch
> that should be used in situations like this to turn off type-checking
> (I've never really looked at all of the directives and didn't see where
> they were in the docs after a quick look) and the type-casting section
> is empty.

Turning off type checking in general would be a bit much, but type casting
is indeed the right thing to do. Type casting works like

- variable type casting:

  TypeName ( Variable or reference expression {"lvalue"} )

- value type casting (for ordinal and real type values):

  TypeName ( expression )

>   SRand(GetPID);

So change this line to:

  SRand(Cardinal(GetPID));

-- 
Frank Heckenbach, Erlangen, Germany
heckenb@mi.uni-erlangen.de
http://home.pages.de/~fjf/links.htm


Frank Heckenbach (heckenb@mi.uni-erlangen.de)

HTML conversion by Lluís de Yzaguirre i Maura
Institut de Lingüística Aplicada - Universitat "Pompeu Fabra"
e-mail: de_yza@upf.es