我正在使用 gcc (Ubuntu 13.2.0-23ubuntu4) 13.2.0。
我的代码中有一个错误,其中数值分析函数给出了非常不同的答案。 以下每项都返回 true。
assert (fpclassify(x) == FP_NORMAL);;
assert (x == x);
assert (~(isnan(x)));
assert (~(isnormal(x)));
我认为一切都应该是单线程的,我不认为任何事情会神奇地改变我的代码之外的变量的值。我无法理解怎么会这样。
这可能是编译器错误还是有其他解释?
~
运算符是按位补码——它翻转值的所有位——不是逻辑非。因此,将其应用于“布尔”值很少会达到您想要的效果。例如程序:
#include <stdbool.h>
#include <stdio.h>
int main() {
if (~true) {
printf("~true is true\n");
}
}
将打印
~true is true