代码1:
#define max(a,b) ((((long)((a)-(b)))&0x80000000)?b:a)
代码2:
double c = 1.0;
double d = 0.9999999999999999;
cout << (long)(c - d) << endl;
cout << c - d << endl;
cout << ((c - d) > 0) << endl;
double a = 1.1;
//cout << reinterpret_cast<int>(a) << endl;
cout << static_cast<int>(a) << endl;
//cout << dynamic_cast<int>(a) << endl;
//cout << const_cast<int>(a) << endl;
输出:
0
1.11022e-16
1
备注:
涉及到类型转换的时候要特别注意。如上面代码所示,double到int的类型转换,只能是static_cast类型的,其他类型则编译出错。
因为有代码2的输出,所以代码1中的宏定义并不适用于浮点数的计算。
代码3:
(int)(0.1*10) // 1
(int)((0.1+0.7)*10) // 7
(int)((0.1+0.9)*10) // 10
以上类型转换在PHP/Java/C++/C#下运行后的结果都如注释所示。