1. Comparison of Boolean variables:
If (FLAG) if (! Flag)
Use non-logical! Operator, because it is suitable for Boolean variables.
2. Comparison of integer variables:
If (value = 0) if (value! = 0)
Use normal = and! = Operator.
3. Comparison of floating point variables:
If (x = 0.0) // The error may be extremely large. If (x> =-epsinon) & (x <= epsinon ))
Avoid using "=" or "! To compare the value with the number, try to convert it to the "> =" or "<=" format, because the truncation error of the floating point variable is very likely. For example, the result of 1/3*3 is 0. 9999999 ..., if we compare it with 1 again, of course there will be a problem!
I have a deep understanding of this point. I used the following method to solve this problem when I did a computational geometric problem:
If (ABS (x) <EPS) x = 0;
Since X is close to 0, set it to 0 directly.
4. Comparison of pointer variables:
If (P = NULL) // Explicit Comparison between P and null emphasizes that p is the pointer variable if (P! = NULL)
Refer to self-developed high quality programming guide-C ++/C language 4.3if statement