Questions tagged [logarithm]

The logarithm of a number is the exponent by which another fixed value, the base, has to be raised to produce that number. It has applications in algebra and complexity theory. It is usually denoted as log in programming languages. Use this tag for any programming questions involving logarithms.

A base-b logarithm of a number represents the exponent that b must be raised to to obtain the original number. Namely, for a number n, it is like asking the question "b to which power equals n?"

Given a number n

  • Express n = bx for some base b, and some exponent x.
  • Then, the base-b logarithm of n is the exponent x. logb(n) = x.

The three most important bases of a logarithm is the base-2 logarithm, prevalent in computer science, the base-10 logarithm similar to counting the number of decimal places, and the natural (base-e) logarithm with unique mathematical properties.

The notations of the logarithm of a number n for different bases is

  • logb(n) for a given base b
  • log(n), which may mean the base-10 logarithm, the natural logarithm, or a generic logarithm
  • ln(n), which is often but not always used for the natural logarithm.

Furthermore, the base of the logarithm can be changed using a simple formula involving a ratio of logarithms. In fact, logarithms of base c is expressed as a constant multiple of a logarithm of base b.

  • logc(n) = logb(n)/logb(c)

Most of the time, the logarithm is an irrational number that cannot be expressed exactly in terms of decimals, let alone floating-point numbers.

Logarithm in programming languages

Unlike simple additive and multiplicative operators, and in some cases, the exponent, using the logarithm for a given programming language is usually used as a particular built-in library method. The logarithm usually accepts floating-point inputs strictly greater than zero and returns the approximate logarithm of the number, usually the natural logarithm.

Furthermore, note that the name log(...) often refers to the natural logarithm in the context of programming languages, rather than ln(...). The base-10 log is usually named log10(...).

Languages which support a built-in base-b logarithm passes through two arguments, and may either be of the form log(x, b) or log(b, x) for a number to be evaluated x and base b.

Usages in various programming languages (base-e)

  • C: log(double x), logf(float x), logl(long double x) (#include <math.h>)
  • C++: std::log(x) (#include <cmath>)
  • Java: java.lang.Math.log(double x)
  • JavaScript: Math.log(x) (do not confuse with console.log)
  • Python: log(x), log(x, b) for base-b logarithm (import log from math)
  • Rust: ln(self: f64) -> f64, (log10 and log2 for bases 10, 2)
  • Fortran: LOG(x)
  • MySQL: LOG(x), LOG(b, x) for base-b logarithm of a number x
  • Excel: LN(x), LOG(x, b) for base-b

Applications of logarithms

The logarithm is the inverse of exponentiation, namely that logb(bn), blogb(n) are identities that equal the original number n.

Logarithms are important in many mathematical fields, especially those involving variables differing by many orders of magnitude, logarithmic axes, and solving formulas involving exponential expressions. It is also a way to interpolate the number of digits (of some base b) of a given number: the number of decimal digits required for a positive integer n is ⌊log10(n)⌋.

The logarithm also represents a major complexity class in algorithmic complexity theory. Logarithmic complexity represents time or space complexity of O(log(n)) for an input of size n. Logarithmic growth is very slow, and is asymptotically slower than any power function nc where c is strictly positive including the linear function. Therefore, logarithmic complexity is very efficient, and is considered to be in polynomial time.

These algorithms are all logarithmic-time:

  • Binary Search
  • BST Insertion (average case)

There is also another major complexity class called linearithmic complexity, which represents O(n * log(n)) complexity. It usually occurs when a logarithmic-complexity process is executed n times. It is asymptotically slower than any power function nc where c > 1.

These algorithms are all in linearithmic, or n-log-n time.

  • Quicksort (average case)
  • Listing all elements in a BST (average case)

Read more

Documentation

Tags

  • : DO NOT use this tag for logarithms. The word "log" is also used to refer to the action of logging. That tag is for logarithms and logarithmic concepts only.
  • : DO NOT use this tag for the natural logarithm. That refers to the link command. Instead, use .
  • , : Inverses of the logarithm function
  • : Refers to the natural exponent, the inverse of log(...).
    Namely, log(exp(x)) == exp(log(x)) == x for x > 0.
  • , , , : The logarithm is an important class of algorithmic complexity. It is very efficient, much more efficient than linear, but is lesser than constant.

External links

1026 questions
30
votes
5 answers

Logarithm for BigInteger

I have a BigInteger number, for example beyond 264. Now i want to calculate the logarithm of that BigInteger number, but the method BigInteger.log() does not exist. How do I calculate the (natural) logarithm of my large BigInteger value?
RAVITEJA SATYAVADA
  • 2,503
  • 23
  • 56
  • 88
26
votes
4 answers

Why are log2 and log1p so much faster than log and log10, in numpy?

Whilst playing around with this question I noticed something I couldn't explain regarding the relative performance of np.log2, np.log and np.log10: In [1]: %%timeit x = np.random.rand(100000) ....: np.log2(x) ....: 1000 loops, best of 3: 1.31…
ali_m
  • 71,714
  • 23
  • 223
  • 298
26
votes
3 answers

Non-linear axes for imshow in matplotlib

I am generating 2D arrays on log-spaced axes (for instance, the x pixel coordinates are generated using logspace(log10(0.95), log10(2.08), n). I want to display the image using a plain old imshow, in its native resolution and scaling (I don't need…
endolith
  • 25,479
  • 34
  • 128
  • 192
25
votes
4 answers

Big O confusion: log2(N) vs log3(N)

Why is O(log2N) = O(log3N) ? I don't understand this. Does big O not mean upper bound of something? Isn't log2N bigger than log3N ? When I graph them, log2N is above log3N .
Daver Muzaffar
  • 648
  • 1
  • 7
  • 17
24
votes
4 answers

What is the big-O of the function (log n)^k

What is the big-O complexity of the function (log n)k for any k?
ADeveloper
  • 249
  • 1
  • 2
  • 3
23
votes
1 answer

Logarithm Calculation with Windows 7 Calculator

I would like to use the Windows Calculator in Scientific Mode in order solve a very basic Logarithm equation but, unfortunately, I couldn't do that. Here is the problem: log_5 125=? Thank you very much for your help... Well, I know it equals to…
Kerim Atasoy
  • 331
  • 1
  • 2
  • 5
23
votes
7 answers

Python math module

Whenever I try to use any of the built-in functions of Python's exponentiation and logarithms module, I get an error like this: NameError: name 'sqrt' is not defined I have tried using math.sqrt(4),sqrt(4) and sqrt(4.0), but none of them work. The…
user1126849
  • 291
  • 1
  • 2
  • 8
22
votes
5 answers

C/C++ fastest cmath log operation

I'm trying to calculate logab (and get a floating point back, not an integer). I was planning to do this as log(b)/log(a). Mathematically speaking, I can use any of the cmath log functions (base 2, e, or 10) to do this calculation; however, I will…
Nick
  • 2,821
  • 5
  • 30
  • 35
22
votes
1 answer

How can you quickly compute the integer logarithm for any base?

How can I quickly compute the integer logarithm for any base, not just base 10? This question has a really efficient solution for base 10 but I would like to understand how I can generalize that to other bases.
Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
21
votes
1 answer

Logarithm function of an arbitrary integer base in C

Is there a function or any other way to calculate in C the logarithm of base x, where x is an integer variable of my program?
nikos
  • 2,893
  • 11
  • 30
  • 39
20
votes
1 answer

semilog plots with hold on

If I try to plot multiple plots with a logarithmic axis, the log scale is disabled. If I remove the hold on the log scale is enabled, but I can only plot a single plot. figure(1); clf x = linspace(0,1,100); y = exp(-x); hold on; semilogy(x,…
Matthias Pospiech
  • 3,130
  • 18
  • 55
  • 76
20
votes
6 answers

Calculating the length of a segment in a logarithmic scale

I want to calculate the length of a line for a series of events. I'm doing this with the following code. var maxLineLength = 20; var lineLen = function(x, max) { return maxLineLength * (x / max); } var events = [0.1, 1, 5, 20, 50]; var…
cdarwin
  • 4,141
  • 9
  • 42
  • 66
20
votes
8 answers

Very fast approximate Logarithm (natural log) function in C++?

We find various tricks to replace std::sqrt (Timing Square Root) and some for std::exp (Using Faster Exponential Approximation) , but I find nothing to replace std::log. It's part of loops in my program and its called multiple times and while exp…
qwark
  • 493
  • 1
  • 4
  • 15
20
votes
19 answers

Where can I learn about logarithms?

I hear logarithms mentioned quite a lot in the programming context. They seem to be the solution to many problems and yet I can't seem to find a real-world way of making use of them. I've read the Wikipedia entry and that, quite frankly, leaves me…
Charles Roper
  • 20,125
  • 20
  • 71
  • 101
19
votes
8 answers

How to find a binary logarithm very fast? (O(1) at best)

Is there any very fast method to find a binary logarithm of an integer number? For example, given a number x=52656145834278593348959013841835216159447547700274555627155488768 such algorithm must find y=log(x,2) which is 215. x is always a power of…
psihodelia
  • 29,566
  • 35
  • 108
  • 157
1 2
3
68 69