问题
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