循环时浮点

时间:2015-09-29 21:29:56

标签: c floating-point

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);
}

我的while循环是无限的,它永远不会结束,但它应该以41结束,因为当我测试41时,它等于0.99999404

2 个答案:

答案 0 :(得分:3)

result永远不会在循环中发生变化,因此没有理由相信,一旦启动,循环将永远结束。

答案 1 :(得分:-1)

结果属于值类型且永远不会更改。你必须使用指针。

相关问题