Calculate mean for array of longint lazarus / 61-Beginners ---------------------------------------------------------------------- tudi_x · 2017-11-08 09:59 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TGETSpeed.Execute;var  list: TStringList;  i: word;  latency_values: array of longint;  mean_values: array of longint;  max_value, min_value: longint; begin  list := TStringList.Create;  list.LoadFromFile('bytes.txt');   for i := 0 to list.Count - 1 do  begin    if (i > 0) and (i < list.Count - 1) then    begin      SetLength(latency_values, Length(latency_values) + 1);      latency_values[Length(latency_values)] := CheckSpeed(_URL, StrToInt(list.Strings[i]));    end    else      CheckSpeed(_URL, StrToInt(list.Strings[i]));  end;   min_value := MinValue(latency_values);  max_value := MaxValue(latency_values);   for i := Low(latency_values) to High(latency_values) do  begin    if (latency_values[i] <> min_value) and (latency_values[i] <> max_value) then    begin      SetLength(mean_values, Length(mean_values) + 1);    mean_values[Length(mean_values)] := latency_values[i];    end;  end;   TriggerError('latency:' + IntToStr(mean(mean_values)));   //CheckSpeed(_URL, 10485760);  FreeAndNil(list);end; ---------------------------------------------------------------------- Thaddy · 2017-11-08 10:10 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.. ---------------------------------------------------------------------- tudi_x · 2017-11-08 10:14 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? ---------------------------------------------------------------------- Bart · 2017-11-08 10:21 Isn't it rather trivial? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ...  sum:= 0;  for i := low(intarray) to high(intarray) do  begin    sum := sum + intarray[i];  end;  result:= sum / length(intarray);...  Bart ---------------------------------------------------------------------- Thaddy · 2017-11-08 10:24 Bart, true, but the result is a double anyway.... Unless a div is acceptable to him. ---------------------------------------------------------------------- tudi_x · 2017-11-08 10:30 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 ---------------------------------------------------------------------- Thaddy · 2017-11-08 10:38 Yeah. But what result do you expect for integer types? An integer or a float? Or add an IntMean? ---------------------------------------------------------------------- munair · 2017-11-08 10:41 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 ---------------------------------------------------------------------- Bart · 2017-11-08 10:43 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses math; ... function mean(const data: array of Integer):Float; var  Sum: Int64;  i: Integer;begin  Result := 0;  if (Length(data) = 0) then Exit;  Sum := 0;  for i := 0 to high(data) do Sum := Sum + data[i];  Result := Sum / Length(data);end;  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 ---------------------------------------------------------------------- munair · 2017-11-08 10:46 Quote from: Bart on November 08, 2017, 11:43:00 amCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses math; ... function mean(const data: array of Integer):Float; var  Sum: Int64;  i: Integer;begin  Result := 0;  if (Length(data) = 0) then Exit;  Sum := 0;  for i := 0 to high(data) do Sum := Sum + data[i];  Result := Sum / Length(data);end;  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 ---------------------------------------------------------------------- tudi_x · 2017-11-08 10:59 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. ---------------------------------------------------------------------- Thaddy · 2017-11-08 11:00 Quote from: Munair on November 08, 2017, 11:46:43 amIt's already in the library: https://www.freepascal.org/docs-html/rtl/math/mean.html No, it is not... ---------------------------------------------------------------------- munair · 2017-11-08 13:18 Quote from: Thaddy on November 08, 2017, 12:00:11 pmQuote from: Munair on November 08, 2017, 11:46:43 amIt'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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  numbers: array[1..5] of int64;  meanVal: double;begin  numbers[1] :=  1;  numbers[2] :=  2;  numbers[3] :=  3;  numbers[4] :=  4;  numbers[5] := 25;   meanVal := SumInt(numbers) / Length(numbers);   ShowMessage('Mean    = '+FloatToStr(meanVal));end; ---------------------------------------------------------------------- jamie · 2017-11-17 22:58 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;