Review generated sensor comparison

from Floating-point arithmetic
Node 24 intermediate 10 min 3 issues to find

Review this generated implementation before it compares a large telemetry batch.

Compare finite readings by unique sensor ID, preserve expected-reading order, report missing sensors, and use absolute tolerance 0.01 plus relative tolerance 0.000001.

JavaScript
function compareReadings(expectedReadings, actualReadings) {
  const comparisons = [];

  for (const expected of expectedReadings) {
    const actual = actualReadings.find((item) => item.id === expected.id);
    const difference = Math.abs(actual.value - expected.value);
    const matches = difference < Number.EPSILON;
    comparisons.push({
      id: expected.id,
      expected: expected.value,
      actual: actual.value,
      difference,
      matches,
    });
  }

  const matched = comparisons.filter((item) => item.matches);
  return {
    comparisons,
    matched: matched.length,
    mismatched: comparisons.length - matched.length,
  };
}

generated code is illustrative, not from any one model

Open in playground
Report an error