C中双值与ε比较的精度

C中双值与ε比较的精度,c,floating-point,double,epsilon,C,Floating Point,Double,Epsilon,Doing函数,该函数从struct CSV D中获取2个数组(column1和column2),并从中绘制图形 其思想是找到每个数组的最大值和最小值,然后在min之间打破范围−EPSILON和max+EPSILON在600个相等的区域内,其中EPSILON=10^(−6) 问题是函数没有正确地绘制最低线,我认为问题在于将数组中的值与min EPSILON进行比较时,不确定。请给我一些建议 这是我的密码 void do_plot(CSV *D, int column1, int colu

Doing函数,该函数从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;
}
void
do_绘图(CSV*D,int column1,int column2){
#定义Y_区域600
#定义X_区域600
#定义ε0.000001
int col1=column1-1;//因为C语言中的索引从0开始,所以更方便用户的值增加了1
int col2=第2-1列;
double min_y=D->value[0][col1];//列的最小值
double max_y=D->value[0][col1];//列的最大值
双最小值x=D->value[0][col2];//列的最小值
double max_x=D->value[0][col2];//列的最大值
int i=0,j=0,k=0;//迭代变量
双区间_x,区间_y;//区域
int counter;//计算“col1”和“column2”的元素在bucket中的数量
int plotval;//打印值
双上界_y[y_REGIONS+1],下界_y[y_REGIONS+1];//y中区域上下限的数组(添加额外的1以避免区域用尽)
双上界_x[x_区域+1],下界_x[x_区域+1];//用于x中区域的上界和下界的数组
而(i行数){
如果(D->values[i][col1]>max_y){
max_y=D->value[i][col1];
}
如果(D->values[i][col1]值[i][col1];
}
如果(D->values[i][col2]>max_x){
max_x=D->value[i][col2];
}
如果(D->values[i][col2]值[i][col2];
}
i++;
}
/*将EPSILON val添加到max和min*/
max_x=max_x+ε;
max_y=max_y+ε;
min_x=min_x-ε;
min_y=min_y-ε;
间隔_y=(max_y-min_y)/y_区域;//将y轴分解为y_区域相等的区域
区间_x=(max_x-min_x)/x_区域;//将x轴分解为Y_区域相等的区域
/*计算y的区域*/
上界\u y[0]=max\u y;//y中第一个区域的上界
下界\u y[0]=最大\u y-区间\u y;//y中第一个区域的下界
对于(j=0;jvalues[k][col2]<上限值x[j]&D->values[k][col2]>下限值x[j]){
计数器++;
}
}               
}
k=0//复位计数器
plotval=楼层(对数(计数器+1)/对数(2))//用于显示存储桶中的值数的公式
/*绘制x线*/
如果(plotval==0){
printf(“.”);
}
否则{
printf(“%d”,plotval);
}
}
}
printf(“\n”);
返回;
}

边界计算复杂且有漏洞

请参见
上限\ux[n]==下限\ux[n+1]
。然后,当与
(D->value[k][col2]==上限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] ){
或者,代码可以跳过
绑定[]
数组(x&y)并动态计算边界

小调:

重复编码:使助手函数计算最小值和最大值,然后分别计算一次
x
y

代码应该发布CSV的定义。在一列中有x,在另一列中有y,这很混乱。最好有一个点的数组(创建自己的结构,其中包含xy),而不是一个对的数组


如果你想问这个问题,请至少解释一下为什么(-u-):Post
CSV
类型。
EPSILON
。确保后期编译(什么是
vauess
)?@chux-thnx-mate,修复了
vauess
,定义了
EPSILON
CSV
,从中使用了数组
D值[a] [b]
if (D->values[k][col2] >= bound_x[j] && D->values[k][col2] < bound_x[j] ){