32

What is the complexity of the log base 10 function?

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
Paul Manta
  • 27,853
  • 27
  • 109
  • 192

2 Answers2

45

This really depends on the domain of what values you want to compute a logarithm of.

For IEEE doubles, many processors can take logarithms in a single assembly instruction; x86 has the FYL2X and FYL2XP1 instructions, for example. Although typically instructions like these will only take the logarithm in some fixed base, they can be used to take logarithms in arbitrary bases by using the fact that

loga b = logc b / logc a

by simply taking two logarithms and finding their quotient.

For general integers (of arbitrary precision), you can use repeated squaring combined with a binary search to take logarithms using only O(log log n) arithmetic operations (each time you square a number you double the exponent, which means you can only square the number log log n times before you've exceeded its value and can do a binary search). Using some cute tricks with Fibonacci numbers, you can do this in only O(log n) space. If you're computing the binary logarithm, there are some cute tricks you can use with bit shifts to compute the value in less time (though the asymptotic complexity is the same).

For arbitrary real numbers the logic is harder. You can use Newton's method or the Taylor series to compute logarithms to within a certain precision, though I confess that I'm not familiar with the methods for doing this. However, you rarely actually need to do this because most real numbers are IEEE doubles and there are better algorithms (or even hardware instructions) in that case.

Hope this helps!

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
  • For integers there's often also an instruction (or a short sequence, to do `CTZ(x & (x - 1))` or `wordsize - LZC(x)`) - but AFAIK that doesn't help with the time complexity at all (just actual speed) – harold Sep 06 '11 at 09:42
  • 1
    @templatetypedef Which you can multiply by a constant factor to get logarithms in any other base, as you just demonstrated. :) – Nick Johnson Sep 07 '11 at 05:25
  • @NickJohnson Yes, and thought it is fast indeed, we should note, that multiplication operation itself has higher that logarithmic complexity: https://en.wikipedia.org/wiki/Computational_complexity_of_mathematical_operations – Rustam A. Apr 25 '21 at 10:11
5

To do log(n) in O(1) ( where n is an integer )

int log(long long x)
{
    return 64 - __builtin_clzl(x) - 1;
}

for __builtin_clzl(x) refer here

iamakshatjain
  • 320
  • 4
  • 8
  • 1
    This will compute the binary logarithm of the number, assuming the number is an integer. But if you wanted to compute log base 10, I think you'd need to use a different strategy. – templatetypedef May 15 '20 at 21:38