smallest-power-of-two
v2.0.0
Published
Find the smallest power of 2 with magnitude greater than or equal to the input. Zero runtime dependencies, ESM, TypeScript types.
Downloads
154
Maintainers
Readme
smallest-power-of-two
Find the smallest power of 2 with magnitude greater than or equal to the input. Zero runtime dependencies, ESM, TypeScript types.
Install
npm install smallest-power-of-twoUsage
import smallestPowerOfTwo from 'smallest-power-of-two';
smallestPowerOfTwo(1); // 1
smallestPowerOfTwo(2); // 2
smallestPowerOfTwo(3); // 4
smallestPowerOfTwo(5); // 8
smallestPowerOfTwo(124); // 128
smallestPowerOfTwo(0); // 0 — special case
smallestPowerOfTwo(-5); // -8 — negatives work too
smallestPowerOfTwo(1.1); // 2 — non-integers are ceil()'d firstAPI
function smallestPowerOfTwo(lowerBound: number): number;- Returns
0for input0(and-0). - Returns the smallest power of two
>= lowerBoundfor positive inputs. - For negatives, returns
-smallestPowerOfTwo(-lowerBound). - Throws
TypeErrorfor non-numbers. - Accuracy bounded by
Number.MAX_SAFE_INTEGER; inputs beyond2^53may saturate.
v2.0.0 — breaking change vs v1
v2 is ESM only ("type": "module", "exports" field). If you need CommonJS interop, stay on v1.2.7. The function shape and behavior are unchanged.
License
ISC — see LICENSE.
