/**
The epsilon exists because floating‑point numbers in Java (and in every mainstream
language) are inherently imprecise. Even simple arithmetic produces tiny rounding errors,
so comparing floats with == sometimes gives false mismatches
Two floats that should be equal often differ by a tiny amount,
so you treat them as “equal enough” if their difference is smaller than a tolerance.
*/
class Main {
static boolean floatArraysEqual(float[] a, float[] b, float epsilon) {
if (a.length != b.length) return false;
for (int i = 0; i < a.length; i++) {
// If the difference is larger than epsilon → treat as different
// If the difference is smaller → treat as equal
// You choose epsilon based on how much precision your application needs.
if (Math.abs(a[i] - b[i]) > epsilon) {
return false;
}
}
return true;
}
public static void main(String[] args) {
float[] floatArr1 = new float[]{12314.9872f, 3.14f, 12387.88372f, 8878.57913f};
float[] floatArr2 = new float[]{12314.9872f, 3.14f, 12387.88372f, 8878.57913f};
boolean equal = floatArraysEqual(floatArr1, floatArr2, 1e-6f);
System.out.println(equal);
}
}
/*
run:
true
*/