Repository: denoland/std (TypeScript)

Issue:
`assertAlmostEquals` failed when comparing 2 negative numbers
**Describe the bug**

When comparing 2 negative numbers with `assertAlmostEquals`, it failed due to an incorrect calculation of the default tolerance. This bug only occurs in `@std/assert` pre-release version `1.0.0-rc.1`.

**Steps to Reproduce**

```ts
import { assertAlmostEquals } from "@std/assert";

Deno.test("test", () => {
  assertAlmostEquals(-9.888977057628651, -9.888977058);
});
```

`@std/assert@^1.0.0-rc.1`

```sh
test => ./mod.ts:3:6
error: AssertionError: Expected actual: "-9.88897705762865e+0" to be close to "-9.888977058e+0": delta "3.7134917363346176e-10" is greater than "-9.888977058e-7".
  throw new AssertionError(
        ^
    at assertAlmostEquals (https://jsr.io/@std/assert/1.0.0-rc.1/assert_almost_equals.ts:51:9)
    at file:///D:/coding/project/javascript/deno/console/mod.ts:4:3
```

**Expected behavior**

The test will pass like the lastest non-prerelease version.

`@std/assert@^0.226.0`

```sh
test ... ok (2ms)
````

**Environment**

<!-- please complete the following information -->

- OS:
- deno version: 1.44.1
- std version: `@std/assert@^1.0.0-rc.1`



Discussion:
Here's a standalone test that can be simply be copied + pasted and run locally to reproduce the juxtaposition:

<details>
<summary>module</summary>

`test.ts`:
```ts
// v0.226.0 - https://github.com/denoland/deno_std/blob/release-2024.06.03/assert/deno.json#L3
import { assertAlmostEquals as assertAlmostEquals_0_226_0 } from "https://raw.githubusercontent.com/denoland/deno_std/release-2024.06.03/assert/assert_almost_equals.ts";

// v1.0.0-rc.1 - https://github.com/denoland/deno_std/blob/release-2024.06.06/assert/deno.json#L3
import { assertAlmostEquals as assertAlmostEquals_1_0_0_rc_1 } from "https://raw.githubusercontent.com/denoland/deno_std/release-2024.06.06/assert/assert_almost_equals.ts";

const positiveActual = 9.888977057628651;
const positiveExpected = 9.888977058;
const negativeActual = -9.888977057628651;
const negativeExpected = -9.888977058;

Deno.test("0.226.0 - positive", () => {
  assertAlmostEquals_0_226_0(positiveActual, positiveExpected);
});

Deno.test("0.226.0 - negative", () => {
  assertAlmostEquals_0_226_0(negativeActual, negativeExpected);
});

Deno.test("1.0.0-rc.1 - positive", () => {
  assertAlmostEquals_1_0_0_rc_1(positiveActual, positiveExpected);
});

Deno.test("1.0.0-rc.1 - negative", () => {
  assertAlmostEquals_1_0_0_rc_1(negativeActual, negativeExpected);
});

```
</details>

<details>
<summary>output</summary>

```txt
% deno --version
deno 1.44.1 (release, aarch64-apple-darwin)
v8 12.6.228.3
typescript 5.4.5

% deno test test.ts
running 4 tests from ./test.ts
0.226.0 - positive ... ok (0ms)
0.226.0 - negative ... ok (0ms)
1.0.0-rc.1 - positive ... ok (0ms)
1.0.0-rc.1 - negative ... FAILED (0ms)

 ERRORS

1.0.0-rc.1 - negative => ./test.ts:24:6
error: AssertionError: Expected actual: "-9.88897705762865e+0" to be close to "-9.888977058e+0": delta "3.7134917363346176e-10" is greater than "-9.888977058e-7".
  throw new AssertionError(
        ^
    at assertAlmostEquals (https://raw.githubusercontent.com/denoland/deno_std/release-2024.06.06/assert/assert_almost_equals.ts:51:9)
    at file:///Users/deno/test.ts:25:3

 FAILURES

1.0.0-rc.1 - negative => ./test.ts:24:6

FAILED | 3 passed | 1 failed (1ms)

error: Test failed
```
</details>




Patch:
diff --git a/assert/assert_almost_equals.ts b/assert/assert_almost_equals.ts
index 1514ddf1fba1..daac5aa958da 100644
--- a/assert/assert_almost_equals.ts
+++ b/assert/assert_almost_equals.ts
@@ -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;


Test patch:
diff --git a/assert/assert_almost_equals_test.ts b/assert/assert_almost_equals_test.ts
index 24a56cc7d994..3989c94d25f7 100644
--- a/assert/assert_almost_equals_test.ts
+++ b/assert/assert_almost_equals_test.ts
@@ -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", () => {