#include <stdio.h>
#define SQR(X) X*X int main(int argc, char* argv[])
{
int a = ;
int k = ;
int m = ; printf("SQR(k+m) = %d\n", SQR(k+m)); //SQR(k+m) = 5
printf("SQR(k+m)/SQR(k+m) = %d\n", SQR(k+m)/SQR(k+m)); //SQR(k+m)/SQR(k+m) = 7
printf("SQR(k+m)/SQR(k+m) = %.2f\n", 1.0*SQR(k+m)/SQR(k+m)); //SQR(k+m)/SQR(k+m) = 7.00
printf("SQR(k+m)/SQR(k+m) = %.2f\n", SQR(k+1.0*m)/SQR(k+m)); //SQR(k+m)/SQR(k+m) = 7.50
printf("a/(SQR(k+m)/SQR(k+m)) = %d\n", a/(SQR(k+m)/SQR(k+m))); //a/(SQR(k+m)/SQR(k+m)) = 1
printf("1.0*a/(SQR(k+m)/SQR(k+m)) = %.2f\n",1.0*a/(SQR(k+m)/SQR(k+m)));//1.0*a/(SQR(k+m)/SQR(k+m)) = 1.43 return ;
} /*
SQR(k+m) = 5
SQR(k+m)/SQR(k+m) = 7
SQR(k+m)/SQR(k+m) = 7.00
SQR(k+m)/SQR(k+m) = 7.50
a/(SQR(k+m)/SQR(k+m)) = 1
1.0*a/(SQR(k+m)/SQR(k+m)) = 1.43 以上测试得:
SQR(k+m)/SQR(k+m)表达式展开替换为: 1.0*SQR(k+m)/SQR(k+m)表达式展开替换为:
k+m*k+m/k+m*k+m = 2 + 1*2 +1/2 +1*2 + 1 1.0*k+m*k+m/k+m*k+m = 1.0*2 + 1*2 +1/2 +1*2 + 1
= 2 + 1 + 1/2 + 3 + 1 = 2.0 + 1 + 1/2 + 3 + 1
= 3 + 1/2 + 4 = 2.0 + 1 + 0 + 3 + 1
= 3 + 0 + 4 = 2.0 + 5
=7 = 7.0
1.0*(SQR(k+m)/SQR(k+m))表达式展开替换为:
= 1.0*(k+m*k+m/k+m*k+m )
= 1.0 * 7
= 7.0
SQR(k+1.0*m)/SQR(k+m) =
= k+1.0*m*k+1.0*m/k+m*k+m
= 2 + 1.0*2 + 1.0/2 + 1*2 + 1
= 2 + 2.0 + 0.5 + 2 + 1
= 2.0 + 2.0 + 0.5 + 2.0 + 1.0
= 7.50
总结:
define定义的宏变量(以及相应的头文件等),在编译前进行代码替换,注意仅仅是代码替换,并不涉及到运算符等的操作,因为运算符操
作是在编译阶段进行的
define 只是定义而已,在编择时只是进行简单代换而已,并不经过任何其他的处理(例如:加减等运算或者是附加括号等的结合性干预)
表达式中的运算是按“块”来分步运算的,各个块中按照块中各成员精度最高者运算并按照最高精度得出结果。 */
#define与运算精度问题探究
扩展阅读:C/C++源代码到可执行程序的过程详解