delphi - Easiest way to find the mean of a dynamic array -
i have created dynamic array, , have passed values it. there shortcut finding mean of dynamic array.
var themin, themax: integer; x: array of integer; //dynamic array declaration .... themin := minintvalue(x);//i able retrieve minium value of dynamic array themax := maxintvalue(x);//i able retrieve maximum value of dynamic array
is there other way mean using math library.
it easy write such function.
function mean(const data: array of integer): double; overload; var i: integer; begin result := 0.0; := low(data) high(data) result := result + data[i]; result := result / length(data); end;
i overloaded sit alongside same named functions in math
unit.
if wish use built in library code can use sumint
math
unit:
themean := sumint(x) / length(x);
sumint
performs summation using integer
accumulator. faster bespoke function uses floating point accumulator. however, integer
accumulator potentially subject overflow may off-putting. on other hand, integer
accumulator potentially more accurate floating point accumulator. depending on usage requirements these issues may important you.
in bother cases, if input array of length 0 runtime floating point divide 0 error raised.
Comments
Post a Comment