在C中比较双重值与EPSILON的精度 [英] precision of comparing double values with EPSILON in C
问题描述
执行从struct CSV D获取2个数组(column1和column2)的函数,并从中绘制图形。
Doing function that takes 2 arrays (column1 and column2) from struct CSV D and plots the graph from it.
想法是找到每个的最大值,最小值阵列,然后在 min-EPSILON 和 max + EPSILON 之间的距离范围在600个相等的区域,其中 EPSILON = 10 ^( - 6)
Idea is to find max, min values of each array, then break range between min−EPSILON and max+EPSILON in to 600 equal regions, where EPSILON = 10^(−6)
问题是该函数不能正确绘制最低行,我认为比较数组中的值与 min-EPSILON 的问题是不确定的。请指导。
Problem is that function does not plot the lowest line properly, I think the issue is when comparing the value from array with min-EPSILON, not sure. Please advice.
这是我的代码。
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;
}
推荐答案
边界计算是复杂的,孔。
Bounds calculations are convoluted and have holes.
看到 upper_bound_x [n] == lower_bound_x [n + 1]
。然后当与(D->值[k] [col2] == upper_bound_x [n]
进行比较时,它将不适合区域 n
或区域 n + 1
。
See that upper_bound_x[n] == lower_bound_x[n+1]
. Then when a compare occurs with (D->values[k][col2] == upper_bound_x[n]
, it will neither fit in in region n
nor region 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]
数组,然后使用compare:
Suggest re-write and use a bound_x[X_REGIONS+1]
array and then use compare:
if (D->values[k][col2] >= bound_x[j] && D->values[k][col2] < bound_x[j] ){
或者,代码可以跳过 bound []
数组(x& y)并计算边界。
Alternately, code could skip the bound[]
arrays (x&y) and calculate bounds on the fly.
次要:
重复的代码:使帮助函数计算最小和最大然后每次计算一次,以计算 x
和 y
。
Repeated code: Make helper functions to calculate min and max and then cal once each to calculate for the x
and the y
.
代码应该定义 CSV
。在一列中有 x
是混乱的,另一列中有 y
。更好地拥有一个数组 point
(使自己的结构保持一个 x
和 y
),而不是一个数组
double
pair。
Code should post definition of CSV
. It is confusion to have x
in one column and y
in another. Better to have a array of point
(Make own struct holding an x
and y
), rather than an array of double
pairs.
确保 #include< math.h>
这篇关于在C中比较双重值与EPSILON的精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!