简体   繁体   English

浮点while循环

[英]floating point while loop

int main()
{
    float x = 50;
    float y = 1/x;
    float result = y * x;

    float test = 41;
    float z = 1/test;
    float testRes = z * test;


    while (result == 1)
    {    
        x--;
    }

    printf("%f\n", x);
    printf("%.6f\n", testRes);
}

My while loop is infinite, it never ends, but it should end at 41 because when I test 41, it equals 0.99999404我的 while 循环是无限的,它永远不会结束,但它应该以 41 结束,因为当我测试 41 时,它等于 0.99999404

result内环路永远不会改变,因此没有理由相信,一旦它开始,循环永远不会结束。

Result is of value type and is never changing.结果是值类型并且永远不会改变。 You have to use pointers for that.您必须为此使用指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM