Skip to content

Commit 800bb5b

Browse files
author
Alexander Belov
committed
Fixes
1 parent f6c8357 commit 800bb5b

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/lib/math/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from './is-power-of-two/is-power-of-two';
2+
export * from './nearest-highest-power-of-two/nearest-highest-power-of-two';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// tslint:disable:no-expression-statement
2+
import test from 'ava';
3+
import { nearestHighestPowerOfTwo } from './nearest-highest-power-of-two';
4+
5+
test('nearest highest power of two', t => {
6+
const tests = [
7+
[ 1, 0 ],
8+
[ 2, 1 ],
9+
[ 3, 2 ],
10+
[ 5, 3 ],
11+
[ 11, 4 ],
12+
[ 1023, 10 ],
13+
[ 1024, 10 ],
14+
[ 1025, 11 ],
15+
[ 2 ** 31 - 1, 31 ],
16+
[ 2 ** 78 - 1, 78 ],
17+
[ 2 ** 78 + 1, 79 ],
18+
];
19+
20+
for (const v of tests) {
21+
const [ value, power ] = v;
22+
23+
t.is(nearestHighestPowerOfTwo(value), power);
24+
}
25+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Returns nearest highest power of two from specific number
3+
*
4+
* @param {number} value
5+
* @returns {number}
6+
*/
7+
export function nearestHighestPowerOfTwo(value: number): number {
8+
return Math.ceil(Math.log2(value));
9+
}

0 commit comments

Comments
 (0)