结果确实是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));
PropEst 100 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)
|