例子
#include <stdio.h>
int main(){
float a = 16777217.0f;
if(a == 16777216.0f) // 这里判断应该为假?
printf("value is %.10fn", a);
if(0.1 + 0.2 == 0.3) // 这里判断应该为真?
printf("you will see: 0.1 + 0.2 == 0.3n");
int x = 65535;
int y = x*x;
printf("x * x = %dn", y); // 这里应该打印4294836225?
x = -2147483648;
// 这里应该打印true?
printf("x: %d and y: %d; (x>y) == (-x<-y) is %sn", -2147483648, 1, (x>1) == (-x<-1)?"true": "false");
return 0;
}
实验操作
$ gcc -o out float.c
# 或者gdb ./out
$ ./out
# 以下是输出
value is 16777216.0000000000
x * x = -131071
x: -2147483648 and y: 1; (x>y) == (-x<-y) is false