美文网首页
comparing floating point with ==

comparing floating point with ==

作者: 嵌入式昆仑 | 来源:发表于2019-11-05 10:27 被阅读0次

comparing floating point with == or != is unsafe

因为float数据类型存储的原因,导致编译器对比较时刻的精度不能保证准确的。

建议

if(a == 0.3f)

修改为

if((a-0.3f)<=0.001)

或者用函数,以及宏定义(或常量)进行一个包装

相关文章

网友评论

      本文标题:comparing floating point with ==

      本文链接:https://www.haomeiwen.com/subject/cllsbctx.html