简体   繁体   中英

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

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

Result is of value type and is never changing. You have to use pointers for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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