Re: checking arbitrary bounds




"Pawel Slusarz" <wiedzmin@xxxxxxxxx> schreef in bericht
news:iQLle.11169$PS3.2823@xxxxxxxxxxxx
> Surely someone must've run into this before. I want to write a
> function that combines the functionality of Low() and High().
>
> // non-working example
> function IsIndexWithinBounds(i : integer; var a) : boolean;
> begin
> Result := (i >= low(a)) and (i <= high(a));
> end;
>
> I'd settle for arbitrary arrays, if the above can't be done.
>
> Thanks for your comments,
> Paul
>
>

The difficulty is with arrays with a non-zero lower bound.
If you use the following type and function:

type
Tar5 = array[5..15] of integer;

function withinBounds(k : integer; var a : array of integer) : boolean;
begin
result:=(k>=low(a)) AND (k<=high(a))
end;

then the result will not be correct when you pass it a variable of type
Tar5. That is because low() will always return zero for an open array
variable. The space of the variable is mapped onto a zero-based index.
But if you define the function specifically for Tar5, like

function withinBounds(k : integer; var a : Tar5) : boolean;
begin
result:=(k>=low(a)) AND (k<=high(a))
end;

then it will return the correct result when called with a Tar5 parameter.
If you have a limited number of array types for which you want to test the
index, then define functions like the above with the overload directive. So,
you make one for open integer arrays, one for open extended arrays, several
for predefined arrays, etc.

Tom


.



Relevant Pages

  • Re: "Ugly" declaration of high rank automatic arrays
    ... Not unless assumed-shape arrays fit your needs. ... several arrays you can use 'specification variables' (I just now made ... CHARACTER variable has zero length at ... and both compilers are handling this case OK. ...
    (comp.lang.fortran)
  • Re: should every thing be zero indexed?
    ... I just don't know why a programmer would want his 4th ... As for zero based indexing, i think that's fine for arrays ... which you can use an iterator in a loop, ...
    (comp.programming)
  • Re: convert from zero-index array to MatLab
    ... The FOR statement will accept a zero index, so it is up to your ... Wouldn't it be nice if matlab started their ... arrays at zero like everyone else. ... PS. yes, Rroot is real roots - a function defined elsewhere in program, ...
    (comp.soft-sys.matlab)
  • Re: OverflowError: math range error...
    ... I have a written a script that will check to see if the divisor is zero ... Does anyone know why Python is complaining: ... Comparisons yield arrays of boolean values. ... Both numarray and numpy throw an exception when one attempts to use arrays as truth values since the desired meaning is ambiguous. ...
    (comp.lang.python)
  • Re: Finding the zero crossings
    ... John D'Errico wrote: ... >>> I have x and y which are two arrays of real numbers. ... How do I find the zero crossings of this function y ... > there is a bug of course, ...
    (comp.soft-sys.matlab)