/*
Compare two float arrays element-by-element for exact equality.
Floating‑point values must match bit-for-bit; even tiny rounding
differences will cause inequality. This is stricter than using
a tolerance-based comparison.
*/
#include <stdio.h>
#include <stdbool.h>
/*
Compares two float arrays and returns true only if all elements
are exactly equal. This uses direct == comparison, which checks
for exact binary equality.
*/
bool compareFloatArraysExact(const float *a, const float *b, size_t length) {
// If length is zero, treat arrays as equal
if (length == 0) {
return true;
}
// Compare each element directly
for (size_t i = 0; i < length; i++) {
if (a[i] != b[i]) {
return false; // Found mismatch → arrays differ
}
}
// All elements matched exactly
return true;
}
/*
Prints the comparison result.
*/
void printComparison(bool result) {
if (result) {
printf("Arrays are exactly equal.\n");
} else {
printf("Arrays differ.\n");
}
}
int main(void) {
// Example arrays
float floatArr1[] = {
12314.9872f,
3.14f,
12387.91834f,
8873.579013f
};
float floatArr2[] = {
12314.9872f,
3.14f,
12387.91834f,
8873.579013f
};
// Determine array length
size_t length = sizeof(floatArr1) / sizeof(floatArr1[0]);
// Perform exact comparison
bool result = compareFloatArraysExact(floatArr1, floatArr2, length);
// Output result
printComparison(result);
return 0;
}
/*
run:
Arrays are exactly equal.
*/