Forum > Beginners

Calculate mean for array of longint

(1/3) > >>

tudi_x:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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:
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:
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:
Isn't it rather trivial?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...  sum:= 0;  for i := low(intarray) to high(intarray) do  begin    sum := sum + intarray[i];  end;  result:= sum / length(intarray);... 
Bart

Thaddy:
Bart, true, but the result is a double anyway.... Unless a div is acceptable to him.

Navigation

[0] Message Index

[#] Next page

Go to full version