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
19
votes
7 answers

Compile time computing of number of bits needed to encode n different states

Edit: In the initial question had a wrong formula and the algorithm tried was doing something completely different than what was intended. I apologise and I decided to rewrite the question to eliminate all the confusion. I need to compute at compile…
bolov
  • 72,283
  • 15
  • 145
  • 224
18
votes
3 answers

Labelling logarithmic scale display in R

While plotting histogarm, scatterplots and other plots with axes scaled to logarithmic scale in R, how is it possible to use labels such as 10^-1 10^0 10^1 10^2 10^3 and so on instead of the axes showing just -1, 0, 1, 2, 3 etc. What parameters…
user744121
  • 467
  • 2
  • 7
  • 17
17
votes
8 answers

Displaying minor logarithmic ticks in x-axis in R

I have a normal distribution plot and a histogram plot with x axis in log scale displaying 0, 10^0, 10^1 ... I want to include minor ticks between the major ones. Actually I was able to change the major ticks format from 1, 2, 3 and so on to 10^0,…
user744121
  • 467
  • 2
  • 7
  • 17
17
votes
1 answer

Logarithmic scale returns NaN

I have issues creating a logarithmic scale in d3. The scale works fine if it is set to linear. This works: var myLinScale = d3.scale.linear() .domain([0, 100]) .range([50, 1150]); console.log(myLinScale(71)); //output = 831 However,…
Pauline
  • 3,566
  • 8
  • 23
  • 39
17
votes
3 answers

Is there an FFT that uses a logarithmic division of frequency?

Wikipedia's Wavelet article contains this text: The discrete wavelet transform is also less computationally complex, taking O(N) time as compared to O(N log N) for the fast Fourier transform. This computational advantage is not inherent to the…
endolith
  • 25,479
  • 34
  • 128
  • 192
16
votes
1 answer

axis equal in a Matlab loglog plot

In Matlab the command 'axis equal': sets the aspect ratio so that equal tick mark increments on the x-,y- and z-axis are equal in size. This makes SPHERE(25) look like a sphere, instead of an ellipsoid However, when using the loglog…
Yossi Farjoun
  • 2,180
  • 3
  • 17
  • 25
16
votes
2 answers

Logarithmic plot of a cumulative distribution function in matplotlib

I have a file containing logged events. Each entry has a time and latency. I'm interested in plotting the cumulative distribution function of the latencies. I'm most interested in tail latencies so I want the plot to have a logarithmic y-axis. I'm…
nic
  • 1,511
  • 2
  • 14
  • 27
16
votes
2 answers

How does using log10 correctly calculate the length of a integer?

int length = (int) floor( log10 (float) number ) + 1; My question is essentially a math question: WHY does taking the log10() of a number, flooring that number, adding 1, and then casting it into an int correctly calculate the length of number? I…
user2926999
  • 393
  • 1
  • 3
  • 11
16
votes
3 answers

I need help proving that if f(n) = O(g(n)) implies 2^(f(n)) = O(2^g(n)))

In a previous problem, I showed (hopefully correctly) that f(n) = O(g(n)) implies lg(f(n)) = O(lg(g(n))) with sufficient conditions (e.g., lg(g(n)) >= 1, f(n) >= 1, and sufficiently large n). Now, I need to prove OR disprove that f(n) = O(g(n))…
norman
  • 5,128
  • 13
  • 44
  • 75
15
votes
4 answers

Make y-axis logarithmic in histogram using R

Hi I'm making histogram using R, but the number of Y axis is so large that I need to turn it into logarithmic.See below my script: hplot<-read.table("libl") hplot pdf("first_end") hist(hplot$V1, breaks=24, xlim=c(0,250000000),…
LookIntoEast
  • 8,048
  • 18
  • 64
  • 92
15
votes
3 answers

Problem of discrete logarithm calculation using Python code

I have a set of logarithm which are L1, L2 and L3 which I have retrieved it from the paper An Ultra-secure Router-to-router Spontaneous Key Exchange System (2015), here. The aim of this paper is to securely share key between Alice and Bob. For…
Afir
  • 483
  • 3
  • 15
15
votes
1 answer

Why are logarithms in computer science presumed to be base-2 logarithms?

I saw the following in a Computer Science text book, ... so, I was wondering if someone could explain to me why.
Andy
  • 349
  • 1
  • 2
  • 8
15
votes
4 answers

How can I compare the performance of log() and fp division in C++?

I’m using a log-based class in C++ to store very small floating-point values (as the values otherwise go beyond the scope of double). As I’m performing a large number of multiplications, this has the added benefit of converting the multiplications…
Ventzi Zhechev
  • 487
  • 1
  • 4
  • 12
15
votes
7 answers

Finding the exponent of n = 2**x using bitwise operations [logarithm in base 2 of n]

Is there a straightforward way to extracting the exponent from a power of 2 using bitwise operations only? EDIT: Although the question was originally about bitwise operations, the thread is a good read also if you are wondering "What's the fastest…
mac
  • 42,153
  • 26
  • 121
  • 131
15
votes
7 answers

Compute fast log base 2 ceiling in python

for given x < 10^15, quickly and accurately determine the maximum integer p such that 2^p <= x Here are some things I've tried: First I tried this but it's not accurate for large numbers: >>> from math import log >>> x = 2**3 >>> x 8 >>> p =…
Rusty Rob
  • 16,489
  • 8
  • 100
  • 116