Skip to content

Commit 2dba7f4

Browse files
Fix SpotBugs FE_FLOATING_POINT_EQUALITY violations (#4362)
* Fix SpotBugs FE_FLOATING_POINT_EQUALITY warnings Fixed floating point equality checks by replacing `==` and `!=` with `Float.compare` and `Double.compare` or `isZero` checks across multiple files. Updated `generate-quality-report.py` to enforce this rule in CI. Fixed files: - CodenameOne/src/com/codename1/ui/geom/GeneralPath.java - CodenameOne/src/com/codename1/ui/plaf/CSSBorder.java - CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java - CodenameOne/src/com/codename1/charts/models/XYSeries.java - CodenameOne/src/com/codename1/charts/models/XYValueSeries.java - CodenameOne/src/com/codename1/location/Location.java - CodenameOne/src/com/codename1/ui/Command.java - CodenameOne/src/com/codename1/ui/Stroke.java - CodenameOne/src/com/codename1/ui/layouts/LayeredLayout.java - CodenameOne/src/com/codename1/ui/plaf/Border.java * Fix SpotBugs FE_FLOATING_POINT_EQUALITY warnings Fixed floating point equality checks by replacing `==` and `!=` with `Float.compare` and `Double.compare` or `isZero` checks across multiple files. Updated `generate-quality-report.py` to enforce this rule in CI. Fixed files: - CodenameOne/src/com/codename1/ui/geom/GeneralPath.java - CodenameOne/src/com/codename1/ui/plaf/CSSBorder.java - CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java - CodenameOne/src/com/codename1/charts/models/XYSeries.java - CodenameOne/src/com/codename1/charts/models/XYValueSeries.java - CodenameOne/src/com/codename1/location/Location.java - CodenameOne/src/com/codename1/ui/Command.java - CodenameOne/src/com/codename1/ui/Stroke.java - CodenameOne/src/com/codename1/ui/layouts/LayeredLayout.java - CodenameOne/src/com/codename1/ui/plaf/Border.java * Fix SpotBugs FE_FLOATING_POINT_EQUALITY warnings Refactored to use `com.codename1.util.MathUtil.compare` for floating point comparisons to avoid using `Float.compare` and `Double.compare` which are not available in CLDC 1.1. Added `compare` methods to `com.codename1.util.MathUtil`. Fixed `FE_FLOATING_POINT_EQUALITY` warnings in: - CodenameOne/src/com/codename1/ui/geom/GeneralPath.java - CodenameOne/src/com/codename1/ui/plaf/CSSBorder.java - CodenameOne/src/com/codename1/ui/plaf/RoundRectBorder.java - CodenameOne/src/com/codename1/charts/models/XYSeries.java - CodenameOne/src/com/codename1/charts/models/XYValueSeries.java - CodenameOne/src/com/codename1/location/Location.java - CodenameOne/src/com/codename1/ui/Command.java - CodenameOne/src/com/codename1/ui/Stroke.java - CodenameOne/src/com/codename1/ui/layouts/LayeredLayout.java - CodenameOne/src/com/codename1/ui/plaf/Border.java Updated `generate-quality-report.py` to enforce this rule in CI. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 53ca24c commit 2dba7f4

File tree

12 files changed

+97
-31
lines changed

12 files changed

+97
-31
lines changed

.github/scripts/generate-quality-report.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,8 @@ def main() -> None:
766766
"EQ_CHECK_FOR_OPERAND_NOT_COMPATIBLE_WITH_THIS",
767767
"IA_AMBIGUOUS_INVOCATION_OF_INHERITED_OR_OUTER_METHOD",
768768
"RpC_REPEATED_CONDITIONAL_TEST",
769-
"ES_COMPARING_PARAMETER_STRING_WITH_EQ"
769+
"ES_COMPARING_PARAMETER_STRING_WITH_EQ",
770+
"FE_FLOATING_POINT_EQUALITY"
770771
}
771772
violations = [
772773
f for f in spotbugs.findings

CodenameOne/src/com/codename1/charts/models/XYSeries.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void remove(int index) {
188188
XYEntry<Double, Double> removedEntry = mXY.removeByIndex(index);
189189
double removedX = removedEntry.getKey();
190190
double removedY = removedEntry.getValue();
191-
if (removedX == mMinX || removedX == mMaxX || removedY == mMinY || removedY == mMaxY) {
191+
if (com.codename1.util.MathUtil.compare(removedX, mMinX) == 0 || com.codename1.util.MathUtil.compare(removedX, mMaxX) == 0 || com.codename1.util.MathUtil.compare(removedY, mMinY) == 0 || com.codename1.util.MathUtil.compare(removedY, mMaxY) == 0) {
192192
initRange();
193193
}
194194
}

CodenameOne/src/com/codename1/charts/models/XYValueSeries.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void add(double x, double y) {
9494
public void remove(int index) {
9595
super.remove(index);
9696
double removedValue = mValue.remove(index);
97-
if (removedValue == mMinValue || removedValue == mMaxValue) {
97+
if (com.codename1.util.MathUtil.compare(removedValue, mMinValue) == 0 || com.codename1.util.MathUtil.compare(removedValue, mMaxValue) == 0) {
9898
initRange();
9999
}
100100
}

CodenameOne/src/com/codename1/location/Location.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ public int compare(Location o1, Location o2) {
294294
*/
295295
boolean equalsLatLng(Location l) {
296296

297-
return l != null && l.latitude == latitude && l.longitude == longitude;
297+
return l != null && com.codename1.util.MathUtil.compare(l.latitude, latitude) == 0 && com.codename1.util.MathUtil.compare(l.longitude, longitude) == 0;
298298

299299
}
300300
}

CodenameOne/src/com/codename1/ui/Command.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,12 @@ public boolean equals(Object obj) {
275275
if (((Command) obj).command == null) {
276276
return obj.getClass() == getClass() && command == null &&
277277
((Command) obj).icon == icon && ((Command) obj).commandId == commandId &&
278-
((Command) obj).materialIcon == materialIcon && ((Command) obj).materialIconSize == materialIconSize &&
278+
((Command) obj).materialIcon == materialIcon && com.codename1.util.MathUtil.compare(((Command) obj).materialIconSize, materialIconSize) == 0 &&
279279
(Objects.equals(clientProperties, ((Command) obj).clientProperties));
280280
} else {
281281
return obj.getClass() == getClass() && ((Command) obj).command.equals(command) &&
282282
((Command) obj).icon == icon && ((Command) obj).commandId == commandId &&
283-
((Command) obj).materialIcon == materialIcon && ((Command) obj).materialIconSize == materialIconSize &&
283+
((Command) obj).materialIcon == materialIcon && com.codename1.util.MathUtil.compare(((Command) obj).materialIconSize, materialIconSize) == 0 &&
284284
(Objects.equals(clientProperties, ((Command) obj).clientProperties));
285285
}
286286
}

CodenameOne/src/com/codename1/ui/Stroke.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void setMiterLimit(float miterLimit) {
227227
public boolean equals(Object obj) {
228228
if (obj instanceof Stroke) {
229229
Stroke s = (Stroke) obj;
230-
return (s.miterLimit == miterLimit && s.capStyle == capStyle && s.joinStyle == joinStyle && s.lineWidth == lineWidth);
230+
return (com.codename1.util.MathUtil.compare(s.miterLimit, miterLimit) == 0 && s.capStyle == capStyle && s.joinStyle == joinStyle && com.codename1.util.MathUtil.compare(s.lineWidth, lineWidth) == 0);
231231
}
232232
return false;
233233
}

CodenameOne/src/com/codename1/ui/geom/GeneralPath.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2224,7 +2224,7 @@ private static float[] intersectLineWithRectAsHash(float x1, float y1, float x2,
22242224
float minX = Math.min(x1, x2);
22252225
float maxX = Math.max(x1, x2);
22262226
int i = 0;
2227-
if (dx == 0) {
2227+
if (isZero(dx)) {
22282228
if (ry1 > minY && ry1 < maxY) {
22292229
num++;
22302230
x[i++] = ry1;
@@ -2255,7 +2255,7 @@ private static float[] intersectLineWithRectAsHash(float x1, float y1, float x2,
22552255
out[8] = num;
22562256

22572257

2258-
} else if (dy == 0) {
2258+
} else if (isZero(dy)) {
22592259
if (rx1 > minX && rx1 < maxX) {
22602260
num++;
22612261
x[i++] = rx1;
@@ -2344,8 +2344,8 @@ public static int solveQuad(double[] eqn, double[] res) {
23442344
double b = eqn[1];
23452345
double c = eqn[0];
23462346
int rc = 0;
2347-
if (a == 0.0) {
2348-
if (b == 0.0) {
2347+
if (isZero(a)) {
2348+
if (isZero(b)) {
23492349
return -1;
23502350
}
23512351
res[rc++] = -c / b;
@@ -2358,7 +2358,7 @@ public static int solveQuad(double[] eqn, double[] res) {
23582358
d = Math.sqrt(d);
23592359
res[rc++] = (-b + d) / (a * 2.0);
23602360
// d != 0.0
2361-
if (d != 0.0) {
2361+
if (!isZero(d)) {
23622362
res[rc++] = (-b - d) / (a * 2.0);
23632363
}
23642364
}
@@ -2374,7 +2374,7 @@ public static int solveQuad(double[] eqn, double[] res) {
23742374
*/
23752375
public static int solveCubic(double[] eqn, double[] res) {
23762376
double d = eqn[3];
2377-
if (d == 0) {
2377+
if (isZero(d)) {
23782378
return solveQuad(eqn, res);
23792379
}
23802380
double a = eqn[2] / d;
@@ -2453,7 +2453,7 @@ public static int crossLine(double x1, double y1, double x2, double y2, double x
24532453
if ((x < x1 && x < x2) ||
24542454
(x > x1 && x > x2) ||
24552455
(y > y1 && y > y2) ||
2456-
(x1 == x2)) {
2456+
(isZero(x1 - x2))) {
24572457
return 0;
24582458
}
24592459

@@ -3025,14 +3025,14 @@ int cross(double[] res, int rc, double py1, double py2) {
30253025
}
30263026
// CURVE-START
30273027
if (t < DELTA) {
3028-
if (py1 < 0.0 && (bx != 0.0 ? bx : ax - bx) < 0.0) {
3028+
if (py1 < 0.0 && (!isZero(bx) ? bx : ax - bx) < 0.0) {
30293029
cross--;
30303030
}
30313031
continue;
30323032
}
30333033
// CURVE-END
30343034
if (t > 1 - DELTA) {
3035-
if (py1 < ay && (ax != bx ? ax - bx : bx) > 0.0) {
3035+
if (py1 < ay && (!isZero(ax - bx) ? ax - bx : bx) > 0.0) {
30363036
cross++;
30373037
}
30383038
continue;
@@ -3060,10 +3060,10 @@ int solvePoint(double[] res, double px) {
30603060

30613061
int solveExtrem(double[] res) {
30623062
int rc = 0;
3063-
if (Ax != 0.0) {
3063+
if (!isZero(Ax)) {
30643064
res[rc++] = -Bx / (Ax + Ax);
30653065
}
3066-
if (Ay != 0.0) {
3066+
if (!isZero(Ay)) {
30673067
res[rc++] = -By / (Ay + Ay);
30683068
}
30693069
return rc;
@@ -3130,14 +3130,14 @@ int cross(double[] res, int rc, double py1, double py2) {
31303130
}
31313131
// CURVE-START
31323132
if (t < DELTA) {
3133-
if (py1 < 0.0 && (bx != 0.0 ? bx : (cx != bx ? cx - bx : ax - cx)) < 0.0) {
3133+
if (py1 < 0.0 && (!isZero(bx) ? bx : (!isZero(cx - bx) ? cx - bx : ax - cx)) < 0.0) {
31343134
cross--;
31353135
}
31363136
continue;
31373137
}
31383138
// CURVE-END
31393139
if (t > 1 - DELTA) {
3140-
if (py1 < ay && (ax != cx ? ax - cx : (cx != bx ? cx - bx : bx)) > 0.0) {
3140+
if (py1 < ay && (!isZero(ax - cx) ? ax - cx : (!isZero(cx - bx) ? cx - bx : bx)) > 0.0) {
31413141
cross++;
31423142
}
31433143
continue;

CodenameOne/src/com/codename1/ui/layouts/LayeredLayout.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3256,7 +3256,7 @@ public Inset changeReference(Container parent, Component newRef, float pos) {
32563256
// // This could potentially affect the opposite inset if it is a percentage
32573257
// referenceComponent(newRef).referencePosition(pos);
32583258
//} else {
3259-
if (newRef != referenceComponent || pos != referencePosition) {
3259+
if (newRef != referenceComponent || com.codename1.util.MathUtil.compare(pos, referencePosition) != 0) {
32603260
// This may potentially affect both this inset
32613261
// and the opposite inset if it is either flexible or
32623262
// percent.
@@ -3437,7 +3437,7 @@ public Inset translatePixels(int delta, boolean preferMM, Container parent) {
34373437
return this;
34383438
}
34393439
float percentDelta = delta / relH * 100f;
3440-
if (percentDelta == Float.NEGATIVE_INFINITY || percentDelta == Float.POSITIVE_INFINITY) {
3440+
if (com.codename1.util.MathUtil.compare(percentDelta, Float.NEGATIVE_INFINITY) == 0 || com.codename1.util.MathUtil.compare(percentDelta, Float.POSITIVE_INFINITY) == 0) {
34413441
percentDelta = 0f;
34423442
}
34433443
value += percentDelta;
@@ -3450,7 +3450,7 @@ public Inset translatePixels(int delta, boolean preferMM, Container parent) {
34503450
}
34513451
float percentDelta = delta / relH * 100f;
34523452
//System.out.println("percentDelta="+percentDelta);
3453-
if (percentDelta == Float.NEGATIVE_INFINITY || percentDelta == Float.POSITIVE_INFINITY) {
3453+
if (com.codename1.util.MathUtil.compare(percentDelta, Float.NEGATIVE_INFINITY) == 0 || com.codename1.util.MathUtil.compare(percentDelta, Float.POSITIVE_INFINITY) == 0) {
34543454
percentDelta = 0f;
34553455
}
34563456
value += percentDelta;

CodenameOne/src/com/codename1/ui/plaf/Border.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ public boolean equals(Object obj) {
11021102

11031103
boolean v = ((themeColors == b.themeColors) &&
11041104
(type == b.type) &&
1105-
(thickness == b.thickness) &&
1105+
(com.codename1.util.MathUtil.compare(thickness, b.thickness) == 0) &&
11061106
(colorA == b.colorA) &&
11071107
(colorB == b.colorB) &&
11081108
(colorC == b.colorC) &&

CodenameOne/src/com/codename1/ui/plaf/CSSBorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ ScalarUnit copy() {
13451345
public boolean equals(Object obj) {
13461346
if (obj instanceof ScalarUnit) {
13471347
ScalarUnit u = (ScalarUnit) obj;
1348-
return u.value == 0 && value == 0 || u.value == value && u.type == type;
1348+
return com.codename1.util.MathUtil.compare(u.value, 0f) == 0 && com.codename1.util.MathUtil.compare(value, 0f) == 0 || com.codename1.util.MathUtil.compare(u.value, value) == 0 && u.type == type;
13491349
}
13501350
return false;
13511351
}

0 commit comments

Comments
 (0)