C- Floating point precision

后端 未结 4 1243
终归单人心
终归单人心 2020-11-28 16:25

I have a program:

int main() 
{   
        float f = 0.0f;  
        int i;  

        for (i = 0 ; i < 10 ; i++) 
                f = f + 0.1f; 

                


        
4条回答
  •  心在旅途
    2020-11-28 16:51

    This is equivelent to adding 0.33 together 3 times (0.99) and wondering why it is not equal to 1.0.

    You may wish to read What Every Programmer Should Know About Floating Point Arithmetic

提交回复
热议问题