Recent

Author Topic: Calculate mean for array of longint  (Read 7135 times)

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Calculate mean for array of longint
« on: November 08, 2017, 10:59:26 am »
hi All,
please advise how i could use a builtin function to calculate the mean for an array of longint.
i see mean already works for the real type double.
thank you

Code: Pascal  [Select][+][-]
  1. procedure TGETSpeed.Execute;
  2. var
  3.   list: TStringList;
  4.   i: word;
  5.   latency_values: array of longint;
  6.   mean_values: array of longint;
  7.   max_value, min_value: longint;
  8.  
  9. begin
  10.   list := TStringList.Create;
  11.   list.LoadFromFile('bytes.txt');
  12.  
  13.   for i := 0 to list.Count - 1 do
  14.   begin
  15.     if (i > 0) and (i < list.Count - 1) then
  16.     begin
  17.       SetLength(latency_values, Length(latency_values) + 1);
  18.       latency_values[Length(latency_values)] := CheckSpeed(_URL, StrToInt(list.Strings[i]));
  19.     end
  20.     else
  21.       CheckSpeed(_URL, StrToInt(list.Strings[i]));
  22.   end;
  23.  
  24.   min_value := MinValue(latency_values);
  25.   max_value := MaxValue(latency_values);
  26.  
  27.   for i := Low(latency_values) to High(latency_values) do
  28.   begin
  29.     if (latency_values[i] <> min_value) and (latency_values[i] <> max_value) then
  30.     begin
  31.       SetLength(mean_values, Length(mean_values) + 1);
  32.       mean_values[Length(mean_values)] := latency_values[i];
  33.     end;
  34.   end;
  35.  
  36.   TriggerError('latency:' + IntToStr(mean(mean_values)));
  37.  
  38.   //CheckSpeed(_URL, 10485760);
  39.   FreeAndNil(list);
  40. end;      
Lazarus 2.0.2 64b on Debian LXDE 10

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Calculate mean for array of longint
« Reply #1 on: November 08, 2017, 11:10:16 am »
The math unit contains functions for mean calculation out-of-the-box. Just use the double one and use arrays of double. That's because mean is likely to have a fraction anyway..
« Last Edit: November 08, 2017, 11:22:03 am by Thaddy »
Specialize a type, not a var.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Calculate mean for array of longint
« Reply #2 on: November 08, 2017, 11:14:59 am »
i might be missing something. please advise.
as per https://www.freepascal.org/docs-html/rtl/math/mean.html i do not see for array of longint.
does it mean that i need to use real numbers when my values are integers?
Lazarus 2.0.2 64b on Debian LXDE 10

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Calculate mean for array of longint
« Reply #3 on: November 08, 2017, 11:21:16 am »
Isn't it rather trivial?

Code: Pascal  [Select][+][-]
  1. ...
  2.   sum:= 0;
  3.   for i := low(intarray) to high(intarray) do
  4.   begin
  5.     sum := sum + intarray[i];
  6.   end;
  7.   result:= sum / length(intarray);
  8. ...
  9.  

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Calculate mean for array of longint
« Reply #4 on: November 08, 2017, 11:24:16 am »
Bart, true, but the result is a double anyway.... Unless a div is acceptable to him.
Specialize a type, not a var.

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Calculate mean for array of longint
« Reply #5 on: November 08, 2017, 11:30:20 am »
sorry guys, i am puzzled how come some functions accept integers and some do not.
for example https://www.freepascal.org/docs-html/rtl/math/max.html.
i know how to calculate the mean but shouldn't we use a builtin function for all the defined types as we already have for some?
thank you
Lazarus 2.0.2 64b on Debian LXDE 10

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Calculate mean for array of longint
« Reply #6 on: November 08, 2017, 11:38:45 am »
Yeah. But what result do you expect for integer types? An integer or a float? Or add an IntMean?
Specialize a type, not a var.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Calculate mean for array of longint
« Reply #7 on: November 08, 2017, 11:41:44 am »
What defined types are you referring to? The link you provide shows the function Max overloaded for common data types from integer to double. But you asked for a function returning the mean of an array of longint.

You can actually find a lot of info about this with a google search. Here are some useful links:

https://stackoverflow.com/questions/38827605/easiest-way-to-find-the-mean-of-a-dynamic-array

http://forum.lazarus-ide.org/index.php?topic=18665.0

While Delphi provides a function SumInt, Free Pascal has Sum for several data types:
https://www.freepascal.org/docs-html/rtl/math/sum.html
« Last Edit: November 08, 2017, 11:45:05 am by Munair »
keep it simple

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Calculate mean for array of longint
« Reply #8 on: November 08, 2017, 11:43:00 am »
Code: Pascal  [Select][+][-]
  1. uses math;
  2.  
  3. ...
  4.  
  5. function mean(const data: array of Integer):Float;
  6. var
  7.   Sum: Int64;
  8.   i: Integer;
  9. begin
  10.   Result := 0;
  11.   if (Length(data) = 0) then Exit;
  12.   Sum := 0;
  13.   for i := 0 to high(data) do Sum := Sum + data[i];
  14.   Result := Sum / Length(data);
  15. end;
  16.  

Feel free to submit as a feature request in the bugtracker.

Notice that it can easily overflow (eg if data = [maxint,maxint,maxint]), that's probably why there are only functions for floting point data, where that risc is a little less.


Bart
« Last Edit: November 08, 2017, 11:46:07 am by Bart »

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Calculate mean for array of longint
« Reply #9 on: November 08, 2017, 11:46:43 am »
Code: Pascal  [Select][+][-]
  1. uses math;
  2.  
  3. ...
  4.  
  5. function mean(const data: array of Integer):Float;
  6. var
  7.   Sum: Int64;
  8.   i: Integer;
  9. begin
  10.   Result := 0;
  11.   if (Length(data) = 0) then Exit;
  12.   Sum := 0;
  13.   for i := 0 to high(data) do Sum := Sum + data[i];
  14.   Result := Sum / Length(data);
  15. end;
  16.  

Feel free to submit as a feature request in the bugtracker.

Notice that it can easily overflow (eg if data = [maxint,maxint,maxint]), that's probably why there are only functions for floting point data, where that risc is a little less.


Bart

It's already in the library:
https://www.freepascal.org/docs-html/rtl/math/mean.html
keep it simple

tudi_x

  • Hero Member
  • *****
  • Posts: 532
Re: Calculate mean for array of longint
« Reply #10 on: November 08, 2017, 11:59:02 am »
thank you Bart. opened https://bugs.freepascal.org/view.php?id=32661

@Munair: library is missing array of integers. only for real numbers the functionality is implemented. the point of 32661 is to cover all defined types.
Lazarus 2.0.2 64b on Debian LXDE 10

Thaddy

  • Hero Member
  • *****
  • Posts: 14214
  • Probably until I exterminate Putin.
Re: Calculate mean for array of longint
« Reply #11 on: November 08, 2017, 12:00:11 pm »
Specialize a type, not a var.

munair

  • Hero Member
  • *****
  • Posts: 798
  • compiler developer @SharpBASIC
    • SharpBASIC
Re: Calculate mean for array of longint
« Reply #12 on: November 08, 2017, 02:18:43 pm »
It's already in the library:
https://www.freepascal.org/docs-html/rtl/math/mean.html
No, it is not...

I see. Example that sticks to built-in library functions (Delphi compatible):

Code: Pascal  [Select][+][-]
  1. var
  2.   numbers: array[1..5] of int64;
  3.   meanVal: double;
  4. begin
  5.   numbers[1] :=  1;
  6.   numbers[2] :=  2;
  7.   numbers[3] :=  3;
  8.   numbers[4] :=  4;
  9.   numbers[5] := 25;
  10.  
  11.   meanVal := SumInt(numbers) / Length(numbers);
  12.  
  13.   ShowMessage('Mean    = '+FloatToStr(meanVal));
  14. end;
keep it simple

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Calculate mean for array of longint
« Reply #13 on: November 17, 2017, 11:58:22 pm »
Now is a good time for you to make an OVERLOADED function of MEAN...

function mean(const data: array of Integer):Float; Overload
Begin
 // write your code.
End;
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018