CS.FLOAT.EQCHECKTwo float or double values are compared using equality operators (==, !=). Vulnerability and riskAvoid equality checks on floating point types because of the possible inaccuracy of floating point calculations. The example below can lead to an infinite loop because x1 + 700 times ((x2 - x1) / 700) is not equal to x2, due to inaccuracy. Example 11 class Math { 2 public static double integral(MyFunction f, double x1, double x2) { 3 double x = x1; 4 double result = 0; 5 double step = (x2 - x1) / 700; 6 while (x != x2) { // defect, (x <= x2) should be used instead 7 result = result + f.valueFor(x) * step; 8 x = x + step; 9 } 10 return result; 11 } 12 } |