qp.math.floor_log2¶
- floor_log2(n)[source]¶
Compute the floor of the base-2 logarithm of a number, with integer as output data type.
- Parameters:
n (int or float) – Number to compute the rounded-down base-2 logarithm of. Traced inputs must have an integer data type.
- Returns:
Rounded-down base-2 logarithm of
n.- Return type:
int
Warning
Under just-in-time (JIT) compilation with JAX, this function will not raise an error for the input
0:>>> import jax >>> jax.jit(qp.math.floor_log2)(0) Array(2147483647, dtype=int32)
Example
On powers of two,
floor_log2simply acts likenp.log2whose result was converted to anint:>>> qp.math.floor_log2(8) 3
On other numbers, the rounding of the logarithm becomes visible:
>>> qp.math.log2(14) 3.807354922057604 >>> qp.math.floor_log2(14) 3
Note that we always round down:
>>> qp.math.round(qp.math.log2(15)) 4.0 >>> qp.math.floor_log2(15) 3
The result is exact even for inputs with more significant bits than a float can hold:
>>> qp.math.floor_log2(2**53 - 1) 52
For simple
intinputs, this function computes the same asn.bit_length() - 1, which does not work for inputs likenp.int64.