Total and mean numbers calculation in floating point data table
This function calculates the sum and the arithmetic mean (both with +/- signs) of floating point values in the specified 32-bit data table.
Input
starting area of data table
ending area of data table
Output
sum of all elements in data table area specified
mean of all elements in data table area specified
Input s1_Start specifies the starting area of the data table, and s2_End specifies the end. The sum of all elements in the data table is returned at output Sum and the arithmetic mean of all elements in the data table is returned at output Mean.
Instead of using this F instruction, we recommend using the corresponding FP7 instruction:
if the address of the variable at input s1_Start > s2_End.
if the address areas are different.
if the floating point values exceed the processing range.
if the address of the variable at input s1_Start > s2_End.
if the address areas are different.
if the floating point values exceed the processing range.
if the total value range overflows or an underflow.
All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.
VAR
start: BOOL:=FALSE;
(*activates the function*)
data_array: ARRAY [0..4] OF REAL:=[4.0,7.0,-32.0,13.0,5.0];
(*result after a 0->1 leading
edge from start:
[-32.0,4.0,5.0,7.0,13.0]*)
asc_order: INT:=0;
(*which way to sort:
0 means ascending order
1 means descending order*)
END_VAR
When the variable start is set to TRUE, the function is carried out.
It calculates the sum = 2.0 + 3.45 + (-6.91) + 5.44 + 1.3 = 5.28
and the mean = Sum/5 = 5.28/5 = 1.056
of the elements of the data_field.
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 6 ;
NETWORK_BODY
B(B_VARIN,,output,8,1,10,3,);
B(B_VARIN,,data field[0],8,2,10,4,);
B(B_VARIN,,data field[4],8,3,10,5,);
B(B_VAROUT,,sum,19,2,21,4,);
B(B_F,F352_FMEAN,,10,0,19,5,,?DEN?D@'s1'?Ds2?AENO?Csum?Cmean);
B(B_VAROUT,,mean,19,3,21,5,);
L(1,0,1,6);
END_NETWORK_BODY
END_NET_WORK
END_BODY
IF start then
F352_FMEAN( s1_Start:= data_field[0] ,
s2_End:= data_field[4] ,
Sum=> sum ,
Mean=> mean );
END_IF;