Skip to content

Commit

Permalink
添加了infinity支持,删除缓存,删除lazyMultiplyPositive
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Apr 10, 2022
1 parent 4b73a6c commit f900694
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 44 deletions.
104 changes: 60 additions & 44 deletions powx-n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,70 @@ export default function myPow(x: number, n: number): number {
if (Number.isNaN(x) || Number.isNaN(n)) {
throw Error("nan:" + x + "," + n);
}
if (!Number.isFinite(x) || !Number.isFinite(n)) {
throw Error("InFinite:" + x + "," + n);
// console.log(x, n);
if (x === Infinity) {
return n > 0 ? Infinity : n < 0 ? 0 : 1;
}
setTimeout(() => {
cacheStore.clear();
}, 0);
const cached = cacheStore.get(`${x},${n}`);
if (cached) {
return cached;
if (x === -Infinity) {
return (n % 2 === 0 ? 1 : -1) * myPow(-x, n);
}
// console.log(x,n)
const result = n === 1
? x
: x < 0
? (n % 2 === 0 ? 1 : -1) * myPow(-x, n)
: x === 1
? 1
: x === 0
? 0
: n === 0
? 1
: n < 0
? myPow(1 / x, -n)
: n % 2
? lazyMultiplyPositive(
() => x,
() => myPow(x, n - 1),
)
: lazyMultiplyPositive(
if (n === Infinity) {
return x === 0 ? 0 : Infinity;
}
if (n === -Infinity) {
return x === 0 ? Infinity : 0;
}
// setTimeout(() => {
// cacheStore.clear();
// }, 0);
// const cached = cacheStore.get(`${x},${n}`);
// if (cached) {
// console.log("cached");
// console.log(x, n);
// return cached;
// }

const result =
n === 1
? x
: x < 0
? (n % 2 === 0 ? 1 : -1) * myPow(-x, n)
: x === 1
? 1
: x === 0
? 0
: n === 0
? 1
: n < 0
? myPow(1 / x, -n)
: n % 2
? x * myPow(x, n - 1)
: /* lazyMultiplyPositive(
() => x,
() => myPow(x, n - 1)
) */
myPow(x * x, Math.floor(n / 2));
/* lazyMultiplyPositive(
() => myPow(x, Math.floor(n / 2)),
() => myPow(x, n - Math.floor(n / 2)),
);
cacheStore.set(`${x},${n}`, result);
); */

// cacheStore.set(`${x},${n}`, result);
return result;
}
//缓存命中率过低
// const cacheStore = new Map<`${number},${number}`, number>();

const cacheStore = new Map<`${number},${number}`, number>();

function lazyMultiplyPositive(a: () => number, b: () => number): number {
if (Math.random() < 0.5) {
[b, a] = [a, b];
}
const l = a();
if (l === 0) {
return 0;
} else if (l === Infinity) {
return Infinity;
} else {
return l * b();
}
}
// function lazyMultiplyPositive(a: () => number, b: () => number): number {
// if (Math.random() < 0.5) {
// [b, a] = [a, b];
// }
// const l = a();
// if (l === 0) {
// return 0;
// } else if (l === Infinity) {
// return Infinity;
// } else {
// return l * b();
// }
// }
15 changes: 15 additions & 0 deletions powx-n/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,27 @@ Deno.test("powx-n", () => {
output: 64.0,
},
{ input: [2, -99999], output: 0.0 },
{ input: [2, 99999], output: Infinity },
{ input: [6553688888888, 6553688888888], output: Infinity },
{ input: [6553688888888, -6553688888888], output: 0.0 },
{ input: [1 / 6553688888888, 6553688888888], output: 0.0 },
{ input: [1 / 6553688888888, -6553688888888], output: Infinity },
{ input: [-6553688888888, -6553688888888], output: 0.0 },
{ input: [-6553688888888, 6553688888888], output: Infinity },
{ input: [Infinity, 2], output: Infinity },
{ input: [Infinity, -2], output: 0 },
{ input: [Infinity, 0], output: 1 },
{ input: [-Infinity, 0], output: 1 },
{ input: [-Infinity, 3], output: -Infinity },
{ input: [-Infinity, 30], output: Infinity },
{ input: [-Infinity, -30], output: 0 },

{ input: [0, Infinity], output: 0 },
{ input: [10, Infinity], output: Infinity },
{ input: [-10, Infinity], output: Infinity },
{ input: [-10, -Infinity], output: 0 },
{ input: [10, -Infinity], output: 0 },
{ input: [0, -Infinity], output: Infinity },
];
examples.forEach(({ input, output }) => {
assertAlmostEquals(output, pow_x_n(...input));
Expand Down

0 comments on commit f900694

Please sign in to comment.