What is the significance of 0.0f when initializing (in C)?

前端 未结 7 520
被撕碎了的回忆
被撕碎了的回忆 2020-11-29 04:30

I\'ve seen code where people initialize float variables like this:

float num = 0.0f;

Is there a significant difference between this and jus

7条回答
  •  被撕碎了的回忆
    2020-11-29 05:09

    I don't see any reason to use this for initialization process. But, for operations involving floating point literals, this would be useful. For example;

    float a=0.43, b;
    b = 0.5*a + 2.56*a*a;
    

    Floating point literals without a suffix are considered doubles. So, for this computation, "a" will be type-casted to double and the final answer of RHS evaluation will be a double. During the assignment, the double value of RHS is casted to float and assigned to "b". This would degrade performance if the machine does not have double precision FPU. To avoid this and use float for entire computation. suffixes are used. For example,

    float a=0.43, b;
    b = 0.5f*a + 2.56f*a*a;
    

提交回复
热议问题