Kaydet (Commit) 9176dd1a authored tarafından Eike Rathke's avatar Eike Rathke

Introduce ScFunctionData::update() and getResult()

... to further replace code where that is explicitly done.

Change-Id: I2d4065ee2c111a073b65bfc0e42e3479fbd9cf6e
Reviewed-on: https://gerrit.libreoffice.org/65039
Tested-by: Jenkins
Reviewed-by: 's avatarEike Rathke <erack@redhat.com>
üst b586bfcf
...@@ -60,6 +60,9 @@ struct ScFunctionData // to calculate single functions ...@@ -60,6 +60,9 @@ struct ScFunctionData // to calculate single functions
ScFunctionData( ScSubTotalFunc eFn ) : ScFunctionData( ScSubTotalFunc eFn ) :
eFunc(eFn), nVal(0.0), nCount(0), bError(false) {} eFunc(eFn), nVal(0.0), nCount(0), bError(false) {}
void update( double fNewVal );
/// Check bError after (!) obtaining the result.
double getResult();
}; };
#endif #endif
......
...@@ -62,6 +62,132 @@ bool SubTotal::SafeDiv(double& fVal1, double fVal2) ...@@ -62,6 +62,132 @@ bool SubTotal::SafeDiv(double& fVal1, double fVal2)
return bOk; return bOk;
} }
void ScFunctionData::update( double fNewVal )
{
if (bError)
return;
switch (eFunc)
{
case SUBTOTAL_FUNC_SUM:
if (!SubTotal::SafePlus(nVal, fNewVal))
bError = true;
break;
case SUBTOTAL_FUNC_PROD:
if (nCount == 0) // copy first value (nVal is initialized to 0)
{
nVal = fNewVal;
nCount = 1; // don't care about further count
}
else if (!SubTotal::SafeMult(nVal, fNewVal))
bError = true;
break;
case SUBTOTAL_FUNC_CNT:
case SUBTOTAL_FUNC_CNT2:
++nCount;
break;
case SUBTOTAL_FUNC_AVE:
if (!SubTotal::SafePlus(nVal, fNewVal))
bError = true;
else
++nCount;
break;
case SUBTOTAL_FUNC_MAX:
if (nCount == 0) // copy first value (nVal is initialized to 0)
{
nVal = fNewVal;
nCount = 1; // don't care about further count
}
else if (fNewVal > nVal)
nVal = fNewVal;
break;
case SUBTOTAL_FUNC_MIN:
if (nCount == 0) // copy first value (nVal is initialized to 0)
{
nVal = fNewVal;
nCount = 1; // don't care about further count
}
else if (fNewVal < nVal)
nVal = fNewVal;
break;
case SUBTOTAL_FUNC_VAR:
case SUBTOTAL_FUNC_STD:
case SUBTOTAL_FUNC_VARP:
case SUBTOTAL_FUNC_STDP:
maWelford.update( fNewVal);
break;
default:
// unhandled unknown
bError = true;
}
}
double ScFunctionData::getResult()
{
if (bError)
return 0.0;
double fRet = 0.0;
switch (eFunc)
{
case SUBTOTAL_FUNC_CNT:
case SUBTOTAL_FUNC_CNT2:
fRet = nCount;
break;
case SUBTOTAL_FUNC_SUM:
case SUBTOTAL_FUNC_MAX:
case SUBTOTAL_FUNC_MIN:
// Note that nVal is 0.0 for MAX and MIN if nCount==0, that's also
// how it is defined in ODFF.
fRet = nVal;
break;
case SUBTOTAL_FUNC_PROD:
fRet = (nCount > 0) ? nVal : 0.0;
break;
case SUBTOTAL_FUNC_AVE:
if (nCount == 0)
bError = true;
else
fRet = nVal / nCount;
break;
case SUBTOTAL_FUNC_VAR:
case SUBTOTAL_FUNC_STD:
if (maWelford.getCount() < 2)
bError = true;
else
{
fRet = maWelford.getVarianceSample();
if (fRet < 0.0)
bError = true;
else if (eFunc == SUBTOTAL_FUNC_STD)
fRet = sqrt( fRet);
}
break;
case SUBTOTAL_FUNC_VARP:
case SUBTOTAL_FUNC_STDP:
if (maWelford.getCount() < 1)
bError = true;
else if (maWelford.getCount() == 1)
fRet = 0.0;
else
{
fRet = maWelford.getVariancePopulation();
if (fRet < 0.0)
bError = true;
else if (eFunc == SUBTOTAL_FUNC_STDP)
fRet = sqrt( fRet);
}
break;
default:
assert(!"unhandled unknown");
bError = true;
break;
}
if (bError)
fRet = 0.0;
return fRet;
}
void WelfordRunner::update( double fVal ) void WelfordRunner::update( double fVal )
{ {
++nCount; ++nCount;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment