campo-sirio/gfm/wmean.c

91 lines
1.6 KiB
C
Raw Normal View History

/* DEC *WeightedMean(pDst,pSrc,weight,n)
*
* ARGUMENT
* DEC *pDst;
* DEC **pSrc, **weight;
* int n;
*
* DESCRIPTION
* Sets pDst to the weighted mean of the n DEC elements
* of the array pSrc[i], each with weight weight[i].
* n <= 0 is not allowed. pDst is unchanged if an error occurs.
*
* SIDE EFFECTS
* None.
*
* RETURNS
* Returns pointer to the average if successful, otherwise
* returns GM_NULL.
*
* POSSIBLE ERROR CODES
*
* GM_NULLPOINTER
* GM_ARGVAL
* GM_OVERFLOW
* GM_UNDERFLOW
*
* AUTHOR
* Jared Levy
* Copyright (C) 1987-1990 Greenleaf Software Inc. All rights reserved.
*
* MODIFICATIONS
*
*/
#include <stdio.h>
#include "gm.h"
#include "gmsystem.h"
DEC *WeightedMean(pDst,pSrc,weight,n)
DEC *pDst;
DEC **pSrc, **weight;
int n;
{
int i;
DEC dsum, *sum=&dsum, dsumw, *sumw=&dsumw;
DEC dprod, *prod=&dprod;
/* source must be supplied !! */
_MacStart(GM_WMEAN);
if (!pSrc||!weight) {
_MacErr(GM_NULLPOINTER);
_MacRet(GM_NULL);
}
if (n<=0) {
_MacErr(GM_ARGVAL);
_MacRet(GM_NULL);
}
for (i=0; i<n; i++) {
_MacInVarD(pSrc[i]);
_MacInVarD(weight[i]);
}
_MacOutVarD(pDst);
_MacDZero(sum);
_MacDZero(sumw);
for (i=0;i<n;i++) {
if (_MulDec80Bit(prod, pSrc[i], weight[i])==GM_OVERFLOW) {
_MacErr(GM_OVERFLOW);
_MacRet(GM_NULL);
}
if (_AddDec80Bit(sum, sum, prod)==GM_OVERFLOW) {
_MacErr(GM_OVERFLOW);
_MacRet(GM_NULL);
}
(void) _AddDec80Bit(sumw, sumw, weight[i]);
}
if (_MacIsDecZ(sumw)) {
_MacErr(GM_DIV0);
_MacRet(GM_NULL);
}
pDst = DivideDecimal(pDst, sum, sumw);
_MacRet(pDst);
}