萌新初来乍到,请多指教
变量x,y为double类型,变量a,b为int类型,数学式6*a*b/(7*x*y)的表达式为什么是 6/x*a*b/7/y
#include <stdio.h> int main(int argc, char *argv[]) { int a, b; //add volatile to mask gcc overdoing optimization volatile double x, y; double formula; #define critical_section_in ((int *)&a)[0] = 10; ((int *)&a)[1] = 20; *(double *)&((int *)&a)[2] = 1.25; *(double *)&((int *)&a)[4] = 12.17; #define critical_section_out //attention: while closed print_on gcc optimized the statment (7.0 * x * y) #define PRINT_ON #ifdef PRINT_ON printf("a = %p\n", &a); printf("a = %d\n", a); printf("b = %d\n", b); printf("(a->x) = %p-----", &((int *)&a)[2]); printf("x = %p\n", &x); printf("x = %lf\n", x); printf("y = %lf\n", y); #endif formula = 6 * a * b; formula = formula / (7.0 * x * y); printf("f = %.2lf\n", formula); return 0; }
[此贴子已经被作者于2021-10-12 13:53编辑过]
#include <stdio.h> int main(int argc, char *argv[]) { int a, b; //#define MASK_GCC_OVERDOING #ifdef MASK_GCC_OVERDOING //add volatile to mask gcc CT(compile-time) overdoing optimization /* runtime check */ volatile double x, y; #else /* compile-time check */ double x, y; #endif double formula; #define runtime_block__IN /* as gcc unable to check runtime code */ ((int *)&a)[0] = 10; ((int *)&a)[1] = 20; *(double *)&((int *)&a)[2] = 1.25; *(double *)&((int *)&a)[4] = 12.17; #define runtime_block__OUT #ifdef MASK_GCC_OVERDOING //attention: while closed print_on gcc optimized the expression (7.0 * x * y) #define PRINT_ON #ifdef PRINT_ON printf("a = %p\n", &a); printf("a = %d\n", a); printf("b = %d\n", b); printf("(a->x) = %p-----", &((int *)&a)[2]); /*while CT checking, until here gcc finally realized the exsitence of x, y*/ printf("x = %p\n", &x); printf("x = %lf\n", x); printf("y = %lf\n", y); #endif #endif /*MASK_GCC_OVERDOING*/ formula = 6 * a * b; /* CT: 7.0 * x * y = 0 so got the f = inf[divide zero] */ formula = formula / (7.0 * x * y); printf("f = %.2lf\n", formula); return 0; }