Skip to content

Commit 40871fa

Browse files
committed
float64equals
1 parent da609fa commit 40871fa

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ import {} from "https://gitee.com/masx200/leetcode-test/raw/master/mod.ts";
7979
```
8080

8181
```ts
82-
import {} from "https://github.com/masx200/leetcode-test/raw/master/mod.ts";
82+
import {} from "https://raw.githubusercontent.com/masx200/leetcode-test/master/mod.ts";
8383
```
8484

8585
```ts
@@ -93,7 +93,7 @@ import {} from "https://gitee.com/masx200/leetcode-test/raw/4.0.1/mod.ts";
9393
```
9494

9595
```ts
96-
import {} from "https://github.com/masx200/leetcode-test/raw/4.0.1/mod.ts";
96+
import {} from "https://raw.githubusercontent.com/masx200/leetcode-test/4.0.1/mod.ts";
9797
```
9898

9999
```ts

powx-n/test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { assertAlmostEquals } from "../deps.ts";
1+
import { assert } from "../deps.ts";
22
import { pow_x_n } from "../mod.ts";
3+
import { float64equals } from "../utils/float64equals.ts";
34

45
Deno.test("powx-n", () => {
56
const examples: {
67
input: Parameters<typeof pow_x_n>;
78
output: ReturnType<typeof pow_x_n>;
89
}[] = [
910
{ input: [2.0, 10], output: 1024.0 },
11+
{ input: [100, 100], output: 1e200 },
1012
{ input: [2.1, 3], output: 9.261 },
1113
{ input: [2.0, -2], output: 0.25 },
1214
{
@@ -37,6 +39,6 @@ Deno.test("powx-n", () => {
3739
{ input: [0, -Infinity], output: Infinity },
3840
];
3941
examples.forEach(({ input, output }) => {
40-
assertAlmostEquals(output, pow_x_n(...input));
42+
assert(float64equals(output, pow_x_n(...input)));
4143
});
4244
});

utils/float64equals.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function float64equals(a: number, b: number): boolean {
2+
// console.log(a, b);
3+
if (!Number.isFinite(a) || !Number.isFinite(b)) {
4+
return a === b;
5+
}
6+
if (b === 0) {
7+
return a === b;
8+
}
9+
return Math.abs((a - b) / b) < 1e-14;
10+
}

0 commit comments

Comments
 (0)