概述
因为如果a与b差不多时,得出的是大于呢?还是小于?
所以只能用abs(a-b)<=0.0000001,来判断a是否等于b,
参考:c语言中float为什么不能直接用==与0比较
Floating point math is not exact. Simple values like 0.2 cannot be precisely represented using binary floating point numbers, and the limited precision of floating point numbers means that slight changes in the order of operations can change the result. Different compilers and CPU architectures store temporary results at different precisions, so results will differ depending on the details of your environment. If you do a calculation and then compare the results against some expected value it is highly unlikely that you will get exactly the result you intended.
In other words, if you do a calculation and then do this comparison:
if (result == expectedResult)
then it is unlikely that the comparison will be true. If the comparison is true then it is probably unstable – tiny changes in the input values, compiler, or CPU may change the result and make the comparison be false.
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
最后
以上就是爱笑发夹为你收集整理的float 不能用==,>=,<= 参考:c语言中float为什么不能直接用==与0比较的全部内容,希望文章能够帮你解决float 不能用==,>=,<= 参考:c语言中float为什么不能直接用==与0比较所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复