-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Description
Not a big issue, but still needlessly complex:
function nearestPowerOfTwo(val) {
return Math.pow(2, Math.round(Math.log(val) / Math.log(2)));
}
// Math.log(2) === Math.LN2
function nearestPowerOfTwo_(val) {
return Math.pow(2, Math.round(Math.log(val) / Math.LN2));
}
// Math.log(val) / Math.LN2) === Math.log2(val)
function nearestPowerOfTwo__(val) {
return Math.pow(2, Math.round(Math.log2(val)));
}
arr = [0,1,2,3,4,5];
console.log(arr.map(nearestPowerOfTwo))
console.log(arr.map(nearestPowerOfTwo_));
console.log(arr.map(nearestPowerOfTwo__));
Lines 192 to 194 in 62c1c7f
nearestPowerOfTwo(val) { | |
return Math.pow(2, Math.round(Math.log(val) / Math.log(2))); | |
}, |
Saving some code and complexity?
Metadata
Metadata
Assignees
Labels
No labels