[英]Not sure why C prints out NaN
I was coding up a short program to compute right endpoints for a given graph (not shown here), so to avoid tedious computation, I decided to write up a short program to do the work for me. 我当时正在编写一个简短的程序来为给定的图形(此处未显示)计算正确的端点,因此为了避免繁琐的计算,我决定编写一个简短的程序来为我完成工作。 Yet my C program just prints out nan.
但是我的C程序只是打印出nan。 I am very rusty on C, but I am not sure why I am getting NaN.
我对C非常生锈,但是我不确定为什么会得到NaN。
#include <stdio.h>
int main(void) {
int x;
float y, z;
for (x = 0; x <= 8; x++) {
y += 10.0 - (12.0 + (float)x) / 4.0;
printf("%f\n", y);
}
z = 0.5 * y;
printf("%f\n", z);
return 0;
}
y = 10.0 - (12.0 + (float)x) / 4.0;
Followed by 其次是
y = y+1;
This makes sense else you have y
uninitialized which leads to undefined behavior because the value of y
is undeterminate. 这很有意义,否则您将
y
初始化为未定义状态,这会导致未定义的行为,因为y
的值不确定。 During declaration you can initialize y
and use +=
operator. 在声明期间,您可以初始化
y
并使用+=
运算符。
Like 喜欢
float y = 0;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.