assertAlmostEquals

import { assertAlmostEquals } from "jsr:@ende/assert";

function

function assertAlmostEquals(actual: number, expected: number, tolerance?: number, msg?: string): void

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.

The default tolerance is one hundred thousandth of a percent of the expected value.

actual: number

The actual value to compare.

expected: number

The expected value to compare.

tolerance?: number

The tolerance to consider the values almost equal. The default is one hundred thousandth of a percent of the expected value.

msg?: string

The optional message to include in the error.

Example: Usage

import { assertAlmostEquals } from "@ende/assert";

assertAlmostEquals(0.01, 0.02); // Throws
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
assertAlmostEquals(0.1 + 0.2, 0.3, 1e-16); // Doesn't throw
assertAlmostEquals(0.1 + 0.2, 0.3, 1e-17); // Throws

Source