列如数组array[401.843,443.38,508.667,659.051,661.03,789.256,799.369];
取接近660的那个值。肉眼观察659.051。j=相邻两数浮动差。
int value=array[i]-660; int result=0.0;
代码逻辑:
double val = 0.0;
const double eps = 1e-6;
double wave = 0.0,up=0.0, dow = 0.0, b_= 660.0;
double arr[] = {11.11,111.111,222.222,333.336,456.123,659.051,661.03,849.368,'\0'};
for (size_t i = 0; i < sizeof arr; i++)
{
if (i == 0)
continue;
val = arr[i-1] - b_;
if ((val >= -eps) && (val <= eps))
{
dow=arr[i - 1] - b_;
up = arr[i] - b_;
if (up<dow)
{
printf("%f\n", arr[i]);
}
else
{
printf("%f\n", arr[i-1]);
}
}
}
取660相邻前后俩个数与之差值最小那个数就OK了。