Subranges in Pascal
I’m reading some Pascal code and everything is pretty straightforward except subranges. In the program I’m reading there’s a variable d with the following type declaration: array[0..22] of 0..15 there’s then the following procedure: var a:integer; begin a:=0; while k>0 do begin k:=k-1; a:=(a+dig[k]*two) div 10; end; round_decimals:=(a+1) div 2; end; Where k is a variable of type 0..63. So since k can be above the range of the index of dig how does this code work? – It’s a routine for doing fixed point arithmetic and so I would assume the array indices overflows in some controlled manner, but if anybody could explain the exact behavior I’d be really happy. Also, do Pascal arrays assign in some known manner? There were a lot of versions of Pascal and I think this program is written in a pretty “Pascal‐version agnostic” manner, but I can’t find much explanation on the subtle semantics of subrange expressions.
![Subranges in Pascal](https://cdn.sstatic.net/Sites/softwareengineering/Img/apple-touch-icon@2.png?v=1ef7363febba)
I’m reading some Pascal code and everything is pretty straightforward except subranges. In the program I’m reading there’s a variable d
with the following type declaration:
array[0..22] of 0..15
there’s then the following procedure:
var a:integer;
begin
a:=0;
while k>0 do
begin
k:=k-1;
a:=(a+dig[k]*two) div 10;
end;
round_decimals:=(a+1) div 2;
end;
Where k
is a variable of type 0..63
. So since k
can be above the range of the index of dig
how does this code work? – It’s a routine for doing fixed point arithmetic and so I would assume the array indices overflows in some controlled manner, but if anybody could explain the exact behavior I’d be really happy.
Also, do Pascal arrays assign in some known manner? There were a lot of versions of Pascal and I think this program is written in a pretty “Pascal‐version agnostic” manner, but I can’t find much explanation on the subtle semantics of subrange expressions.