lqhenwunai 发表于 2022-9-19 15:19:55

c预定义问题

请教各位大神,

我在读别人代码时遇到以下预定义语句,其中v是一个结构体变量,包含val,sum,sum2 这三个real类型数据,n是int型参数

before:

#define PropAvg(v, n) \
   v.sum /= n, v.sum2 = sqrt (Max (v.sum2 / n - Sqr (v.sum), 0.))

即PropAvg这个函数把v的sum2运算后返还给v的sum2


但是当我对相关变量进行打印时(check 那行语句),即显示打印v.sum2, 以及 v.sum2等号右面内容,得到的结果却不同


#define PropAvg(v, n) \
   v.sum /= n, v.sum2 = sqrt (Max (v.sum2 / n - Sqr (v.sum), 0.));\
printf("Check if same%f %f \n",      v.sum2,       sqrt (Max (v.sum2 / n - Sqr (v.sum), 0.)));\



得到的结果是:
Check if same 0.146077 0.000000

请问这是什么原因呢?

人造人 发表于 2022-9-19 15:27:16

发完整的代码,你不发完整的代码我们就没办法给你调试代码

lqhenwunai 发表于 2022-9-19 15:53:37

人造人 发表于 2022-9-19 15:27
发完整的代码,你不发完整的代码我们就没办法给你调试代码

谢谢。
有好几个文件构成,所以请直接下载吧:
链接: https://pan.baidu.com/s/13h7lZvPcbc55j_KTREfsEw?pwd=p8wx 提取码: p8wx

相关预定义文件在src/in_mddefs.h文件中。

编译运行的话,直接./crun.sh




lqhenwunai 发表于 2022-9-19 16:33:40

人造人 发表于 2022-9-19 15:27
发完整的代码,你不发完整的代码我们就没办法给你调试代码

谢谢。
文件比较多,请直接下载吧。

提取码4tdw


上面提到的预定义的文件在src/in_mddefs.h中

加压后直接运行./crun.sh

会得到类似这样的输出
usage ./crun.sh chp# hw#. i.e. ./crun.sh 02 1
NameList -- data
deltaT                0.00500000
density                0.800000
initUcell        20 20
stepAvg                100
stepEquil        0
stepLimit        10000
temperature        1.00000
----
lqtest1 sum sum2 1.106495 1.224331   
lqtest2 sum sum2 2.383097 2.854043   
lqtest3 sum sum2 3.859806 5.034715   
lqtest4 sum sum2 5.563714 7.938016   
lqtest5 sum sum2 7.522566 11.775116   
lqtest6 sum sum2 9.763801 16.798253   

....
----------------------
Check if same 0.000059 0.000000
------------------------
----------------------
Check if same 0.016839 0.000000
------------------------

(跑一两秒结束循环就好。)

人造人 发表于 2022-9-21 00:58:50

今天有点晚了,明天给你看看

lqhenwunai 发表于 2022-9-21 20:41:28

人造人 发表于 2022-9-21 00:58
今天有点晚了,明天给你看看

哦,非常感谢。不过已经查出问题在哪了。不用浪费时间了。实在不好意思。

人造人 发表于 2022-9-21 20:46:27

lqhenwunai 发表于 2022-9-21 20:41
哦,非常感谢。不过已经查出问题在哪了。不用浪费时间了。实在不好意思。

结果确实是0,没问题
把代码改成这样,然后你看我的调试过程
    //PropAvg (totEnergy, stepAvg);

   /*
    totEnergy.sum /= stepAvg;
    totEnergy.sum2 = sqrt (Max (totEnergy.sum2 / stepAvg - Sqr (totEnergy.sum), 0.));
    printf("Check if same%f %f \n",totEnergy.sum2,sqrt (Max (totEnergy.sum2 / stepAvg - Sqr (totEnergy.sum), 0.)));
    */
    totEnergy.sum /= stepAvg;
    //totEnergy.sum2 = sqrt (Max (totEnergy.sum2 / stepAvg - Sqr (totEnergy.sum), 0.));
    {
      double a = Sqr(totEnergy.sum);
      double b = totEnergy.sum2 / stepAvg;
      double c = b - a;
      double d = Max(c, 0);
      totEnergy.sum2 = sqrt(d);
    }
    //printf("Check if same%f %f \n",totEnergy.sum2,sqrt (Max (totEnergy.sum2 / stepAvg - Sqr (totEnergy.sum), 0.)));
    {
      double a = Sqr(totEnergy.sum);
      double b = totEnergy.sum2 / stepAvg;
      double c = b - a;
      double d = Max(c, 0);
      double e = sqrt(d);
      printf("Check if same%f %f \n", totEnergy.sum2, e);
    }



    PropAvg (kinEnergy, stepAvg);
//printf("~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
//printf("before %f %f\n",PropEst(pressure));


PropEst100 sum sum2 457.510422 2166.029826

Breakpoint 1, AccumProps (icode=2) at src/pr_02_1.c:323
323          totEnergy.sum /= stepAvg;
(gdb) display totEnergy
1: totEnergy = {val = 0.99522634417443578, sum = 99.518657934111843, sum2 = 99.039636125322588}
(gdb) display stepAvg
2: stepAvg = 100
(gdb) n
326                double a = Sqr(totEnergy.sum);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 99.039636125322588}
2: stepAvg = 100
(gdb) n
327                double b = totEnergy.sum2 / stepAvg;
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 99.039636125322588}
2: stepAvg = 100
(gdb) print a
$1 = 0.9903963277006762
(gdb) n
328                double c = b - a;
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 99.039636125322588}
2: stepAvg = 100
(gdb) print b
$2 = 0.99039636125322583
(gdb) n
329                double d = Max(c, 0);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 99.039636125322588}
2: stepAvg = 100
(gdb) print c
$3 = 3.3552549627557937e-08
(gdb) n
330                totEnergy.sum2 = sqrt(d);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 99.039636125322588}
2: stepAvg = 100
(gdb) print d
$4 = 3.3552549627557937e-08
(gdb) n
334                double a = Sqr(totEnergy.sum);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 0.00018317355056764593}
2: stepAvg = 100
(gdb) n
335                double b = totEnergy.sum2 / stepAvg;
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 0.00018317355056764593}
2: stepAvg = 100
(gdb) print a
$5 = 0.9903963277006762
(gdb) n
336                double c = b - a;
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 0.00018317355056764593}
2: stepAvg = 100
(gdb) print b
$6 = 1.8317355056764593e-06
(gdb) n
337                double d = Max(c, 0);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 0.00018317355056764593}
2: stepAvg = 100
(gdb) print c
$7 = -0.99039449596517048
(gdb) n
338                double e = sqrt(d);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 0.00018317355056764593}
2: stepAvg = 100
(gdb) print d
$8 = 0
(gdb) n
339                printf("Check if same%f %f \n", totEnergy.sum2, e);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 0.00018317355056764593}
2: stepAvg = 100
(gdb) print e
$9 = 0
(gdb) n
Check if same0.000183 0.000000
344          PropAvg (kinEnergy, stepAvg);
1: totEnergy = {val = 0.99522634417443578, sum = 0.99518657934111843, sum2 = 0.00018317355056764593}
2: stepAvg = 100
(gdb)
页: [1]
查看完整版本: c预定义问题