Explain this floating point behavior

前端 未结 6 2183
野的像风
野的像风 2020-12-21 18:30

Please explain why the following pieces of code behave differently.

#include
int main(){
 float a=0.1;
 if(a<0.1)
  printf(\"less\");
 else         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-21 18:51

    I would recommend you reading What Every Computer Scientist Should Know About Floating-Point Arithmetic. Basically when working with floating point numbers you should always check if a number is equal, smaller than or greater than some other number given some precision (epsilon) that you have defined.

提交回复
热议问题