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
563
votes
6 answers

Plot logarithmic axes

I want to plot a graph with one logarithmic axis using matplotlib. I've been reading the docs, but can't figure out the syntax. I know that it's probably something simple like 'scale=linear' in the plot arguments, but I can't seem to get it…
Jim
221
votes
10 answers

How can I specify the base for Math.log() in JavaScript?

I need a log function for JavaScript, but it needs to be base 10. I can't see any listing for this, so I'm assuming it's not possible. Are there any math wizards out there who know a solution for this?
MetaGuru
  • 42,847
  • 67
  • 188
  • 294
165
votes
6 answers

How do you do natural logs (e.g. "ln()") with numpy in Python?

Using numpy, how can I do the following: ln(x) Is it equivalent to: np.log(x) I apologise for such a seemingly trivial question, but my understanding of the difference between log and ln is that ln is logspace e?
user1220022
  • 11,167
  • 19
  • 41
  • 57
157
votes
10 answers

How do you calculate log base 2 in Java for integers?

I use the following function to calculate log base 2 for integers: public static int log2(int n){ if(n <= 0) throw new IllegalArgumentException(); return 31 - Integer.numberOfLeadingZeros(n); } Does it have optimal performance? Does someone…
Nulldevice
  • 3,926
  • 3
  • 31
  • 37
150
votes
10 answers

Log to the base 2 in python

How should I compute log to the base two in python. Eg. I have this equation where I am using log base 2 import math e = -(t/T)* math.log((t/T)[, 2])
Soumya
  • 5,262
  • 8
  • 31
  • 30
135
votes
5 answers

Why does math.log result in ValueError: math domain error?

I was just testing an example from Numerical Methods in Engineering with Python. from numpy import zeros, array from math import sin, log from newtonRaphson2 import * def f(x): f = zeros(len(x)) f[0] = sin(x[0]) + x[1]**2 + log(x[2]) - 7.0 …
ramanunni.pm
  • 1,619
  • 2
  • 11
  • 9
133
votes
3 answers

What is the difference between 'log' and 'symlog'?

In matplotlib, I can set the axis scaling using either pyplot.xscale() or Axes.set_xscale(). Both functions accept three different scales: 'linear' | 'log' | 'symlog'. What is the difference between 'log' and 'symlog'? In a simple test I did, they…
Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
122
votes
2 answers

What would cause an algorithm to have O(log log n) complexity?

This earlier question addresses some of the factors that might cause an algorithm to have O(log n) complexity. What would cause an algorithm to have time complexity O(log log n)?
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
115
votes
2 answers

NumPy: Logarithm with base n

From the numpy documentation on logarithms, I have found functions to take the logarithm with base e, 2, and 10: import numpy as np np.log(np.e**3) #3.0 np.log2(2**3) #3.0 np.log10(10**3) #3.0 However, how do I take the logarithm with base n…
dwitvliet
  • 7,242
  • 7
  • 36
  • 62
113
votes
6 answers

What would cause an algorithm to have O(log n) complexity?

My knowledge of big-O is limited, and when log terms show up in the equation it throws me off even more. Can someone maybe explain to me in simple terms what a O(log n) algorithm is? Where does the logarithm come from? This specifically came up…
user1189352
  • 3,628
  • 12
  • 50
  • 90
107
votes
3 answers

What is O(log* N)?

What is O(log* N) and how is it different from O(log N)?
Timmy
  • 12,468
  • 20
  • 77
  • 107
95
votes
6 answers

Logarithmic slider

I have a slider with values ranging from 0 to 100. I want to map them to a range from 100 to 10,000,000. I've seen some functions scattered around the net but they're all in C++. I need it in Javascript. Any ideas?
user103773
  • 953
  • 1
  • 8
  • 5
89
votes
6 answers

set ticks with logarithmic scale

It seems that the set_xticks is not working in log scale: from matplotlib import pyplot as plt fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale('log') ax1.set_xticks([20, 200, 500]) plt.show() is it possible?
Ruggero Turra
  • 16,929
  • 16
  • 85
  • 141
88
votes
7 answers

Difference between O(n) and O(log(n)) - which is better and what exactly is O(log(n))?

This is my first course in data structures and every lecture / TA lecture , we talk about O(log(n)) . This is probably a dumb question but I'd appreciate if someone can explain to me exactly what does it mean !?
JAN
  • 21,236
  • 66
  • 181
  • 318
80
votes
7 answers

Histogram with Logarithmic Scale and custom breaks

I'm trying to generate a histogram in R with a logarithmic scale for y. Currently I do: hist(mydata$V3, breaks=c(0,1,2,3,4,5,25)) This gives me a histogram, but the density between 0 to 1 is so great (about a million values difference) that you can…
Weegee
  • 2,225
  • 1
  • 17
  • 16
1
2 3
68 69