审查生成的传感器比较代码

来自 浮点数运算
Node 24 进阶 10分钟 找出 3处问题

请在这段生成代码比较大型遥测批次前进行审查。

按唯一传感器 ID 比较有限读数,保留预期读数顺序,报告缺失传感器,并使用 0.01 绝对容差与 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,
  };
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误