我有以下函数,它应该可以正常使用for循环来分析一系列双重结果是否已“稳定”。我所写的内容总共得到了3分,我想知道我如何能够接受一个名为“minNumberOfSamples”的变量并将其与那么多结果进行比较?
bool MeasurementStabilized(double newResult, double percentageThreshold, double limitRange)
{
static double meas1 = 1E100;
static double meas2 = 1E100;
if ((abs(newResult - meas2) / limitRange >= percentageThreshold) ||
(abs(meas2 - meas1) / limitRange >= percentageThreshold))
return TRUE;
meas1 = meas2;
meas2 = newResult;
return FALSE;
}
答案 0 :(得分:0)
我想我现在在@John Coleman的帮助下离开了。这似乎可以完成这项工作,但如果有人能想到任何改进,那就寻找一些改进:
static double* gArr;
void MeasurementStabilizedCleanup(void)
{
free(gArr);
}
static bool MeasurementStabilized(double newResult, double percentageThreshold, double limitRange, int minNumberOfSamples)
{
static bool firstRun = TRUE;
bool retVal = TRUE;
int x;
//init variables
if (firstRun)
{
gArr = malloc((minNumberOfSamples + 1) * sizeof(double));
for(x = 0; x <= minNumberOfSamples; x++)
{
gArr[x] = 1E100;
}
firstRun = FALSE;
}
//loop through each value comparing it to the latest result
for(x = minNumberOfSamples - 1; x >= 0; x--)
{
gArr[x + 1] = gArr[x]; //4 -> 5 , 3 -> 4, ...
if (fabs(newResult - gArr[x]) / limitRange >= percentageThreshold)
retVal = FALSE;
}
gArr[0] = newResult; //dont forget to update the latest sample
return retVal;
}
void main(void)
{
double doubles[] = {1, 2, 4, 4, 4, 4, 4};
int x;
for (x = 0; x <= 6; x++)
{
if(MeasurementStabilized(doubles[x], 0.01, 4, 4))
{
;
}
}
MeasurementStabilizedCleanup();
}