C 运行for循环时的奇怪输出
我有以下声明:C 运行for循环时的奇怪输出,c,for-loop,types,C,For Loop,Types,我有以下声明: const double s=1529340.57; double f=1-1/1200; for(j=1;j<=384;j++){ printf("Age %3d month %2d you have $%.2lf\n", (j+816) / 12, (j+816)%12, s*pow(f,j-1)-4000*((pow(f,j-1)-f)/(f-1)-1)); } 等等 但是,我获得了以下输出: Age 68 month 1 you have $-1.#J
const double s=1529340.57;
double f=1-1/1200;
for(j=1;j<=384;j++){
printf("Age %3d month %2d you have $%.2lf\n", (j+816) / 12, (j+816)%12, s*pow(f,j-1)-4000*((pow(f,j-1)-f)/(f-1)-1));
}
等等
但是,我获得了以下输出:
Age 68 month 1 you have $-1.#J
Age 68 month 2 you have $-1.#J
Age 68 month 3 you have $-1.#J
你知道为什么吗?
谢谢 尝试
双f=1-1/1200.0代码>。正如Jean-François Fabre已经提到的,在您的代码版本中,1-1/1200不是双精度的。1200.0强制编译器将您的公式处理为十进制。f=1-1/1200
就像f=1
谢谢!你能给我一些提示,如何获得这个值吗?这里太晚了,所以我可能弄错了,但是68岁1个月应该是1529340.57吗?这就是我所得到的,至少我所做的所有改变是使算术更显式地使用浮点。
Age 68 month 1 you have $-1.#J
Age 68 month 2 you have $-1.#J
Age 68 month 3 you have $-1.#J