fix(assert): fix tolerance calculation when comparing negative values (#5019)

This commit is contained in:
Yoshiya Hinosawa 2024-06-10 20:09:27 +09:00 committed by GitHub
parent 2d5b5b42bc
commit a6781da213
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 3 additions and 1 deletions

View File

@ -40,7 +40,7 @@ export function assertAlmostEquals(
}
const delta = Math.abs(expected - actual);
if (tolerance === undefined) {
tolerance = isFinite(expected) ? expected * 1e-7 : 1e-7;
tolerance = isFinite(expected) ? Math.abs(expected * 1e-7) : 1e-7;
}
if (delta <= tolerance) {
return;

View File

@ -8,7 +8,9 @@ Deno.test("assertAlmostEquals() matches values within default precision range",
assertAlmostEquals(NaN, NaN);
assertAlmostEquals(Number.NaN, Number.NaN);
assertAlmostEquals(9e20, 9.0000000001e20);
assertAlmostEquals(-9e20, -9.0000000001e20);
assertAlmostEquals(1.000000001e-8, 1.000000002e-8);
assertAlmostEquals(-1.000000001e-8, -1.000000002e-8);
});
Deno.test("assertAlmostEquals() throws values outside default precision range", () => {