Make sure c++ decimal comparison is correct

a 夏天 提交于 2019-12-25 02:57:33

问题


I have two double variable. double a = 0.10000, double b = 0.1. How can I make sure the comparison (a == b) is always true ?


回答1:


If you are being paranoid about using == on doubles or floats (which you should be) you can always check that they are close within a small tolerance.

bool same = fabs(a-b) < 0.000001;


来源:https://stackoverflow.com/questions/29522171/make-sure-c-decimal-comparison-is-correct

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!