c - 莫名其妙的计算错误

标签 c structure

我正在编写一个程序,从单独的信号和背景文件中读取波长和强度数据(因此每个文件由多对波长和强度组成)。正如您所看到的,我通过创建一个结构,然后在循环中使用 fscanf 将值分配给结构中的正确元素来完成此操作。读入数据后,程序应该将其绘制在每个文件中记录的波长重叠的间隔上,即波长的公共(public)范围。波长在存在重叠的地方完美地对齐,并且已知以恒定的差值间隔。因此,我辨别结构阵列的哪些元素适用的方法是确定两个文件中哪一个的最小波长较高,最大波长较低。然后,对于具有较低最小值和较高最大值的文件,我会找到该文件与较高最小值/较低最大值之间的差异,然后将其除以常数步长以确定要偏移的元素数量。这是可行的,除非数学完成后,程序返回一个完全无法解释的错误答案。

在下面的代码中,我通过计算一个元素与其前面的元素的波长之间的差值将常数步长定义为 lambdastep。以我的样本数据为例,它是0.002,这是通过printf确认的。但是,当我运行程序并除以 lambdastep 时,我得到了错误的答案。当我运行程序除以 0.002 时,我得到了正确的答案。为什么会出现这种情况呢?我想不出任何解释。

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include "plots.h"

struct spectrum{
    double lambda;
    double intensity;
};

main(){
double a=0,b=0,c=0,d=0,lambdastep,smin,smax,bmin,bmax,tmin,tmax,sintmin,bintmin,tintmin,sintmax,bintmax,tintmax,ymin,ymax;
int ns,nb,nt,i=0,sminel,smaxel,bminel,bmaxel,tminel,tmaxel;
double min(struct spectrum *a,int,int);
double max(struct spectrum *a,int,int);
FILE *Input;                                
Input = fopen("sig.dat","r");
FILE *InputII;                              
InputII = fopen("bck.dat","r");
fscanf(Input,"%d",&ns);
fscanf(InputII,"%d",&nb);
struct spectrum signal[ns];
struct spectrum background[nb];
struct spectrum *s = &signal[0];
struct spectrum *ba = &background[0];
s = malloc(ns*sizeof(struct spectrum));
ba = malloc(nb*sizeof(struct spectrum));
while( fscanf(Input,"%lf%lf",&a,&b) != EOF){
    signal[i].lambda = a;
    signal[i].intensity = b;
    i++;
}
i = 0;
while( fscanf(InputII,"%lf%lf",&c,&d) != EOF){
    background[i].lambda = c;
    background[i].intensity = d;
    i++;
}
for (i=0; i < ns ;i++){
    printf("%.7lf %.7lf\n", signal[i].lambda,signal[i].intensity);
}
printf("\n");
for (i=0; i < nb ;i++){
    printf("%.7lf %.7lf\n", background[i].lambda,background[i].intensity);
}
lambdastep = signal[1].lambda - signal[0].lambda;           //this is where I define lambdastep as the interval between two measurements
smin = signal[0].lambda;
smax = signal[ns-1].lambda;
bmin = background[0].lambda;
bmax = background[nb-1].lambda;
if (smin > bmin)
    tmin = smin;
else
    tmin = bmin;
if (smax > bmax)
    tmax = bmax;
else
    tmax = smax;
printf("%lf %lf %lf %lf %lf %lf %lf\n",lambdastep,smin,smax,bmin,bmax,tmin,tmax);   //here is where I confirm that it is .002, which is the expected value
sminel = (tmin-smin)/(lambdastep);  //sminel should be 27, but it returns 26 when lamdastep is used. it works right when .002 is directly entered , but not with lambdastep, even though i already confirmed they are exactly the same. why?

最佳答案

sminel 是一个整数,因此计算结束时 (tmin-smin)/lambdastep 将转换为整数。

lambdastep 中的一个非常细微的差异可能是获得例如27.00001 和 26.99999;后者在转换为 int 时截断为 26。

尝试使用floorceilround来更好地控制返回值的舍入。

关于c - 莫名其妙的计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15727217/

相关文章:

c - sscanf 多字符串扫描

c - C 中的 Srand 与 char 数组?

c - 更新函数中的结构数组并将其作为参数传递给其他函数

c - 如何从文件中读取数据?我无法修复它

c - 表达式必须是可修改的左值(指向结构的指针)

c - 有没有办法找出一个进程中对动态库的引用数?

c - 为什么 llvm 和 gcc 在 x86 64 上使用不同的函数序言?

TensorFlow 中 C 代码的代码完成

python - 解码 ctypes 结构

c - 打印我们的字符结构和长度