Skip to content

Simplify Math.log(2) math? #7568

@kungfooman

Description

@kungfooman

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__));

nearestPowerOfTwo(val) {
return Math.pow(2, Math.round(Math.log(val) / Math.log(2)));
},

Saving some code and complexity?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions