【问题标题】:precision of comparing double values with EPSILON in C在 C 中将 double 值与 EPSILON 进行比较的精度
【发布时间】:2015-08-06 02:31:37
【问题描述】:

执行从 struct CSV D 中获取 2 个数组(column1 和 column2)并从中绘制图形的函数。

想法是找到每个数组的最大值、最小值,然后打破 min−EPSILONmax+EPSILON 之间的范围 到 600 个相等的区域,其中 EPSILON = 10^(-6)

问题是函数没有正确绘制最低线,我认为问题在于将数组中的值与 min-EPSILON 进行比较时,不确定。请指教。

这是我的代码。

void   
do_plot(CSV *D, int column1, int column2) {
#define Y_REGIONS 600
#define X_REGIONS 600
#define EPSILON 0.000001
int col1=column1-1;                     //since indexing in C language starts from 0, to be more user friendly values increased by 1
int col2=column2-1;
double  min_y = D->values[0][col1]; //min val of column
double  max_y = D->values[0][col1]; //max val of column
double  min_x = D->values[0][col2]; //min val of column
double  max_x = D->values[0][col2]; //max val of column
int     i=0,j=0,k=0;                //iteration variables
double  interval_x, interval_y;     //region
int     counter;                    //counts how many elements of "col1" and "column2" are in bucket
int     plotval;                    //plotted value
double  upper_bound_y[Y_REGIONS+1],lower_bound_y[Y_REGIONS+1];      //arrays for lower and upper bounds of regions in y (added extra 1 not to run out of regions)
double  upper_bound_x[X_REGIONS+1],lower_bound_x[X_REGIONS+1];      //arrays for lower and upper bounds of regions in x
while (i < D->number_of_rows){      
    if (D->values[i][col1] > max_y){
        max_y = D->values[i][col1];
    }
    if (D->values[i][col1] < min_y){
        min_y = D->values[i][col1];
    }
    if (D->values[i][col2] > max_x){
        max_x = D->values[i][col2];
    }
    if (D->values[i][col2] < min_x){
        min_x = D->values[i][col2];
    }
    i++;
}
/* adding EPSILON val to max and min */
max_x=max_x+EPSILON;
max_y=max_y+EPSILON;
min_x=min_x-EPSILON;
min_y=min_y-EPSILON;
interval_y=(max_y-min_y)/Y_REGIONS; //breaking y axis into Y_REGIONS equal regions
interval_x=(max_x-min_x)/X_REGIONS; //breaking x axis into Y_REGIONS equal regions
/* calculating regions of y*/
upper_bound_y[0]=max_y;             //upper bound of the first region in y
lower_bound_y[0]=max_y-interval_y;  //lower bound of the first region in y
for (j=0; j<Y_REGIONS; j++){
    upper_bound_y[j+1]=upper_bound_y[j]-interval_y;
    lower_bound_y[j+1]=lower_bound_y[j]-interval_y;
}
/* calculating regions of x */
upper_bound_x[0]=min_x+interval_x;  //upper bound of the first region in y
lower_bound_x[0]=min_x;             //lower bound of the first region in y
for (j=0; j<X_REGIONS; j++){
    upper_bound_x[j+1]=upper_bound_x[j]+interval_x;
    lower_bound_x[j+1]=lower_bound_x[j]+interval_x;
}
/* plotting the graph */
for (i=0; i<Y_REGIONS; i++){
    printf("\n%6.20lf--%6.20lf: ", lower_bound_y[i], upper_bound_y[i]); //plotting y axis
    for (j=0; j<X_REGIONS; j++){    //x axis
        counter=0;          //resetting counter
        while (k <= D->number_of_rows){
            k++;
            /* checking whether element of input lies within region and counting number of elements */
            if (D->values[k][col1] < upper_bound_y[i] && D->values[k][col1] > lower_bound_y[i]){
                if (D->values[k][col2] < upper_bound_x[j] && D->values[k][col2] > lower_bound_x[j] ){
                    counter++;
                }
            }               
        }
        k=0; //resetting counter
        plotval=floor(log(counter+1)/log(2)); //formula to show number of values in bucket
        /* plotting x lines */
        if (plotval==0){
            printf(".");
        }
        else{
            printf("%d",plotval);
        }
    }
}
printf("\n");
return;
}

【问题讨论】:

  • 如果你问这个问题,请至少解释一下为什么(-_-)
  • @MotherLand:发布CSV 类型。 EPSILON。确保帖子编译(什么是vauess)?
  • @chux thnx mate,修复了vauess,定义了EPSILONCSV 从中使用了数组 D-&gt;values[a][b]

标签: c floating-point double epsilon


【解决方案1】:

边界计算是复杂的并且有漏洞。

看到upper_bound_x[n] == lower_bound_x[n+1]。然后,当与(D-&gt;values[k][col2] == upper_bound_x[n] 进行比较时,它既不适合区域n,也不适合区域n+1

// Existing code
upper_bound_x[0]=min_x+interval_x;  //upper bound of the first region in y
lower_bound_x[0]=min_x;             //lower bound of the first region in y
for (j=0; j<X_REGIONS; j++){
    upper_bound_x[j+1]=upper_bound_x[j]+interval_x;
    lower_bound_x[j+1]=lower_bound_x[j]+interval_x;
}
....
if (D->values[k][col2] < upper_bound_x[j] && D->values[k][col2] > lower_bound_x[j] ){

建议重写并使用bound_x[X_REGIONS+1] 数组,然后使用比较:

if (D->values[k][col2] >= bound_x[j] && D->values[k][col2] < bound_x[j] ){

或者,代码可以跳过 bound[] 数组 (x&y) 并即时计算边界。

次要:

重复代码:创建帮助函数来计算最小值和最大值,然后分别计算 xy

代码应该发布CSV 的定义。将x 放在一列中而将y 放在另一列中会造成混淆。最好有一个 point 数组(让自己的结构包含 xy),而不是 double 对数组。

一定要#include &lt;math.h&gt;

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 2012-11-21
    • 2018-06-05
    • 2016-03-11
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多