我正在尝试在 C 中使用浮点数执行除法。为了演示我的意思,您可以运行以下代码。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define TEST 20.0 / 65536.0
int main() {
float a = 11.147;
printf("%f\n", a);
float b = 20.0 / 65536.0;
printf("%f\n", b);
float c = a/b;
printf("%f\n", c);
int d = (int) c;
printf("%d\n", d);
float e = a/(float) TEST;
printf("%f\n",e);
printf("%f\n", TEST);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码给出了以下结果
11.147000
0.000305
**36526.492188**
36526
**0.000009**
0.000305
Run Code Online (Sandbox Code Playgroud)
我突出显示的值应该是相同的,因为它是相同公式的结果。唯一的区别是我用来#define定义后者的除数,这给出了错误的值。
我不知道为什么会发生这种情况,有人可以解释一下为什么我会得到这个结果吗?谢谢。
c ×1