【发布时间】:2017-01-17 22:32:07
【问题描述】:
我有以下 C 程序:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <math.h>
int main() {
const int opt_count = 2;
int oc = 30;
int c = 900;
printf("%d %f\n", c, pow(oc, opt_count));
assert(c == (int)(pow(oc, opt_count)));
}
我在 Windows 8.1 上运行 MinGW。 Gcc 版本 4.9.3。我编译我的程序:
gcc program.c -o program.exe
当我运行它时,我得到这个输出:
$ program
900 900.000000
Assertion failed: c == (int)(pow(oc, opt_count)), file program.c, line 16
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
发生了什么事?我希望断言能够通过,因为 900 == 30^2。
谢谢!
编辑
我没有使用任何分数或小数。我只使用整数。
【问题讨论】:
-
如果将
%f替换为%.20f会发生什么? -
每个程序员的...等等等等
-
@Olaf 我知道计算机不能用二进制浮点表示 0.1。但它们当然可以完全代表 3、30、900 等。
-
@herugar5 那么中间计算呢?还有如何你知道
900可以精确表示吗?
标签: c math gcc mingw assertion