2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2023-07-13 07:04:30 +00:00
|
|
|
import { AssertionError } from "./assertion_error.ts";
|
|
|
|
|
|
|
|
/**
|
2023-12-06 17:13:38 +00:00
|
|
|
* Make an assertion that `actual` and `expected` are almost equal numbers
|
|
|
|
* through a given tolerance. It can be used to take into account IEEE-754
|
|
|
|
* double-precision floating-point representation limitations. If the values
|
|
|
|
* are not almost equal then throw.
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2024-06-03 06:55:39 +00:00
|
|
|
* The default tolerance is one hundred thousandth of a percent of the
|
|
|
|
* expected value.
|
|
|
|
*
|
2024-05-30 02:38:16 +00:00
|
|
|
* @example Usage
|
2024-09-19 23:29:31 +00:00
|
|
|
* ```ts ignore
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { assertAlmostEquals } from "@std/assert";
|
2023-07-13 07:04:30 +00:00
|
|
|
*
|
2023-12-06 17:13:38 +00:00
|
|
|
* assertAlmostEquals(0.01, 0.02); // Throws
|
2024-06-03 06:55:39 +00:00
|
|
|
* assertAlmostEquals(1e-8, 1e-9); // Throws
|
|
|
|
* assertAlmostEquals(1.000000001e-8, 1.000000002e-8); // Doesn't throw
|
|
|
|
* assertAlmostEquals(0.01, 0.02, 0.1); // Doesn't throw
|
2023-12-06 17:13:38 +00:00
|
|
|
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw
|
|
|
|
* assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws
|
2023-07-13 07:04:30 +00:00
|
|
|
* ```
|
2024-05-30 02:38:16 +00:00
|
|
|
*
|
|
|
|
* @param actual The actual value to compare.
|
|
|
|
* @param expected The expected value to compare.
|
2024-06-03 13:30:20 +00:00
|
|
|
* @param tolerance The tolerance to consider the values almost equal. The
|
|
|
|
* default is one hundred thousandth of a percent of the expected value.
|
2024-05-30 02:38:16 +00:00
|
|
|
* @param msg The optional message to include in the error.
|
2023-07-13 07:04:30 +00:00
|
|
|
*/
|
|
|
|
export function assertAlmostEquals(
|
|
|
|
actual: number,
|
|
|
|
expected: number,
|
2024-06-03 06:55:39 +00:00
|
|
|
tolerance?: number,
|
2023-07-13 07:04:30 +00:00
|
|
|
msg?: string,
|
2024-01-08 02:36:07 +00:00
|
|
|
) {
|
2023-07-13 07:04:30 +00:00
|
|
|
if (Object.is(actual, expected)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const delta = Math.abs(expected - actual);
|
2024-06-03 06:55:39 +00:00
|
|
|
if (tolerance === undefined) {
|
2024-06-10 11:09:27 +00:00
|
|
|
tolerance = isFinite(expected) ? Math.abs(expected * 1e-7) : 1e-7;
|
2024-06-03 06:55:39 +00:00
|
|
|
}
|
2023-07-13 07:04:30 +00:00
|
|
|
if (delta <= tolerance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const msgSuffix = msg ? `: ${msg}` : ".";
|
|
|
|
const f = (n: number) => Number.isInteger(n) ? n : n.toExponential();
|
|
|
|
throw new AssertionError(
|
|
|
|
`Expected actual: "${f(actual)}" to be close to "${f(expected)}": \
|
|
|
|
delta "${f(delta)}" is greater than "${f(tolerance)}"${msgSuffix}`,
|
|
|
|
);
|
|
|
|
}
|