You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In practice, `log2_int` differs from `math.log2` in the following ways:
32
26
27
+
1. its implementation is restricted to integers only;
28
+
2. if `need_pow2` is false, the result is rounded up to the nearest integer;
29
+
3. it doesn't raise an exception for `n == 0`;
30
+
4. if `need_pow2` is false, it doesn't raise an exception for `n < 0`.
33
31
34
-
In practice, developers seem to use `log2_int` as a variant of `math.log2` whose result is rounded-up to an integer, in order to know how many bits are needed to represent a number.
32
+
#### Observations
35
33
36
-
However, the fact that it transparently accepts `0` and rejects non-integer values such as `0.5` makes it inadequate for use cases outside bit manipulation.
34
+
**1)* is a desirable property; coercing integers into floating-point numbers is fraught with peril, as the latter have limited precision.
35
+
**2)* has common use-cases in digital design, such as address decoders.
36
+
**3)* and *4)* are misleading at best. Despite being advertised as a logarithm, `log2_int` doesn't exclude 0 or negative integers from its domain.
0 commit comments