C中的无限循环

发布于 2025-01-02 05:12:48 字数 116 浏览 0 评论 0原文

初学者在这里。 为什么这是一个无限循环?

for (p = 0; p < 5; p += 0.5)
{
    printf("p=%2.2f\n",p);
}

Beginner here.
Why is this an endless loop ?

for (p = 0; p < 5; p += 0.5)
{
    printf("p=%2.2f\n",p);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

昵称有卵用 2025-01-09 05:12:48

您会看到无限循环,因为您的 p 是整数类型(例如 int)。无论将 0.5 添加到 int 多少次,它都将保持为 0,因为 int 会截断分配给它的 double/fp 值。换句话说,它相当于在每个步骤上添加零的循环。

如果您将 p 设为 floatdouble,您的问题就会消失。

编辑(由 Oli Charlesworth 的评论建议)

值得注意的是,不鼓励使用浮点数和双精度数来控制循环,因为结果并不总是像示例中那样干净。将步长从 0.5(2 的负 1 次方)更改为 0.1(不是 2 的负整数次方)会改变您看到的结果以一种相当意想不到的方式。

如果您需要按非整数步骤进行迭代,您应该考虑使用这个简单的模式:

// Loop is controlled by an integer counter
for (int i = 0 ; i != 10 ; i++) {
    // FP value is calculated by multiplying the counter by the intended step:
    double p = i * 0.5;
    // p is between 0 and 4.5, inclusive
}

You see an endless loop because your p is of an integral type (e.g. an int). No matter how many times you add 0.5 to an int, it would remain 0, because int truncates double/fp values assigned to it. In other words, it is equivalent to a loop where you add zero on each step.

If you make p a float or a double, your problem would go away.

EDIT (Suggested by Oli Charlesworth's comment)

It is worth noting that using floats and doubles to control loops is discouraged, because the results are not always as clean as in your example. Changing the step from 0.5 (which is 2 to the negative power of 1) to 0.1 (which is not an integral negative power of 2) would change the results that you see in a rather unexpected way.

If you need to iterate by a non-integer step, you should consider using this simple pattern:

// Loop is controlled by an integer counter
for (int i = 0 ; i != 10 ; i++) {
    // FP value is calculated by multiplying the counter by the intended step:
    double p = i * 0.5;
    // p is between 0 and 4.5, inclusive
}
谁对谁错谁最难过 2025-01-09 05:12:48

我认为这取决于 p 的声明方式。如果是整数类型,p将始终为0(因为0 + 0.5的结果每次都会被截断为0),因此for 永远不会停止。

I think it depends on how p is declared. If it's an integer type, p will always be 0 (because the result of 0 + 0.5 will be truncated to 0 every time) so the for will never stop.

半葬歌 2025-01-09 05:12:48

类型转换问题,float/double 在分配给整数类型时丢失精度。

PS 在条件测试中使用 float/double 确实是一个非常坏主意。并非计算机中的所有浮点数都是准确的。

a type conversion problem, float/double lost precision when assigned to an integer type.

P.S. It is really a very bad idea to use float/double in condition test. Not all floating point numbers in computers are accurate.

饮湿 2025-01-09 05:12:48

如果 pfloatdouble,则代码没有问题,循环将终止。

如果 p 是整数,则代码的行为未定义,因为 printf() 中的格式说明符是错误的。

If p is a float or a double, there's nothing wrong with the code, and the loop will terminate.

If p is integer, the behaviour of the code is undefined since the format specifier in printf() is wrong.

寄人书 2025-01-09 05:12:48

当您将双精度常数添加到整数变量时,双精度常数“变成”整数。 0.5 就变成了 0。所以你在 p 上加上 0。

when you add a double constant to integer variable, the double constant "becomes" integer. 0.5 becomes just 0. So you add 0 to p.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文