Make sure c++ decimal comparison is correct

2019-09-19 03:58发布

问题:

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;


标签: c++ double