<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<div class="moz-cite-prefix">El 14/07/2017 a las 17:19, <a
class="moz-txt-link-abbreviated"
href="mailto:james@productionautomation.net">james@productionautomation.net</a>
escribió:<br>
</div>
<blockquote type="cite"
cite="mid:092a01d2fcb4$a5208390$ef618ab0$@productionautomation.net">
<blockquote type="cite">
<pre wrap="">If you declare Last_GS as int64, you should not get the warning.
</pre>
</blockquote>
<pre wrap="">I declared both Last_GS and G_End as int64, leaving Max_Program_to_Display as a word and still get the warning. If I also make Max_Program_to_Display int64, then I do not get the warning. I believe it's due to the -1. If Max_Program_To_Display was a 0 then subtracting 1 from it would be out of range from a word for that portion of the formula, even though the end result would fit in Last_GS</pre>
</blockquote>
Using your way, (Max_Program_To_Display-1) is calculated first.
Since the variable is type word, it would make word calculation,
with word range. In such case, if Max_Program_To_Display is zero,
you would get an underflow (and with no bounds checks, you could get
$FFFF that would be even worse).<br>
<br>
<blockquote type="cite"
cite="mid:092a01d2fcb4$a5208390$ef618ab0$@productionautomation.net">
<pre wrap="">
</pre>
<blockquote type="cite">
<pre wrap="">Last_GS:=G_End-(longint(Max_Program_To_Display)-1);
</pre>
</blockquote>
<pre wrap="">This aso fixes the warning if I leave all my variables alone. If I understand this correctly in this case longint() is a function that returns a longint variable to be used in the calculation, so when it does the -1 it's ok it that part of the formula ends up being negative.
So now my question is, which is the best method to use? My thinking with declaring Max_Program_To_Display as a word was that this value has no meaning if it is negative, and actually a word is way too generous for this value, a byte would be overkill.</pre>
</blockquote>
<br>
If negative has no sense, I would declare it word, or byte and cast
it to a signed integer if you need for internal calculations as have
been said. Declaring it word, you have made the compiler to give you
heads-up about a potential problem, then you can decide to check
before if Max_Program_To_Display is zero or double check there is
nothing wrong there. Other way it would have passed unnoticed and
could bite you latter in unexpected ways and points of execution,
making you hunt for a long time a weir bug.<br>
<br>
I wouldn't worry too much about declaring it word or byte. I think
that because of performance, the compiler may align in a word
(compiler gurus will tell better).<br>
In fact, I would forget about internal format and declare a new type
in the range of valid values i.e<br>
<pre>type
TMaxprogram=0..53;</pre>
and let the compiler decide what internal format to use.<br>
<br>
In the conversion I would use "integer" instead of "longint"<br>
<pre wrap="">Last_GS:=G_End-(integer(Max_Program_To_Display)-1);</pre>
"Integer" is the optimal integer format for that architecture,
probably "longint", but I would use "integer" anyway.<br>
<br>
My two cents.<br>
<pre class="moz-signature" cols="72">--
Saludos
Santiago A.
</pre>
</body>
</html>