The following code crashed Delphi 1 with an "Integer Overflow" message.
var j,k : integer ;
for j := 1 to 20 do
begin
k := Random(1000) - 500 ;
end ;
The on-line documentation does not say what is the return type of Random
when it has an argument. It must be of type word, causing the right hand
side to be evaluated as a word variable. This word goes negative and causes
the crash if Random(1000) is less than 500.
On the other hand,
k := -500 + Random(1000) ;
works OK. Presumably by putting the -500 first, we have forced the right
side to be evaluated as an integer rather than as a word.
You can also force the expression to work by type-casting Random(1000) as an
integer ...
k := integer(Random(1000)) - 500 ;
Can anyone explain why the message is INTEGER overflow, when it seems to be
a WORD problem ? Anybody know of any similar problems with the language ?
The only one I've run across is in expressions such as
k := 80 * 1001 div 1101 ;
Although the result is going to be an integer, the expression overflows. You
have to say
k := longint(80) * 1001 div 1101 ;
to force evaluation in long arithmetic.
--- PPoint 2.00
---------------
* Origin: Kingston, Canada (1:249/109.11)
|