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
61
votes
2 answers

scatter plot logarithmic scale

In my code, I take the logarithm of two data series and plot them. I would like to change each tick value of the x-axis by raising it to the power of e (anti-log of natural logarithm). In other words. I want to graph the logarithms of both series…
natsuki_2002
  • 24,239
  • 21
  • 46
  • 50
60
votes
4 answers

Logarithmic y-axis bins in python

I'm trying to create a histogram of a data column and plot it logarithmically (y-axis) and I'm not sure why the following code does not work: import numpy as np import matplotlib.pyplot as plt data = np.loadtxt('foo.bar') fig = plt.figure() ax =…
mannaroth
  • 1,473
  • 3
  • 17
  • 38
56
votes
1 answer

How to expand and compute log(a + b)?

I would like to know the complete expansion of log(a + b). For example log(a * b) = log(a) + log(b); log(a / b) = log(a) - log(b); Similar to this, is there any expansion for log(a + b)?
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55
54
votes
2 answers

Convert Linear scale to Logarithmic

I have a linear scale that ranges form 0.1 to 10 with increments of change at 0.1:   |----------[]----------| 0.1         5.0         10 However, the output really needs to be:   |----------[]----------| 0.1         1.0         10 (logarithmic…
TurdPile
  • 976
  • 1
  • 7
  • 21
53
votes
17 answers

How to do an integer log2() in C++?

In the C++ standard libraries I found only a floating point log method. Now I use log to find the level of an index in a binary tree ( floor(2log(index)) ). Code (C++): int targetlevel = int(log(index)/log(2)); I am afraid that for some of the…
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
50
votes
11 answers

Logarithm of a BigDecimal

How can I calculate the logarithm of a BigDecimal? Does anyone know of any algorithms I can use? My googling so far has come up with the (useless) idea of just converting to a double and using Math.log. I will provide the precision of the answer…
masher
  • 3,814
  • 4
  • 31
  • 35
44
votes
3 answers

Big O notation Log Base 2 or Log Base 10

When articles/question state that the Big O running time of the algorithm is O(LogN) . For example Quicksort has a Big O running time of O (LogN) where the it is Log base 10 but Height of binary tree is O(LogN+1) where it is Log base 2 Question 1)I…
Computernerd
  • 7,378
  • 18
  • 66
  • 95
44
votes
2 answers

Logarithm Algorithm

I need to evaluate a logarithm of any base, it does not matter, to some precision. Is there an algorithm for this? I program in Java, so I'm fine with Java code. How to find a binary logarithm very fast? (O(1) at best) might be able to answer my…
Justin
  • 24,288
  • 12
  • 92
  • 142
41
votes
4 answers

numerically stable way to multiply log probability matrices in numpy

I need to take the matrix product of two NumPy matrices (or other 2d arrays) containing log probabilities. The naive way np.log(np.dot(np.exp(a), np.exp(b))) is not preferred for obvious reasons. Using from scipy.misc import logsumexp res =…
mart
  • 440
  • 4
  • 9
40
votes
2 answers

What is the complexity of the log function?

What is the complexity of the log base 10 function?
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
39
votes
6 answers

Logarithm of a BigInt

Is there a way to get the logarithm of a BigInt in JavaScript? With normal numbers, you would use this code: const largeNumber = 1000; const result = Math.log(largeNumber); However, I need to work with factorial numbers, potentially higher than…
Mielipuoli
  • 1,306
  • 2
  • 12
  • 23
34
votes
4 answers

Fast fixed point pow, log, exp and sqrt

I've got a fixed point class (10.22) and I have a need of a pow, a sqrt, an exp and a log function. Alas I have no idea where to even start on this. Can anyone provide me with some links to useful articles or, better yet, provide me with some…
Goz
  • 61,365
  • 24
  • 124
  • 204
34
votes
4 answers

Using log base 10 in a formula in Java

I'm trying to write a Java program that can take values and put them into a formula involving log base 10. How can I calculate log10 in Java?
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
32
votes
5 answers

Why the cost function of logistic regression has a logarithmic expression?

cost function for the logistic regression is cost(h(theta)X,Y) = -log(h(theta)X) or -log(1-h(theta)X) My question is what is the base of putting the logarithmic expression for cost function .Where does it come from? i believe you can't just put…
Nipun Alahakoon
  • 2,772
  • 5
  • 27
  • 45
30
votes
4 answers

De Bruijn-like sequence for `2^n - 1`: how is it constructed?

I'm looking at the entry Find the log base 2 of an N-bit integer in O(lg(N)) operations with multiply and lookup from Bit Twiddling hacks. I can easily see how the second algorithm in that entry works static const int…
1
2
3
68 69