Skip to content

Commit f900694

Browse files
committed
添加了infinity支持,删除缓存,删除lazyMultiplyPositive
1 parent 4b73a6c commit f900694

File tree

2 files changed

+75
-44
lines changed

2 files changed

+75
-44
lines changed

powx-n/index.ts

Lines changed: 60 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,70 @@ export default function myPow(x: number, n: number): number {
22
if (Number.isNaN(x) || Number.isNaN(n)) {
33
throw Error("nan:" + x + "," + n);
44
}
5-
if (!Number.isFinite(x) || !Number.isFinite(n)) {
6-
throw Error("InFinite:" + x + "," + n);
5+
// console.log(x, n);
6+
if (x === Infinity) {
7+
return n > 0 ? Infinity : n < 0 ? 0 : 1;
78
}
8-
setTimeout(() => {
9-
cacheStore.clear();
10-
}, 0);
11-
const cached = cacheStore.get(`${x},${n}`);
12-
if (cached) {
13-
return cached;
9+
if (x === -Infinity) {
10+
return (n % 2 === 0 ? 1 : -1) * myPow(-x, n);
1411
}
15-
// console.log(x,n)
16-
const result = n === 1
17-
? x
18-
: x < 0
19-
? (n % 2 === 0 ? 1 : -1) * myPow(-x, n)
20-
: x === 1
21-
? 1
22-
: x === 0
23-
? 0
24-
: n === 0
25-
? 1
26-
: n < 0
27-
? myPow(1 / x, -n)
28-
: n % 2
29-
? lazyMultiplyPositive(
30-
() => x,
31-
() => myPow(x, n - 1),
32-
)
33-
: lazyMultiplyPositive(
12+
if (n === Infinity) {
13+
return x === 0 ? 0 : Infinity;
14+
}
15+
if (n === -Infinity) {
16+
return x === 0 ? Infinity : 0;
17+
}
18+
// setTimeout(() => {
19+
// cacheStore.clear();
20+
// }, 0);
21+
// const cached = cacheStore.get(`${x},${n}`);
22+
// if (cached) {
23+
// console.log("cached");
24+
// console.log(x, n);
25+
// return cached;
26+
// }
27+
28+
const result =
29+
n === 1
30+
? x
31+
: x < 0
32+
? (n % 2 === 0 ? 1 : -1) * myPow(-x, n)
33+
: x === 1
34+
? 1
35+
: x === 0
36+
? 0
37+
: n === 0
38+
? 1
39+
: n < 0
40+
? myPow(1 / x, -n)
41+
: n % 2
42+
? x * myPow(x, n - 1)
43+
: /* lazyMultiplyPositive(
44+
() => x,
45+
() => myPow(x, n - 1)
46+
) */
47+
myPow(x * x, Math.floor(n / 2));
48+
/* lazyMultiplyPositive(
3449
() => myPow(x, Math.floor(n / 2)),
3550
() => myPow(x, n - Math.floor(n / 2)),
36-
);
37-
cacheStore.set(`${x},${n}`, result);
51+
); */
52+
53+
// cacheStore.set(`${x},${n}`, result);
3854
return result;
3955
}
56+
//缓存命中率过低
57+
// const cacheStore = new Map<`${number},${number}`, number>();
4058

41-
const cacheStore = new Map<`${number},${number}`, number>();
42-
43-
function lazyMultiplyPositive(a: () => number, b: () => number): number {
44-
if (Math.random() < 0.5) {
45-
[b, a] = [a, b];
46-
}
47-
const l = a();
48-
if (l === 0) {
49-
return 0;
50-
} else if (l === Infinity) {
51-
return Infinity;
52-
} else {
53-
return l * b();
54-
}
55-
}
59+
// function lazyMultiplyPositive(a: () => number, b: () => number): number {
60+
// if (Math.random() < 0.5) {
61+
// [b, a] = [a, b];
62+
// }
63+
// const l = a();
64+
// if (l === 0) {
65+
// return 0;
66+
// } else if (l === Infinity) {
67+
// return Infinity;
68+
// } else {
69+
// return l * b();
70+
// }
71+
// }

powx-n/test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,27 @@ Deno.test("powx-n", () => {
1414
output: 64.0,
1515
},
1616
{ input: [2, -99999], output: 0.0 },
17+
{ input: [2, 99999], output: Infinity },
1718
{ input: [6553688888888, 6553688888888], output: Infinity },
1819
{ input: [6553688888888, -6553688888888], output: 0.0 },
1920
{ input: [1 / 6553688888888, 6553688888888], output: 0.0 },
2021
{ input: [1 / 6553688888888, -6553688888888], output: Infinity },
2122
{ input: [-6553688888888, -6553688888888], output: 0.0 },
2223
{ input: [-6553688888888, 6553688888888], output: Infinity },
24+
{ input: [Infinity, 2], output: Infinity },
25+
{ input: [Infinity, -2], output: 0 },
26+
{ input: [Infinity, 0], output: 1 },
27+
{ input: [-Infinity, 0], output: 1 },
28+
{ input: [-Infinity, 3], output: -Infinity },
29+
{ input: [-Infinity, 30], output: Infinity },
30+
{ input: [-Infinity, -30], output: 0 },
31+
32+
{ input: [0, Infinity], output: 0 },
33+
{ input: [10, Infinity], output: Infinity },
34+
{ input: [-10, Infinity], output: Infinity },
35+
{ input: [-10, -Infinity], output: 0 },
36+
{ input: [10, -Infinity], output: 0 },
37+
{ input: [0, -Infinity], output: Infinity },
2338
];
2439
examples.forEach(({ input, output }) => {
2540
assertAlmostEquals(output, pow_x_n(...input));

0 commit comments

Comments
 (0)