【发布时间】:2018-01-08 15:42:53
【问题描述】:
我一定犯了一些严重的错误,但我正在计算 C 中 2880 * 12 / 512 的结果,即使我将结果保存在 double 变量中,它只是不保存剩下的。根据谷歌和任何其他计算器 2880 * 12 / 512 = 67.5 但是根据下面的程序它是 67.00。这是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
int main() {
double result;
result = 2880 * 12 / 512;
printf("result = %f\n", result);
return 0;
}
我正在编译名为 test.c 的文件,其中包含以下代码:gcc test.c -o test -lm。
我做错了什么?
【问题讨论】:
-
你最喜欢的教程没有介绍这个吗?
-
2880 * 12 / 512都是整数。你可能想要2880.0 * 12.0 / 512.0 -
OT:您显示的代码未使用来自
math.h和/或libm的任何内容。不需要包含/链接。 -
@alk — 和
<stdint.h>、<string.h>和<stdlib.h>也是多余的。