Suppose that:
- You're using a programming language that doesn't support fixed-point or floating-point numbers.
- The said programming language rounds down divisions, e.g.
12 / 5 = 2
. - You're adding 18 trailing decimals to represent fixed-point numbers in your algorithm. for instance, you'd represent 0.5 as 500000000000000000, equivalent to 5e17 in scientific notation.
Now, say you want to calculate the integer part of the binary logarithm of a number x that adheres to the format above.
If x is greater than or equal to 1e18, then the calculation is easy: you just divide x by 1e18, and calculate the most significant bit of the rounded down result. For example, 4e18 / 1e18
is 4, of which the msb is 2. The integer part is thus 2.
But what if x is between 0 and 1e18 (both exclusive)? For example, 1e17? The binary logarithm of a number between 0 and 1 is negative ..