Math.log2
func Math.log2(x: float64): float64Math.log2 returns the base-2 logarithm of x.
Details
Math.log2never panics.0.0and-0.0give-Infinity, a negativexgivesNaN, andNaNgivesNaN.
Basic examples
Math.log2(8.0) // 3.0
Math.log2(1024.0) // 10.0
Math.log2(10.0) // 3.321928094887362Fractions below 1.0 have negative logarithms:
Math.log2(0.5) // -1.0
Math.log2(0.25) // -2.0Scope
1.0 gives 0.0, and both zeros give -Infinity:
Math.log2(1.0) // 0.0
Math.log2(0.0) // -Infinity
Math.log2(-0.0) // -InfinityA negative number has no real logarithm, so the result is NaN. The infinities and NaN follow IEEE 754:
Math.log2(-1.0) // NaN
Math.log2(Math.Infinity) // Infinity
Math.log2(-Math.Infinity) // NaN
Math.log2(Math.NaN) // NaNThe largest finite float64 and the smallest positive one stay in range:
Math.log2(Math.maxFloat64) // 1024.0
Math.log2(5e-324) // -1074.0An integer goes through float64 first:
let n = 4096
Math.log2(float64(n)) // 12.0Pass it by name to Array.map, call it in a pipe, or name the parameter x:
[1.0, 2.0, 4.0, 8.0].map(Math.log2) // [0.0, 1.0, 2.0, 3.0]
16.0 | Math.log2() // 4.0
Math.log2(x: 32.0) // 5.0Applications
Count the bits needed to number n distinct values:
func bitsFor(n: int): int {
int(Math.ceil(Math.log2(float64(n))))
}
[2, 10, 256, 1000].map(bitsFor) // [1, 4, 8, 10]Measure the Shannon entropy of a distribution, in bits:
func entropy(ps: Array[float64]): float64 {
var h = 0.0
for p in ps {
if p > 0.0 {
h -= p * Math.log2(p)
}
}
h
}
entropy([0.5, 0.5]) // 1.0
entropy([0.25, 0.25, 0.25, 0.25]) // 2.0
entropy([0.9, 0.1]) // 0.4689955935892812Find the interval between two frequencies in semitones: an octave doubles the frequency and has 12 semitones:
func semitones(from: float64, to: float64): float64 {
12.0 * Math.log2(to / from)
}
semitones(440.0, 880.0) // 12.0
Math.round(semitones(440.0, 660.0) * 100.0) / 100.0 // 7.02Properties and relations
Math.log2 turns products into sums, Math.pow(2.0, _) undoes it, and it equals Math.log(x) / Math.log(2.0), each up to rounding:
func close(a: float64, b: float64): bool {
Math.abs(a - b) <= 1e-12 * Math.max(1.0, Math.max(Math.abs(a), Math.abs(b)))
}
for x in [0.1, 0.5, 3.0, 10.0, 1e6] {
assert(close(Math.log2(x * 3.0), Math.log2(x) + Math.log2(3.0)))
assert(close(Math.pow(2.0, Math.log2(x)), x))
assert(close(Math.log2(x), Math.log(x) / Math.log(2.0)))
}For a positive int, the floor of its base-2 logarithm is the index of its highest set bit. Bits.leadingZeros gives that index exactly, without going through float64:
Math.floor(Math.log2(1000.0)) // 9.0
63 - Bits.leadingZeros(1000) // 9Possible issues
Math.log(x) / Math.log(2.0) rounds twice, so its result can differ from Math.log2(x):
let x = Math.pow(2.0, 29.0)
Math.log2(x) // 29.0
Math.log(x) / Math.log(2.0) // 29.000000000000004A large int rounds when it becomes a float64, and the logarithm rounds again, so the floor of its logarithm can be one too high. Bits.leadingZeros stays exact:
let n = Bits.shiftLeft(1, 50) - 1
Math.floor(Math.log2(float64(n))) // 50.0
63 - Bits.leadingZeros(n) // 49
Math.floor(Math.log2(float64(Math.maxInt))) // 63.0
63 - Bits.leadingZeros(Math.maxInt) // 62Converting the result to int panics when it has a fraction. Round it first, or use Math.toInt to get null:
int(Math.log2(1000.0)) // panic: 9.965784284662087 has a fraction, so it does not convert to int
int(Math.floor(Math.log2(1000.0))) // 9
Math.toInt(Math.log2(1000.0)) // nullAn integer argument is a compile error, since a numeric literal without a point is an int:
Math.log2(8) // error: cannot use int where float64 is expectedSee also
Math.log · Math.log10 · Math.exp · Math.pow · Bits.leadingZeros · Math.toInt · Math
