Recent

Author Topic: How does MaxValue() work on Records and Multidimensional Arrays ?  (Read 4435 times)

FrankBKK

  • New Member
  • *
  • Posts: 31
I created a record and tried to get the Min/Max values for some of those columns via Math lib's MaxValue() but that does not seem to work.

Am i missing something syntax-wise or is there another way besides looping through the whole Array ?

Code: Pascal  [Select][+][-]
  1. type
  2.   TTest = Record
  3.     Name : String ;
  4.     ValuesA  : Integer ;
  5.     ValuesB : Integer ;
  6.   end ;
  7.   aTest = array of TTest;  
  8.  
  9.  
  10. // both of them give compiler errors:
  11. iMax := MaxValue(aTest.ValuesA) ;
  12. iMin := aTest.ValuesA.MinValue ;  
  13.  






« Last Edit: June 18, 2021, 10:06:39 am by FrankBKK »

avk

  • Hero Member
  • *****
  • Posts: 752
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #1 on: June 18, 2021, 11:41:24 am »
The Math.MaxValue()/MinValue() functions only work with arrays of simple numeric types. Personally, I know of only one third-party library that "out of the box" provides the functions you need for user-defined types - LGenerics.

FrankBKK

  • New Member
  • *
  • Posts: 31
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #2 on: June 18, 2021, 12:06:57 pm »
Thanks for your prompt feedback.

Yeah, it seems this way - hard to find any info on this ....

I will have a look on LGenerics - looks promising.

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #3 on: June 18, 2021, 01:45:13 pm »
The MaxValue function is not applicable to this case; as avk already noted the array elements must be simple numeric data types, but in your case the array constists of records, and the maxValue should be applied to one of the record elements.

But you can easily work around by quickly writing a record helper:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$MODE delphi}
  4.  
  5. type
  6.   TTest = Record
  7.     Name : String ;
  8.     ValuesA  : Integer ;
  9.     ValuesB : Integer ;
  10.   end ;
  11.   TTestArray = array of TTest;
  12.  
  13.   TTestArrayHelper = record helper for TTestArray
  14.     function MaxA: Integer;
  15.     function MaxB: Integer;
  16.   end;
  17.  
  18.   function TTestArrayHelper.MaxA: Integer;
  19.   var
  20.     t: TTest;
  21.   begin
  22.     Result := -MaxInt;
  23.     for t in self do
  24.       if t.ValuesA > Result then Result := t.ValuesA;
  25.   end;
  26.  
  27.   function TTestArrayHelper.MaxB: Integer;
  28.   var
  29.     t: TTest;
  30.   begin
  31.     Result := -MaxInt;
  32.     for t in self do
  33.       if t.ValuesB > Result then Result := t.ValuesB;
  34.   end;
  35.  
  36. var
  37.   test: TTestArray;
  38. begin
  39.   SetLength(test, 2);
  40.  
  41.   test[0].Name := 'A';
  42.   test[0].ValuesA := 11;
  43.   test[0].ValuesB := 2;
  44.  
  45.   test[1].Name := 'B';
  46.   test[1].ValuesA := 10;
  47.   test[1].ValuesB := 20;
  48.  
  49.   WriteLn(test.MaxA);
  50.   WriteLn(test.MaxB);
  51.  
  52.   ReadLn;
  53.  
  54. end.

FrankBKK

  • New Member
  • *
  • Posts: 31
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #4 on: June 18, 2021, 03:56:02 pm »
Perfect, exactly what I wanted  !

Thanks a lot for your help !

avk

  • Hero Member
  • *****
  • Posts: 752
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #5 on: June 18, 2021, 04:45:27 pm »
...you can easily work around by quickly writing a record helper...
Personally, I like the idea of a "library" so that you don't have to implement any code over and over again, you just put it once in the library.
And perhaps this case is not as simple as it seems. I suppose an empty array is a perfectly legitimate value. What do the above helpers return in this case?

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #6 on: June 18, 2021, 04:54:45 pm »
I suppose an empty array is a perfectly legitimate value. What do the above helpers return in this case?
-MaxInt

avk

  • Hero Member
  • *****
  • Posts: 752
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #7 on: June 18, 2021, 05:19:52 pm »
And at the same time, the question remains, maybe this value is really the maximum in the array?
Code: Pascal  [Select][+][-]
  1.   ...
  2.   test[0].Name := 'A';
  3.   test[0].ValuesA := Low(Integer);
  4.   test[0].ValuesB := 2;
  5.  
  6.   test[1].Name := 'B';
  7.   test[1].ValuesA := -MaxInt;
  8.   test[1].ValuesB := 20;
  9.  
  10.  

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #8 on: June 18, 2021, 06:38:43 pm »
Sorry, this hairsplitting. If the difference between Low(Integer) (-2147483648) and -MaxInt (-2147483647) really is important the user has a problem with the data types anyway.

avk

  • Hero Member
  • *****
  • Posts: 752
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #9 on: June 18, 2021, 06:51:02 pm »
You know better, of course, but I believe that it is highly desirable to confidently distinguish the case when the maximum does not exist.

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #10 on: June 18, 2021, 07:08:37 pm »
I believe that it is highly desirable to confidently distinguish the case when the maximum does not exist.
Without a different layout of the function arguments you cannot, simply because there is no "NaN" or "Undefined" for integers. In my example numbers are expected to be in a "reasonable" range, and -MaxInt (or Low(Integer), or 999999, or whatever) takes the role of "no valid result". Of course, when the function is written in a different way there is more freedom:

Code: Pascal  [Select][+][-]
  1. function MaxA(out MaxValue: Integer): boolean;
  2. var
  3.   t: TTest;
  4. begin
  5.   Result := false;
  6.   MaxValue := -MaxInt;
  7.   if Length(self) = 0 then
  8.     exit;
  9.   for t in self do
  10.     if t.ValuesA > MaxValue then MaxValue := t.ValuesA;
  11.   Result := true;
  12. end;


avk

  • Hero Member
  • *****
  • Posts: 752
Re: How does MaxValue() work on Records and Multidimensional Arrays ?
« Reply #11 on: June 18, 2021, 07:29:17 pm »
Yes, I meant something like this.

 

TinyPortal © 2005-2018