为什么不满足这个条件?

Why is this condition not satisfied ?

printf("Elements of vector U:\n");
rprint_vector(u, n); 
double vt = dasum_(&n, u, &incx);
printf("vt = %lf\n", vt);

if (vt == 0)
    printf("yes vt = 0\n");
else
    printf("No vt != 0\n");

结果:

Elements of vector U:
------------
0.000000
0.000000
-0.000000
0.000000
------------
vt = 0.000000
No vt != 0

即使变量vt等于0也不满足条件。哪里有问题!!

这是有限精度表示的本质。

我将使用固定精度十进制表示法进行类比。您将 1/3 表示为 0.333333,将 2/3 表示为 0.666667。因此,如果您执行 2/3 - 1/3 - 1/3,结果将是 0.000001。您可能会将其显示为 0.000,因为显示更多数字没有多大意义。但它并不完全等于零。

不要以这种方式比较浮点数,因为答案会从“是”切换到“否”,即使有一点不精确。如果你想写一个 "is very, very close to" 函数,那就写吧。